github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/kv_test.go (about)

     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,
    44  			types.ListValue(types.StructValue(
    45  				types.StructFieldValue("id", types.Int64Value(id)),
    46  			)),
    47  			options.ReadColumn("val"),
    48  		)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		defer func() {
    53  			_ = rows.Close()
    54  		}()
    55  		if !rows.NextResultSet(ctx) {
    56  			return fmt.Errorf("no result sets")
    57  		}
    58  		if !rows.NextRow() {
    59  			return fmt.Errorf("no rows")
    60  		}
    61  		if rows.CurrentResultSet().RowCount() != 1 {
    62  			return fmt.Errorf("wrong rows count (%d)", rows.CurrentResultSet().RowCount())
    63  		}
    64  		if rows.CurrentResultSet().ColumnCount() != 1 {
    65  			return fmt.Errorf("wrong column count (%d)", rows.CurrentResultSet().ColumnCount())
    66  		}
    67  		var actualValue string
    68  		if err := rows.ScanNamed(named.OptionalWithDefault("val", &actualValue)); err != nil {
    69  			return err
    70  		}
    71  		t.Logf("%s[%d] = %q", tablePath, id, actualValue)
    72  		return rows.Err()
    73  	})
    74  	scope.Require.NoError(err)
    75  }