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

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"database/sql"
     9  	"errors"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/ydb-platform/ydb-go-sdk/v3"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/retry"
    16  )
    17  
    18  func TestDatabaseSqlGetPrimaryKeys(t *testing.T) {
    19  	var (
    20  		scope = newScope(t)
    21  		db    = scope.SQLDriverWithFolder()
    22  	)
    23  
    24  	defer func() {
    25  		_ = db.Close()
    26  	}()
    27  
    28  	t.Run("create-tables", func(t *testing.T) {
    29  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    30  			_, err = cc.ExecContext(
    31  				ydb.WithQueryMode(ctx, ydb.SchemeQueryMode),
    32  				`CREATE TABLE episodes (
    33  				series_id Uint64,
    34  				season_id Uint64,
    35  				episode_id Uint64,
    36  				title UTF8,
    37  				air_date Date,
    38  				views Uint64,
    39  				PRIMARY KEY (
    40  					series_id,
    41  					season_id,
    42  					episode_id
    43  				)
    44  			)`,
    45  			)
    46  
    47  			return err
    48  		}, retry.WithIdempotent(true))
    49  
    50  		require.NoError(t, err)
    51  	})
    52  
    53  	t.Run("get-primary-keys", func(t *testing.T) {
    54  		err := retry.Do(scope.Ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
    55  			pkCols := make([]string, 0)
    56  			err = cc.Raw(func(drvConn interface{}) (err error) {
    57  				q, ok := drvConn.(interface {
    58  					GetPrimaryKeys(context.Context, string) ([]string, error)
    59  				})
    60  
    61  				if !ok {
    62  					return errors.New("drvConn does not implement extended API")
    63  				}
    64  
    65  				pkCols, err = q.GetPrimaryKeys(ctx, "episodes")
    66  				return err
    67  			})
    68  
    69  			if err != nil {
    70  				return err
    71  			}
    72  
    73  			require.ElementsMatch(t,
    74  				[]string{"series_id", "season_id", "episode_id"},
    75  				pkCols)
    76  			return nil
    77  		}, retry.WithIdempotent(true))
    78  
    79  		require.NoError(t, err)
    80  	})
    81  }