github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xsql/isolation/isolation_test.go (about)

     1  package isolation
     2  
     3  import (
     4  	"database/sql"
     5  	"database/sql/driver"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    12  )
    13  
    14  func TestToYDB(t *testing.T) {
    15  	for _, tt := range []struct {
    16  		name      string
    17  		txOptions driver.TxOptions
    18  		txControl table.TxOption
    19  		err       bool
    20  	}{
    21  		// read-write
    22  		{
    23  			name: xtest.CurrentFileLine(),
    24  			txOptions: driver.TxOptions{
    25  				Isolation: driver.IsolationLevel(sql.LevelDefault),
    26  				ReadOnly:  false,
    27  			},
    28  			txControl: table.WithSerializableReadWrite(),
    29  			err:       false,
    30  		},
    31  		{
    32  			name: xtest.CurrentFileLine(),
    33  			txOptions: driver.TxOptions{
    34  				Isolation: driver.IsolationLevel(sql.LevelReadUncommitted),
    35  				ReadOnly:  false,
    36  			},
    37  			err: true,
    38  		},
    39  		{
    40  			name: xtest.CurrentFileLine(),
    41  			txOptions: driver.TxOptions{
    42  				Isolation: driver.IsolationLevel(sql.LevelReadCommitted),
    43  				ReadOnly:  false,
    44  			},
    45  			err: true,
    46  		},
    47  		{
    48  			name: xtest.CurrentFileLine(),
    49  			txOptions: driver.TxOptions{
    50  				Isolation: driver.IsolationLevel(sql.LevelWriteCommitted),
    51  				ReadOnly:  false,
    52  			},
    53  			err: true,
    54  		},
    55  		{
    56  			name: xtest.CurrentFileLine(),
    57  			txOptions: driver.TxOptions{
    58  				Isolation: driver.IsolationLevel(sql.LevelRepeatableRead),
    59  				ReadOnly:  false,
    60  			},
    61  			err: true,
    62  		},
    63  		{
    64  			name: xtest.CurrentFileLine(),
    65  			txOptions: driver.TxOptions{
    66  				Isolation: driver.IsolationLevel(sql.LevelSnapshot),
    67  				ReadOnly:  false,
    68  			},
    69  			err: true,
    70  		},
    71  		{
    72  			name: xtest.CurrentFileLine(),
    73  			txOptions: driver.TxOptions{
    74  				Isolation: driver.IsolationLevel(sql.LevelSerializable),
    75  				ReadOnly:  false,
    76  			},
    77  			txControl: table.WithSerializableReadWrite(),
    78  			err:       false,
    79  		},
    80  		{
    81  			name: xtest.CurrentFileLine(),
    82  			txOptions: driver.TxOptions{
    83  				Isolation: driver.IsolationLevel(sql.LevelLinearizable),
    84  				ReadOnly:  false,
    85  			},
    86  			err: true,
    87  		},
    88  
    89  		// read-only
    90  		{
    91  			name: xtest.CurrentFileLine(),
    92  			txOptions: driver.TxOptions{
    93  				Isolation: driver.IsolationLevel(sql.LevelDefault),
    94  				ReadOnly:  true,
    95  			},
    96  			err: true,
    97  		},
    98  		{
    99  			name: xtest.CurrentFileLine(),
   100  			txOptions: driver.TxOptions{
   101  				Isolation: driver.IsolationLevel(sql.LevelReadUncommitted),
   102  				ReadOnly:  true,
   103  			},
   104  			err: true,
   105  		},
   106  		{
   107  			name: xtest.CurrentFileLine(),
   108  			txOptions: driver.TxOptions{
   109  				Isolation: driver.IsolationLevel(sql.LevelReadCommitted),
   110  				ReadOnly:  true,
   111  			},
   112  			err: true,
   113  		},
   114  		{
   115  			name: xtest.CurrentFileLine(),
   116  			txOptions: driver.TxOptions{
   117  				Isolation: driver.IsolationLevel(sql.LevelWriteCommitted),
   118  				ReadOnly:  true,
   119  			},
   120  			err: true,
   121  		},
   122  		{
   123  			name: xtest.CurrentFileLine(),
   124  			txOptions: driver.TxOptions{
   125  				Isolation: driver.IsolationLevel(sql.LevelRepeatableRead),
   126  				ReadOnly:  true,
   127  			},
   128  			err: true,
   129  		},
   130  		{
   131  			name: xtest.CurrentFileLine(),
   132  			txOptions: driver.TxOptions{
   133  				Isolation: driver.IsolationLevel(sql.LevelSnapshot),
   134  				ReadOnly:  true,
   135  			},
   136  			txControl: table.WithSnapshotReadOnly(),
   137  			err:       false,
   138  		},
   139  		{
   140  			name: xtest.CurrentFileLine(),
   141  			txOptions: driver.TxOptions{
   142  				Isolation: driver.IsolationLevel(sql.LevelSerializable),
   143  				ReadOnly:  true,
   144  			},
   145  			err: true,
   146  		},
   147  		{
   148  			name: xtest.CurrentFileLine(),
   149  			txOptions: driver.TxOptions{
   150  				Isolation: driver.IsolationLevel(sql.LevelLinearizable),
   151  				ReadOnly:  true,
   152  			},
   153  			err: true,
   154  		},
   155  	} {
   156  		t.Run(tt.name, func(t *testing.T) {
   157  			toYDB, err := ToYDB(tt.txOptions)
   158  			if !tt.err {
   159  				require.NoError(t, err)
   160  				require.Equal(t, table.TxSettings(tt.txControl).Settings(), table.TxSettings(toYDB).Settings())
   161  			} else {
   162  				require.Error(t, err)
   163  			}
   164  		})
   165  	}
   166  }