github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xsql/dsn_test.go (about)

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