Skip to content

Commit 8daa3c1

Browse files
committed
added e2e test for index type
1 parent 2d94108 commit 8daa3c1

File tree

3 files changed

+150
-3
lines changed

3 files changed

+150
-3
lines changed

internal/table/session.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ func (s *session) CreateTable(
294294
}
295295
}
296296
_, err = s.tableService.CreateTable(ctx, &request)
297-
return xerrors.WithStackTrace(err)
297+
if err != nil {
298+
return xerrors.WithStackTrace(err)
299+
}
300+
return nil
298301
}
299302

300303
// DescribeTable describes table at given path.

table/options/models.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,13 @@ const (
283283
func (t IndexType) ApplyIndexOption(d *indexDesc) {
284284
switch t {
285285
case IndexTypeGlobal:
286-
d.Type = &Ydb_Table.TableIndex_GlobalIndex{}
286+
d.Type = &Ydb_Table.TableIndex_GlobalIndex{
287+
GlobalIndex: &Ydb_Table.GlobalIndex{},
288+
}
287289
case IndexTypeGlobalAsync:
288-
d.Type = &Ydb_Table.TableIndex_GlobalAsyncIndex{}
290+
d.Type = &Ydb_Table.TableIndex_GlobalAsyncIndex{
291+
GlobalAsyncIndex: &Ydb_Table.GlobalAsyncIndex{},
292+
}
289293
}
290294
}
291295

table/table_e2e_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/stretchr/testify/require"
27+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
2728
"golang.org/x/xerrors"
2829
"google.golang.org/grpc"
2930
grpcCodes "google.golang.org/grpc/codes"
@@ -2291,3 +2292,142 @@ func TestValueToYqlLiteral(t *testing.T) {
22912292
})
22922293
}
22932294
}
2295+
2296+
func TestCreateTableDescription(t *testing.T) {
2297+
ctx := context.Background()
2298+
db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"))
2299+
if err != nil {
2300+
t.Fatal(err)
2301+
}
2302+
defer func() {
2303+
_ = db.Close(ctx)
2304+
}()
2305+
for _, tt := range []struct {
2306+
opts []options.CreateTableOption
2307+
description options.Description
2308+
equal func(t *testing.T, lhs, rhs options.Description)
2309+
}{
2310+
{
2311+
opts: []options.CreateTableOption{
2312+
options.WithColumn("a", types.TypeUint64),
2313+
options.WithPrimaryKeyColumn("a"),
2314+
},
2315+
description: options.Description{
2316+
Name: "table_0",
2317+
Columns: []options.Column{
2318+
{
2319+
Name: "a",
2320+
Type: types.TypeUint64,
2321+
},
2322+
},
2323+
PrimaryKey: []string{"a"},
2324+
},
2325+
equal: func(t *testing.T, lhs, rhs options.Description) {
2326+
require.Equal(t, lhs.Columns, rhs.Columns)
2327+
require.Equal(t, lhs.PrimaryKey, rhs.PrimaryKey)
2328+
},
2329+
},
2330+
{
2331+
opts: []options.CreateTableOption{
2332+
options.WithColumn("a", types.TypeUint64),
2333+
options.WithColumn("b", types.Optional(types.TypeUint64)),
2334+
options.WithPrimaryKeyColumn("a"),
2335+
options.WithIndex("idx_b",
2336+
options.WithIndexColumns("b"),
2337+
options.WithIndexType(options.GlobalIndex()),
2338+
),
2339+
},
2340+
description: options.Description{
2341+
Name: "table_1",
2342+
Columns: []options.Column{
2343+
{
2344+
Name: "a",
2345+
Type: types.TypeUint64,
2346+
},
2347+
{
2348+
Name: "b",
2349+
Type: types.Optional(types.TypeUint64),
2350+
},
2351+
},
2352+
PrimaryKey: []string{"a"},
2353+
Indexes: []options.IndexDescription{
2354+
{
2355+
Name: "idx_b",
2356+
IndexColumns: []string{"b"},
2357+
Status: Ydb_Table.TableIndexDescription_STATUS_READY,
2358+
Type: options.IndexTypeGlobal,
2359+
},
2360+
},
2361+
},
2362+
equal: func(t *testing.T, lhs, rhs options.Description) {
2363+
require.Equal(t, lhs.Columns, rhs.Columns)
2364+
require.Equal(t, lhs.PrimaryKey, rhs.PrimaryKey)
2365+
require.Equal(t, lhs.Indexes, rhs.Indexes)
2366+
},
2367+
},
2368+
{
2369+
opts: []options.CreateTableOption{
2370+
options.WithColumn("a", types.TypeUint64),
2371+
options.WithColumn("b", types.Optional(types.TypeUint64)),
2372+
options.WithPrimaryKeyColumn("a"),
2373+
options.WithIndex("idx_b",
2374+
options.WithIndexColumns("b"),
2375+
options.WithIndexType(options.GlobalAsyncIndex()),
2376+
),
2377+
},
2378+
description: options.Description{
2379+
Name: "table_2",
2380+
Columns: []options.Column{
2381+
{
2382+
Name: "a",
2383+
Type: types.TypeUint64,
2384+
},
2385+
{
2386+
Name: "b",
2387+
Type: types.Optional(types.TypeUint64),
2388+
},
2389+
},
2390+
PrimaryKey: []string{"a"},
2391+
Indexes: []options.IndexDescription{
2392+
{
2393+
Name: "idx_b",
2394+
IndexColumns: []string{"b"},
2395+
Status: Ydb_Table.TableIndexDescription_STATUS_READY,
2396+
Type: options.IndexTypeGlobalAsync,
2397+
},
2398+
},
2399+
},
2400+
equal: func(t *testing.T, lhs, rhs options.Description) {
2401+
require.Equal(t, lhs.Columns, rhs.Columns)
2402+
require.Equal(t, lhs.PrimaryKey, rhs.PrimaryKey)
2403+
require.Equal(t, lhs.Indexes, rhs.Indexes)
2404+
},
2405+
},
2406+
} {
2407+
t.Run(tt.description.Name, func(t *testing.T) {
2408+
var (
2409+
fullTablePath = path.Join(db.Name(), "TestCreateTableDescription", tt.description.Name)
2410+
description options.Description
2411+
)
2412+
err = db.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
2413+
var exists bool
2414+
if exists, err = sugar.IsTableExists(ctx, db.Scheme(), fullTablePath); err != nil {
2415+
return err
2416+
} else if exists {
2417+
_ = s.DropTable(ctx, fullTablePath)
2418+
}
2419+
err = s.CreateTable(ctx, fullTablePath, tt.opts...)
2420+
if err != nil {
2421+
return err
2422+
}
2423+
description, err = s.DescribeTable(ctx, fullTablePath)
2424+
if err != nil {
2425+
return err
2426+
}
2427+
return nil
2428+
}, table.WithIdempotent())
2429+
require.NoError(t, err)
2430+
tt.equal(t, tt.description, description)
2431+
})
2432+
}
2433+
}

0 commit comments

Comments
 (0)