github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/dsn_test.go (about)

     1  package ydb
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/config"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql"
    12  )
    13  
    14  func TestParse(t *testing.T) {
    15  	newConnector := func(opts ...xsql.ConnectorOption) *xsql.Connector {
    16  		c := &xsql.Connector{}
    17  		for _, opt := range opts {
    18  			if opt != nil {
    19  				if err := opt.Apply(c); err != nil {
    20  					t.Error(err)
    21  				}
    22  			}
    23  		}
    24  
    25  		return c
    26  	}
    27  	compareConfigs := func(t *testing.T, lhs, rhs *config.Config) {
    28  		require.Equal(t, lhs.Secure(), rhs.Secure())
    29  		require.Equal(t, lhs.Endpoint(), rhs.Endpoint())
    30  		require.Equal(t, lhs.Database(), rhs.Database())
    31  	}
    32  	for _, tt := range []struct {
    33  		dsn           string
    34  		opts          []config.Option
    35  		connectorOpts []xsql.ConnectorOption
    36  		err           error
    37  	}{
    38  		{
    39  			dsn: "grpc://localhost:2135/local?go_fake_tx=scripting,scheme",
    40  			opts: []config.Option{
    41  				config.WithSecure(false),
    42  				config.WithEndpoint("localhost:2135"),
    43  				config.WithDatabase("/local"),
    44  			},
    45  			connectorOpts: []xsql.ConnectorOption{
    46  				xsql.WithFakeTx(xsql.ScriptingQueryMode),
    47  				xsql.WithFakeTx(xsql.SchemeQueryMode),
    48  			},
    49  			err: nil,
    50  		},
    51  		{
    52  			dsn: "grpc://localhost:2135/local",
    53  			opts: []config.Option{
    54  				config.WithSecure(false),
    55  				config.WithEndpoint("localhost:2135"),
    56  				config.WithDatabase("/local"),
    57  			},
    58  			connectorOpts: nil,
    59  			err:           nil,
    60  		},
    61  		{
    62  			dsn: "grpcs://localhost:2135/local/db",
    63  			opts: []config.Option{
    64  				config.WithSecure(true),
    65  				config.WithEndpoint("localhost:2135"),
    66  				config.WithDatabase("/local/db"),
    67  			},
    68  			connectorOpts: nil,
    69  			err:           nil,
    70  		},
    71  		{
    72  			dsn: "grpc://localhost:2135/local?query_mode=scripting",
    73  			opts: []config.Option{
    74  				config.WithSecure(false),
    75  				config.WithEndpoint("localhost:2135"),
    76  				config.WithDatabase("/local"),
    77  			},
    78  			connectorOpts: []xsql.ConnectorOption{
    79  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
    80  			},
    81  			err: nil,
    82  		},
    83  		{
    84  			dsn: "grpc://localhost:2135/local?query_mode=scripting&go_query_bind=table_path_prefix(path/to/tables)",
    85  			opts: []config.Option{
    86  				config.WithSecure(false),
    87  				config.WithEndpoint("localhost:2135"),
    88  				config.WithDatabase("/local"),
    89  			},
    90  			connectorOpts: []xsql.ConnectorOption{
    91  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
    92  				xsql.WithTablePathPrefix("path/to/tables"),
    93  			},
    94  			err: nil,
    95  		},
    96  		{
    97  			dsn: "grpc://localhost:2135/local?query_mode=scripting&go_query_bind=table_path_prefix(path/to/tables),numeric", //nolint:lll
    98  			opts: []config.Option{
    99  				config.WithSecure(false),
   100  				config.WithEndpoint("localhost:2135"),
   101  				config.WithDatabase("/local"),
   102  			},
   103  			connectorOpts: []xsql.ConnectorOption{
   104  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
   105  				xsql.WithTablePathPrefix("path/to/tables"),
   106  				xsql.WithQueryBind(bind.NumericArgs{}),
   107  			},
   108  			err: nil,
   109  		},
   110  		{
   111  			dsn: "grpc://localhost:2135/local?query_mode=scripting&go_query_bind=table_path_prefix(path/to/tables),positional", //nolint:lll
   112  			opts: []config.Option{
   113  				config.WithSecure(false),
   114  				config.WithEndpoint("localhost:2135"),
   115  				config.WithDatabase("/local"),
   116  			},
   117  			connectorOpts: []xsql.ConnectorOption{
   118  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
   119  				xsql.WithTablePathPrefix("path/to/tables"),
   120  				xsql.WithQueryBind(bind.PositionalArgs{}),
   121  			},
   122  			err: nil,
   123  		},
   124  		{
   125  			dsn: "grpc://localhost:2135/local?query_mode=scripting&go_query_bind=table_path_prefix(path/to/tables),declare",
   126  			opts: []config.Option{
   127  				config.WithSecure(false),
   128  				config.WithEndpoint("localhost:2135"),
   129  				config.WithDatabase("/local"),
   130  			},
   131  			connectorOpts: []xsql.ConnectorOption{
   132  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
   133  				xsql.WithTablePathPrefix("path/to/tables"),
   134  				xsql.WithQueryBind(bind.AutoDeclare{}),
   135  			},
   136  			err: nil,
   137  		},
   138  		{
   139  			dsn: "grpc://localhost:2135/local?query_mode=scripting&go_query_bind=table_path_prefix(path/to/tables)",
   140  			opts: []config.Option{
   141  				config.WithSecure(false),
   142  				config.WithEndpoint("localhost:2135"),
   143  				config.WithDatabase("/local"),
   144  			},
   145  			connectorOpts: []xsql.ConnectorOption{
   146  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
   147  				xsql.WithTablePathPrefix("path/to/tables"),
   148  			},
   149  			err: nil,
   150  		},
   151  		{
   152  			dsn: "grpc://localhost:2135/local?query_mode=scripting&go_query_bind=positional,declare,table_path_prefix(path/to/tables)", //nolint:lll
   153  			opts: []config.Option{
   154  				config.WithSecure(false),
   155  				config.WithEndpoint("localhost:2135"),
   156  				config.WithDatabase("/local"),
   157  			},
   158  			connectorOpts: []xsql.ConnectorOption{
   159  				xsql.WithDefaultQueryMode(xsql.ScriptingQueryMode),
   160  				xsql.WithTablePathPrefix("path/to/tables"),
   161  				xsql.WithQueryBind(bind.PositionalArgs{}),
   162  				xsql.WithQueryBind(bind.AutoDeclare{}),
   163  			},
   164  			err: nil,
   165  		},
   166  	} {
   167  		t.Run("", func(t *testing.T) {
   168  			opts, err := parseConnectionString(tt.dsn)
   169  			if tt.err != nil {
   170  				require.ErrorIs(t, err, tt.err)
   171  			} else {
   172  				require.NoError(t, err)
   173  				d, err := newConnectionFromOptions(context.Background(), opts...)
   174  				require.NoError(t, err)
   175  				require.Equal(t, newConnector(tt.connectorOpts...), newConnector(d.databaseSQLOptions...))
   176  				compareConfigs(t, config.New(tt.opts...), d.config)
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestExtractTablePathPrefixFromBinderName(t *testing.T) {
   183  	for _, tt := range []struct {
   184  		binderName      string
   185  		tablePathPrefix string
   186  		err             error
   187  	}{
   188  		{
   189  			binderName:      "table_path_prefix(path/to/tables)",
   190  			tablePathPrefix: "path/to/tables",
   191  		},
   192  		{
   193  			binderName:      "table_path_prefix()",
   194  			tablePathPrefix: "",
   195  			err:             errWrongTablePathPrefix,
   196  		},
   197  		{
   198  			binderName:      "table_path_prefix",
   199  			tablePathPrefix: "",
   200  			err:             errWrongTablePathPrefix,
   201  		},
   202  		{
   203  			binderName:      "TablePathPrefix(path/to/tables)",
   204  			tablePathPrefix: "",
   205  			err:             errWrongTablePathPrefix,
   206  		},
   207  	} {
   208  		t.Run("", func(t *testing.T) {
   209  			tablePathPrefix, err := extractTablePathPrefixFromBinderName(tt.binderName)
   210  			if tt.err != nil {
   211  				require.ErrorIs(t, err, tt.err)
   212  			} else {
   213  				require.NoError(t, err)
   214  				require.Equal(t, tt.tablePathPrefix, tablePathPrefix)
   215  			}
   216  		})
   217  	}
   218  }