github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/tests/integration/table_create_table_description_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package integration 5 6 import ( 7 "context" 8 "os" 9 "path" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table" 14 15 "github.com/ydb-platform/ydb-go-sdk/v3" 16 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" 17 "github.com/ydb-platform/ydb-go-sdk/v3/log" 18 "github.com/ydb-platform/ydb-go-sdk/v3/sugar" 19 "github.com/ydb-platform/ydb-go-sdk/v3/table" 20 "github.com/ydb-platform/ydb-go-sdk/v3/table/options" 21 "github.com/ydb-platform/ydb-go-sdk/v3/table/types" 22 "github.com/ydb-platform/ydb-go-sdk/v3/trace" 23 ) 24 25 func TestCreateTableDescription(sourceTest *testing.T) { 26 t := xtest.MakeSyncedTest(sourceTest) 27 ctx := xtest.Context(t) 28 29 db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"), 30 ydb.WithLogger( 31 newLoggerWithMinLevel(t, log.WARN), 32 trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`), 33 ), 34 ) 35 if err != nil { 36 t.Fatal(err) 37 } 38 defer func() { 39 _ = db.Close(ctx) 40 }() 41 for _, tt := range []struct { 42 opts []options.CreateTableOption 43 description options.Description 44 equal func(t *testing.T, lhs, rhs options.Description) 45 }{ 46 { 47 opts: []options.CreateTableOption{ 48 options.WithColumn("a", types.TypeUint64), 49 options.WithPrimaryKeyColumn("a"), 50 }, 51 description: options.Description{ 52 Name: "table_0", 53 Columns: []options.Column{ 54 { 55 Name: "a", 56 Type: types.TypeUint64, 57 }, 58 }, 59 PrimaryKey: []string{"a"}, 60 }, 61 equal: func(t *testing.T, lhs, rhs options.Description) { 62 require.Equal(t, lhs.Columns, rhs.Columns) 63 require.Equal(t, lhs.PrimaryKey, rhs.PrimaryKey) 64 }, 65 }, 66 { 67 opts: []options.CreateTableOption{ 68 options.WithColumn("a", types.TypeUint64), 69 options.WithColumn("b", types.Optional(types.TypeUint64)), 70 options.WithPrimaryKeyColumn("a"), 71 options.WithIndex("idx_b", 72 options.WithIndexColumns("b"), 73 options.WithIndexType(options.GlobalIndex()), 74 ), 75 }, 76 description: options.Description{ 77 Name: "table_1", 78 Columns: []options.Column{ 79 { 80 Name: "a", 81 Type: types.TypeUint64, 82 }, 83 { 84 Name: "b", 85 Type: types.Optional(types.TypeUint64), 86 }, 87 }, 88 PrimaryKey: []string{"a"}, 89 Indexes: []options.IndexDescription{ 90 { 91 Name: "idx_b", 92 IndexColumns: []string{"b"}, 93 Status: Ydb_Table.TableIndexDescription_STATUS_READY, 94 Type: options.IndexTypeGlobal, 95 }, 96 }, 97 }, 98 equal: func(t *testing.T, lhs, rhs options.Description) { 99 require.Equal(t, lhs.Columns, rhs.Columns) 100 require.Equal(t, lhs.PrimaryKey, rhs.PrimaryKey) 101 require.Equal(t, lhs.Indexes, rhs.Indexes) 102 }, 103 }, 104 { 105 opts: []options.CreateTableOption{ 106 options.WithColumn("a", types.TypeUint64), 107 options.WithColumn("b", types.Optional(types.TypeUint64)), 108 options.WithPrimaryKeyColumn("a"), 109 options.WithIndex("idx_b", 110 options.WithIndexColumns("b"), 111 options.WithIndexType(options.GlobalAsyncIndex()), 112 ), 113 }, 114 description: options.Description{ 115 Name: "table_2", 116 Columns: []options.Column{ 117 { 118 Name: "a", 119 Type: types.TypeUint64, 120 }, 121 { 122 Name: "b", 123 Type: types.Optional(types.TypeUint64), 124 }, 125 }, 126 PrimaryKey: []string{"a"}, 127 Indexes: []options.IndexDescription{ 128 { 129 Name: "idx_b", 130 IndexColumns: []string{"b"}, 131 Status: Ydb_Table.TableIndexDescription_STATUS_READY, 132 Type: options.IndexTypeGlobalAsync, 133 }, 134 }, 135 }, 136 equal: func(t *testing.T, lhs, rhs options.Description) { 137 require.Equal(t, lhs.Columns, rhs.Columns) 138 require.Equal(t, lhs.PrimaryKey, rhs.PrimaryKey) 139 require.Equal(t, lhs.Indexes, rhs.Indexes) 140 }, 141 }, 142 } { 143 t.Run(tt.description.Name, func(t *testing.T) { 144 var ( 145 fullTablePath = path.Join(db.Name(), "TestCreateTableDescription", tt.description.Name) 146 description options.Description 147 ) 148 err = db.Table().Do(ctx, func(ctx context.Context, s table.Session) error { 149 var exists bool 150 if exists, err = sugar.IsTableExists(ctx, db.Scheme(), fullTablePath); err != nil { 151 return err 152 } else if exists { 153 _ = s.DropTable(ctx, fullTablePath) 154 } 155 err = s.CreateTable(ctx, fullTablePath, tt.opts...) 156 if err != nil { 157 return err 158 } 159 description, err = s.DescribeTable(ctx, fullTablePath) 160 if err != nil { 161 return err 162 } 163 return nil 164 }, table.WithIdempotent()) 165 require.NoError(t, err) 166 tt.equal(t, tt.description, description) 167 }) 168 } 169 }