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