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