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