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