github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/params/optional_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/xtest" 13 ) 14 15 func TestOptional(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{p(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{p(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{p(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{p(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{p(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{p(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{p(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{p(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{p(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{p("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{p([]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{p(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{p(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{p(time.Second)}, 225 226 expected: expected{ 227 Type: &Ydb.Type{ 228 Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INTERVAL}, 229 }, 230 231 Value: &Ydb.Value{ 232 Value: &Ydb.Value_Int64Value{ 233 Int64Value: 1000000, 234 }, 235 }, 236 }, 237 }, 238 { 239 method: "Datetime", 240 args: []any{p(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{p(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{p(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{p([...]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{p(`{"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{p(`{"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{p([]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{p(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{p([...]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{p(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{p(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{p(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").BeginOptional() 434 435 result, ok := xtest.CallMethod(item, tc.method, tc.args...)[0].(*optionalBuilder) 436 require.True(t, ok) 437 438 params := result.EndOptional().Build().ToYDB(a) 439 require.Equal(t, xtest.ToJSON( 440 map[string]*Ydb.TypedValue{ 441 "$x": { 442 Type: &Ydb.Type{ 443 Type: &Ydb.Type_OptionalType{ 444 OptionalType: &Ydb.OptionalType{ 445 Item: tc.expected.Type, 446 }, 447 }, 448 }, 449 Value: tc.expected.Value, 450 }, 451 }), xtest.ToJSON(params)) 452 }) 453 } 454 } 455 456 func p[T any](v T) *T { 457 return &v 458 }