|
| 1 | +//go:build integration |
| 2 | +// +build integration |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/version" |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/table" |
| 14 | + "github.com/ydb-platform/ydb-go-sdk/v3/table/options" |
| 15 | + "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named" |
| 16 | + "github.com/ydb-platform/ydb-go-sdk/v3/table/types" |
| 17 | +) |
| 18 | + |
| 19 | +func TestKeyValue(t *testing.T) { |
| 20 | + if version.Lt(os.Getenv("YDB_VERSION"), "23.3") { |
| 21 | + t.Skip("read rows not allowed in YDB version '" + os.Getenv("YDB_VERSION") + "'") |
| 22 | + } |
| 23 | + |
| 24 | + var ( |
| 25 | + scope = newScope(t) |
| 26 | + driver = scope.Driver() |
| 27 | + tablePath = scope.TablePath() |
| 28 | + id = int64(100500) |
| 29 | + value = "test value" |
| 30 | + ) |
| 31 | + |
| 32 | + // set |
| 33 | + err := driver.Table().Do(scope.Ctx, func(ctx context.Context, s table.Session) error { |
| 34 | + return s.BulkUpsert(ctx, tablePath, types.ListValue(types.StructValue( |
| 35 | + types.StructFieldValue("id", types.Int64Value(id)), |
| 36 | + types.StructFieldValue("val", types.TextValue(value)), |
| 37 | + ))) |
| 38 | + }) |
| 39 | + scope.Require.NoError(err) |
| 40 | + |
| 41 | + // get |
| 42 | + err = driver.Table().Do(scope.Ctx, func(ctx context.Context, s table.Session) error { |
| 43 | + rows, err := s.ReadRows(ctx, tablePath, types.ListValue(types.Int64Value(id)), |
| 44 | + options.ReadColumn("val"), |
| 45 | + ) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + defer func() { |
| 50 | + _ = rows.Close() |
| 51 | + }() |
| 52 | + if !rows.HasNextResultSet() { |
| 53 | + return fmt.Errorf("no result sets") |
| 54 | + } |
| 55 | + if !rows.HasNextRow() { |
| 56 | + return fmt.Errorf("no rows") |
| 57 | + } |
| 58 | + if rows.CurrentResultSet().RowCount() != 1 { |
| 59 | + return fmt.Errorf("wrong rows count (%d)", rows.CurrentResultSet().RowCount()) |
| 60 | + } |
| 61 | + if rows.CurrentResultSet().ColumnCount() != 1 { |
| 62 | + return fmt.Errorf("wrong column count (%d)", rows.CurrentResultSet().ColumnCount()) |
| 63 | + } |
| 64 | + var actualValue int64 |
| 65 | + if err := rows.ScanNamed(named.Optional("val", &actualValue)); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return rows.Err() |
| 69 | + }) |
| 70 | + scope.Require.NoError(err) |
| 71 | +} |
0 commit comments