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