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

     1  //go:build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"os"
    10  	"path"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    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/table"
    19  	"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
    20  	"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
    21  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    22  )
    23  
    24  func TestTableCreateTablePartitions(t *testing.T) {
    25  	ctx := xtest.Context(t)
    26  
    27  	db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"),
    28  		ydb.WithLogger(
    29  			newLoggerWithMinLevel(t, log.WARN),
    30  			trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
    31  		),
    32  	)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer func() {
    37  		_ = db.Close(ctx)
    38  	}()
    39  
    40  	t.Run("uniform partitions", func(t *testing.T) {
    41  		err := db.Table().Do(ctx,
    42  			func(ctx context.Context, session table.Session) error {
    43  				tablePath := path.Join(db.Name(), "uniformTable")
    44  
    45  				err := session.CreateTable(ctx, tablePath,
    46  					options.WithColumn("key", types.Optional(types.TypeUint64)),
    47  					options.WithColumn("value", types.Optional(types.TypeJSON)),
    48  					options.WithPrimaryKeyColumn("key"),
    49  
    50  					options.WithPartitions(options.WithUniformPartitions(4)),
    51  				)
    52  				if err != nil {
    53  					return fmt.Errorf("failed to create table: %w", err)
    54  				}
    55  
    56  				desc, err := session.DescribeTable(ctx, tablePath, options.WithShardKeyBounds())
    57  				if err != nil {
    58  					return fmt.Errorf("failed to get table description: %w", err)
    59  				}
    60  				if len(desc.KeyRanges) != 4 {
    61  					return errors.New("key ranges len is not as expected")
    62  				}
    63  
    64  				return nil
    65  			},
    66  		)
    67  		assert.NoError(t, err)
    68  	})
    69  
    70  	t.Run("explicit partitions", func(t *testing.T) {
    71  		err := db.Table().Do(ctx,
    72  			func(ctx context.Context, session table.Session) error {
    73  				tablePath := path.Join(db.Name(), "explicitTable")
    74  
    75  				err := session.CreateTable(ctx, tablePath,
    76  					options.WithColumn("key", types.Optional(types.TypeUint64)),
    77  					options.WithColumn("value", types.Optional(types.TypeJSON)),
    78  					options.WithPrimaryKeyColumn("key"),
    79  
    80  					options.WithPartitions(
    81  						options.WithExplicitPartitions(
    82  							types.TupleValue(types.OptionalValue(types.Uint64Value(100))),
    83  							types.TupleValue(types.OptionalValue(types.Uint64Value(300))),
    84  							types.TupleValue(types.OptionalValue(types.Uint64Value(400))),
    85  						),
    86  					),
    87  				)
    88  				if err != nil {
    89  					return fmt.Errorf("failed to create table: %w", err)
    90  				}
    91  
    92  				desc, err := session.DescribeTable(ctx, tablePath, options.WithShardKeyBounds())
    93  				if err != nil {
    94  					return fmt.Errorf("failed to get table description: %w", err)
    95  				}
    96  				if len(desc.KeyRanges) != 4 {
    97  					return errors.New("key ranges len is not as expected")
    98  				}
    99  
   100  				return nil
   101  			},
   102  		)
   103  		assert.NoError(t, err)
   104  	})
   105  }