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

     1  package params
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     9  
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    12  )
    13  
    14  func TestOptional(t *testing.T) {
    15  	for _, tt := range []struct {
    16  		name    string
    17  		builder Builder
    18  		params  map[string]*Ydb.TypedValue
    19  	}{
    20  		{
    21  			name:    xtest.CurrentFileLine(),
    22  			builder: Builder{}.Param("$x").Optional().Uint64(123).Build(),
    23  			params: map[string]*Ydb.TypedValue{
    24  				"$x": {
    25  					Type: &Ydb.Type{
    26  						Type: &Ydb.Type_OptionalType{
    27  							OptionalType: &Ydb.OptionalType{
    28  								Item: &Ydb.Type{
    29  									Type: &Ydb.Type_TypeId{
    30  										TypeId: Ydb.Type_UINT64,
    31  									},
    32  								},
    33  							},
    34  						},
    35  					},
    36  					Value: &Ydb.Value{
    37  						Value: &Ydb.Value_Uint64Value{
    38  							Uint64Value: 123,
    39  						},
    40  					},
    41  				},
    42  			},
    43  		},
    44  		{
    45  			name:    xtest.CurrentFileLine(),
    46  			builder: Builder{}.Param("$x").Optional().Int64(123).Build(),
    47  			params: map[string]*Ydb.TypedValue{
    48  				"$x": {
    49  					Type: &Ydb.Type{
    50  						Type: &Ydb.Type_OptionalType{
    51  							OptionalType: &Ydb.OptionalType{
    52  								Item: &Ydb.Type{
    53  									Type: &Ydb.Type_TypeId{
    54  										TypeId: Ydb.Type_INT64,
    55  									},
    56  								},
    57  							},
    58  						},
    59  					},
    60  					Value: &Ydb.Value{
    61  						Value: &Ydb.Value_Int64Value{
    62  							Int64Value: 123,
    63  						},
    64  					},
    65  				},
    66  			},
    67  		},
    68  		{
    69  			name:    xtest.CurrentFileLine(),
    70  			builder: Builder{}.Param("$x").Optional().Uint32(123).Build(),
    71  			params: map[string]*Ydb.TypedValue{
    72  				"$x": {
    73  					Type: &Ydb.Type{
    74  						Type: &Ydb.Type_OptionalType{
    75  							OptionalType: &Ydb.OptionalType{
    76  								Item: &Ydb.Type{
    77  									Type: &Ydb.Type_TypeId{
    78  										TypeId: Ydb.Type_UINT32,
    79  									},
    80  								},
    81  							},
    82  						},
    83  					},
    84  					Value: &Ydb.Value{
    85  						Value: &Ydb.Value_Uint32Value{
    86  							Uint32Value: 123,
    87  						},
    88  					},
    89  				},
    90  			},
    91  		},
    92  		{
    93  			name:    xtest.CurrentFileLine(),
    94  			builder: Builder{}.Param("$x").Optional().Int32(123).Build(),
    95  			params: map[string]*Ydb.TypedValue{
    96  				"$x": {
    97  					Type: &Ydb.Type{
    98  						Type: &Ydb.Type_OptionalType{
    99  							OptionalType: &Ydb.OptionalType{
   100  								Item: &Ydb.Type{
   101  									Type: &Ydb.Type_TypeId{
   102  										TypeId: Ydb.Type_INT32,
   103  									},
   104  								},
   105  							},
   106  						},
   107  					},
   108  					Value: &Ydb.Value{
   109  						Value: &Ydb.Value_Int32Value{
   110  							Int32Value: 123,
   111  						},
   112  					},
   113  				},
   114  			},
   115  		},
   116  		{
   117  			name:    xtest.CurrentFileLine(),
   118  			builder: Builder{}.Param("$x").Optional().Uint16(123).Build(),
   119  			params: map[string]*Ydb.TypedValue{
   120  				"$x": {
   121  					Type: &Ydb.Type{
   122  						Type: &Ydb.Type_OptionalType{
   123  							OptionalType: &Ydb.OptionalType{
   124  								Item: &Ydb.Type{
   125  									Type: &Ydb.Type_TypeId{
   126  										TypeId: Ydb.Type_UINT16,
   127  									},
   128  								},
   129  							},
   130  						},
   131  					},
   132  					Value: &Ydb.Value{
   133  						Value: &Ydb.Value_Uint32Value{
   134  							Uint32Value: 123,
   135  						},
   136  					},
   137  				},
   138  			},
   139  		},
   140  		{
   141  			name:    xtest.CurrentFileLine(),
   142  			builder: Builder{}.Param("$x").Optional().Int16(123).Build(),
   143  			params: map[string]*Ydb.TypedValue{
   144  				"$x": {
   145  					Type: &Ydb.Type{
   146  						Type: &Ydb.Type_OptionalType{
   147  							OptionalType: &Ydb.OptionalType{
   148  								Item: &Ydb.Type{
   149  									Type: &Ydb.Type_TypeId{
   150  										TypeId: Ydb.Type_INT16,
   151  									},
   152  								},
   153  							},
   154  						},
   155  					},
   156  					Value: &Ydb.Value{
   157  						Value: &Ydb.Value_Int32Value{
   158  							Int32Value: 123,
   159  						},
   160  					},
   161  				},
   162  			},
   163  		},
   164  		{
   165  			name:    xtest.CurrentFileLine(),
   166  			builder: Builder{}.Param("$x").Optional().Uint8(123).Build(),
   167  			params: map[string]*Ydb.TypedValue{
   168  				"$x": {
   169  					Type: &Ydb.Type{
   170  						Type: &Ydb.Type_OptionalType{
   171  							OptionalType: &Ydb.OptionalType{
   172  								Item: &Ydb.Type{
   173  									Type: &Ydb.Type_TypeId{
   174  										TypeId: Ydb.Type_UINT8,
   175  									},
   176  								},
   177  							},
   178  						},
   179  					},
   180  					Value: &Ydb.Value{
   181  						Value: &Ydb.Value_Uint32Value{
   182  							Uint32Value: 123,
   183  						},
   184  					},
   185  				},
   186  			},
   187  		},
   188  		{
   189  			name:    xtest.CurrentFileLine(),
   190  			builder: Builder{}.Param("$x").Optional().Int8(123).Build(),
   191  			params: map[string]*Ydb.TypedValue{
   192  				"$x": {
   193  					Type: &Ydb.Type{
   194  						Type: &Ydb.Type_OptionalType{
   195  							OptionalType: &Ydb.OptionalType{
   196  								Item: &Ydb.Type{
   197  									Type: &Ydb.Type_TypeId{
   198  										TypeId: Ydb.Type_INT8,
   199  									},
   200  								},
   201  							},
   202  						},
   203  					},
   204  					Value: &Ydb.Value{
   205  						Value: &Ydb.Value_Int32Value{
   206  							Int32Value: 123,
   207  						},
   208  					},
   209  				},
   210  			},
   211  		},
   212  		{
   213  			name:    xtest.CurrentFileLine(),
   214  			builder: Builder{}.Param("$x").Optional().Bool(true).Build(),
   215  			params: map[string]*Ydb.TypedValue{
   216  				"$x": {
   217  					Type: &Ydb.Type{
   218  						Type: &Ydb.Type_OptionalType{
   219  							OptionalType: &Ydb.OptionalType{
   220  								Item: &Ydb.Type{
   221  									Type: &Ydb.Type_TypeId{
   222  										TypeId: Ydb.Type_BOOL,
   223  									},
   224  								},
   225  							},
   226  						},
   227  					},
   228  					Value: &Ydb.Value{
   229  						Value: &Ydb.Value_BoolValue{
   230  							BoolValue: true,
   231  						},
   232  					},
   233  				},
   234  			},
   235  		},
   236  		{
   237  			name:    xtest.CurrentFileLine(),
   238  			builder: Builder{}.Param("$x").Optional().Text("test").Build(),
   239  			params: map[string]*Ydb.TypedValue{
   240  				"$x": {
   241  					Type: &Ydb.Type{
   242  						Type: &Ydb.Type_OptionalType{
   243  							OptionalType: &Ydb.OptionalType{
   244  								Item: &Ydb.Type{
   245  									Type: &Ydb.Type_TypeId{
   246  										TypeId: Ydb.Type_UTF8,
   247  									},
   248  								},
   249  							},
   250  						},
   251  					},
   252  					Value: &Ydb.Value{
   253  						Value: &Ydb.Value_TextValue{
   254  							TextValue: "test",
   255  						},
   256  					},
   257  				},
   258  			},
   259  		},
   260  		{
   261  			name:    xtest.CurrentFileLine(),
   262  			builder: Builder{}.Param("$x").Optional().Bytes([]byte("test")).Build(),
   263  			params: map[string]*Ydb.TypedValue{
   264  				"$x": {
   265  					Type: &Ydb.Type{
   266  						Type: &Ydb.Type_OptionalType{
   267  							OptionalType: &Ydb.OptionalType{
   268  								Item: &Ydb.Type{
   269  									Type: &Ydb.Type_TypeId{
   270  										TypeId: Ydb.Type_STRING,
   271  									},
   272  								},
   273  							},
   274  						},
   275  					},
   276  					Value: &Ydb.Value{
   277  						Value: &Ydb.Value_BytesValue{
   278  							BytesValue: []byte("test"),
   279  						},
   280  					},
   281  				},
   282  			},
   283  		},
   284  		{
   285  			name:    xtest.CurrentFileLine(),
   286  			builder: Builder{}.Param("$x").Optional().Float(123).Build(),
   287  			params: map[string]*Ydb.TypedValue{
   288  				"$x": {
   289  					Type: &Ydb.Type{
   290  						Type: &Ydb.Type_OptionalType{
   291  							OptionalType: &Ydb.OptionalType{
   292  								Item: &Ydb.Type{
   293  									Type: &Ydb.Type_TypeId{
   294  										TypeId: Ydb.Type_FLOAT,
   295  									},
   296  								},
   297  							},
   298  						},
   299  					},
   300  					Value: &Ydb.Value{
   301  						Value: &Ydb.Value_FloatValue{
   302  							FloatValue: 123,
   303  						},
   304  					},
   305  				},
   306  			},
   307  		},
   308  		{
   309  			name:    xtest.CurrentFileLine(),
   310  			builder: Builder{}.Param("$x").Optional().Double(123).Build(),
   311  			params: map[string]*Ydb.TypedValue{
   312  				"$x": {
   313  					Type: &Ydb.Type{
   314  						Type: &Ydb.Type_OptionalType{
   315  							OptionalType: &Ydb.OptionalType{
   316  								Item: &Ydb.Type{
   317  									Type: &Ydb.Type_TypeId{
   318  										TypeId: Ydb.Type_DOUBLE,
   319  									},
   320  								},
   321  							},
   322  						},
   323  					},
   324  					Value: &Ydb.Value{
   325  						Value: &Ydb.Value_DoubleValue{
   326  							DoubleValue: 123,
   327  						},
   328  					},
   329  				},
   330  			},
   331  		},
   332  		{
   333  			name:    xtest.CurrentFileLine(),
   334  			builder: Builder{}.Param("$x").Optional().Interval(time.Second).Build(),
   335  			params: map[string]*Ydb.TypedValue{
   336  				"$x": {
   337  					Type: &Ydb.Type{
   338  						Type: &Ydb.Type_OptionalType{
   339  							OptionalType: &Ydb.OptionalType{
   340  								Item: &Ydb.Type{
   341  									Type: &Ydb.Type_TypeId{
   342  										TypeId: Ydb.Type_INTERVAL,
   343  									},
   344  								},
   345  							},
   346  						},
   347  					},
   348  					Value: &Ydb.Value{
   349  						Value: &Ydb.Value_Int64Value{
   350  							Int64Value: 1000000,
   351  						},
   352  					},
   353  				},
   354  			},
   355  		},
   356  		{
   357  			name:    xtest.CurrentFileLine(),
   358  			builder: Builder{}.Param("$x").Optional().Datetime(time.Unix(123456789, 456)).Build(),
   359  			params: map[string]*Ydb.TypedValue{
   360  				"$x": {
   361  					Type: &Ydb.Type{
   362  						Type: &Ydb.Type_OptionalType{
   363  							OptionalType: &Ydb.OptionalType{
   364  								Item: &Ydb.Type{
   365  									Type: &Ydb.Type_TypeId{
   366  										TypeId: Ydb.Type_DATETIME,
   367  									},
   368  								},
   369  							},
   370  						},
   371  					},
   372  					Value: &Ydb.Value{
   373  						Value: &Ydb.Value_Uint32Value{
   374  							Uint32Value: 123456789,
   375  						},
   376  					},
   377  				},
   378  			},
   379  		},
   380  		{
   381  			name:    xtest.CurrentFileLine(),
   382  			builder: Builder{}.Param("$x").Optional().Date(time.Unix(123456789, 456)).Build(),
   383  			params: map[string]*Ydb.TypedValue{
   384  				"$x": {
   385  					Type: &Ydb.Type{
   386  						Type: &Ydb.Type_OptionalType{
   387  							OptionalType: &Ydb.OptionalType{
   388  								Item: &Ydb.Type{
   389  									Type: &Ydb.Type_TypeId{
   390  										TypeId: Ydb.Type_DATE,
   391  									},
   392  								},
   393  							},
   394  						},
   395  					},
   396  					Value: &Ydb.Value{
   397  						Value: &Ydb.Value_Uint32Value{
   398  							Uint32Value: 1428,
   399  						},
   400  					},
   401  				},
   402  			},
   403  		},
   404  		{
   405  			name:    xtest.CurrentFileLine(),
   406  			builder: Builder{}.Param("$x").Optional().Timestamp(time.Unix(123456789, 456)).Build(),
   407  			params: map[string]*Ydb.TypedValue{
   408  				"$x": {
   409  					Type: &Ydb.Type{
   410  						Type: &Ydb.Type_OptionalType{
   411  							OptionalType: &Ydb.OptionalType{
   412  								Item: &Ydb.Type{
   413  									Type: &Ydb.Type_TypeId{
   414  										TypeId: Ydb.Type_TIMESTAMP,
   415  									},
   416  								},
   417  							},
   418  						},
   419  					},
   420  					Value: &Ydb.Value{
   421  						Value: &Ydb.Value_Uint64Value{
   422  							Uint64Value: 123456789000000,
   423  						},
   424  					},
   425  				},
   426  			},
   427  		},
   428  		{
   429  			name:    xtest.CurrentFileLine(),
   430  			builder: Builder{}.Param("$x").Optional().Decimal([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}, 22, 9).Build(), //nolint:lll
   431  			params: map[string]*Ydb.TypedValue{
   432  				"$x": {
   433  					Type: &Ydb.Type{
   434  						Type: &Ydb.Type_OptionalType{
   435  							OptionalType: &Ydb.OptionalType{
   436  								Item: &Ydb.Type{
   437  									Type: &Ydb.Type_DecimalType{
   438  										DecimalType: &Ydb.DecimalType{
   439  											Precision: 22,
   440  											Scale:     9,
   441  										},
   442  									},
   443  								},
   444  							},
   445  						},
   446  					},
   447  					Value: &Ydb.Value{
   448  						High_128: 72623859790382856,
   449  						Value: &Ydb.Value_Low_128{
   450  							Low_128: 648519454493508870,
   451  						},
   452  					},
   453  				},
   454  			},
   455  		},
   456  		{
   457  			name:    xtest.CurrentFileLine(),
   458  			builder: Builder{}.Param("$x").Optional().JSON(`{"a": 1,"b": "B"}`).Build(),
   459  			params: map[string]*Ydb.TypedValue{
   460  				"$x": {
   461  					Type: &Ydb.Type{
   462  						Type: &Ydb.Type_OptionalType{
   463  							OptionalType: &Ydb.OptionalType{
   464  								Item: &Ydb.Type{
   465  									Type: &Ydb.Type_TypeId{
   466  										TypeId: Ydb.Type_JSON,
   467  									},
   468  								},
   469  							},
   470  						},
   471  					},
   472  					Value: &Ydb.Value{
   473  						Value: &Ydb.Value_TextValue{
   474  							TextValue: `{"a": 1,"b": "B"}`,
   475  						},
   476  					},
   477  				},
   478  			},
   479  		},
   480  		{
   481  			name:    xtest.CurrentFileLine(),
   482  			builder: Builder{}.Param("$x").Optional().JSONDocument(`{"a": 1,"b": "B"}`).Build(),
   483  			params: map[string]*Ydb.TypedValue{
   484  				"$x": {
   485  					Type: &Ydb.Type{
   486  						Type: &Ydb.Type_OptionalType{
   487  							OptionalType: &Ydb.OptionalType{
   488  								Item: &Ydb.Type{
   489  									Type: &Ydb.Type_TypeId{
   490  										TypeId: Ydb.Type_JSON_DOCUMENT,
   491  									},
   492  								},
   493  							},
   494  						},
   495  					},
   496  					Value: &Ydb.Value{
   497  						Value: &Ydb.Value_TextValue{
   498  							TextValue: `{"a": 1,"b": "B"}`,
   499  						},
   500  					},
   501  				},
   502  			},
   503  		},
   504  		{
   505  			name:    xtest.CurrentFileLine(),
   506  			builder: Builder{}.Param("$x").Optional().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).Build(),
   507  			params: map[string]*Ydb.TypedValue{
   508  				"$x": {
   509  					Type: &Ydb.Type{
   510  						Type: &Ydb.Type_OptionalType{
   511  							OptionalType: &Ydb.OptionalType{
   512  								Item: &Ydb.Type{
   513  									Type: &Ydb.Type_TypeId{
   514  										TypeId: Ydb.Type_YSON,
   515  									},
   516  								},
   517  							},
   518  						},
   519  					},
   520  					Value: &Ydb.Value{
   521  						Value: &Ydb.Value_BytesValue{
   522  							BytesValue: []byte(`[ 1; 2; 3; 4; 5 ]`),
   523  						},
   524  					},
   525  				},
   526  			},
   527  		},
   528  		{
   529  			name: xtest.CurrentFileLine(),
   530  			builder: Builder{}.Param("$x").
   531  				Optional().
   532  				UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).
   533  				Build(),
   534  			params: map[string]*Ydb.TypedValue{
   535  				"$x": {
   536  					Type: &Ydb.Type{
   537  						Type: &Ydb.Type_OptionalType{
   538  							OptionalType: &Ydb.OptionalType{
   539  								Item: &Ydb.Type{
   540  									Type: &Ydb.Type_TypeId{
   541  										TypeId: Ydb.Type_UUID,
   542  									},
   543  								},
   544  							},
   545  						},
   546  					},
   547  					Value: &Ydb.Value{
   548  						Value: &Ydb.Value_Low_128{
   549  							Low_128: 651345242494996240,
   550  						},
   551  						High_128: 72623859790382856,
   552  					},
   553  				},
   554  			},
   555  		},
   556  	} {
   557  		t.Run(tt.name, func(t *testing.T) {
   558  			a := allocator.New()
   559  			defer a.Free()
   560  			params := tt.builder.Build().ToYDB(a)
   561  			require.Equal(t, paramsToJSON(tt.params), paramsToJSON(params))
   562  		})
   563  	}
   564  }