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

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