github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/queries/query_test.go (about) 1 package queries 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestSetLimit(t *testing.T) { 9 t.Parallel() 10 11 q := &Query{} 12 SetLimit(q, 10) 13 14 expect := 10 15 if q.limit == nil { 16 t.Errorf("Expected %d, got nil", expect) 17 } else if *q.limit != expect { 18 t.Errorf("Expected %d, got %d", expect, *q.limit) 19 } 20 } 21 22 func TestSetOffset(t *testing.T) { 23 t.Parallel() 24 25 q := &Query{} 26 SetOffset(q, 10) 27 28 expect := 10 29 if q.offset != expect { 30 t.Errorf("Expected %d, got %d", expect, q.offset) 31 } 32 } 33 34 func TestSetSQL(t *testing.T) { 35 t.Parallel() 36 37 q := &Query{} 38 SetSQL(q, "select * from thing", 5, 3) 39 40 if len(q.rawSQL.args) != 2 { 41 t.Errorf("Expected len 2, got %d", len(q.rawSQL.args)) 42 } 43 44 if q.rawSQL.sql != "select * from thing" { 45 t.Errorf("Was not expected string, got %s", q.rawSQL.sql) 46 } 47 } 48 49 func TestSetLoad(t *testing.T) { 50 t.Parallel() 51 52 q := &Query{} 53 SetLoad(q, "one", "two") 54 55 if len(q.load) != 2 { 56 t.Errorf("Expected len 2, got %d", len(q.load)) 57 } 58 59 if q.load[0] != "one" || q.load[1] != "two" { 60 t.Errorf("Was not expected string, got %v", q.load) 61 } 62 } 63 64 type apple struct{} 65 66 func (apple) Apply(*Query) {} 67 68 func TestSetLoadMods(t *testing.T) { 69 t.Parallel() 70 71 q := &Query{} 72 SetLoadMods(q, "a", apple{}) 73 SetLoadMods(q, "b", apple{}) 74 75 if len(q.loadMods) != 2 { 76 t.Errorf("Expected len 2, got %d", len(q.loadMods)) 77 } 78 } 79 80 func TestAppendWhere(t *testing.T) { 81 t.Parallel() 82 83 q := &Query{} 84 expect := "x > $1 AND y > $2" 85 AppendWhere(q, expect, 5, 3) 86 AppendWhere(q, expect, 5, 3) 87 88 if len(q.where) != 2 { 89 t.Errorf("%#v", q.where) 90 } 91 92 if q.where[0].clause != expect || q.where[1].clause != expect { 93 t.Errorf("Expected %s, got %#v", expect, q.where) 94 } 95 96 if len(q.where[0].args) != 2 || len(q.where[0].args) != 2 { 97 t.Errorf("arg length wrong: %#v", q.where) 98 } 99 100 if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 { 101 t.Errorf("args wrong: %#v", q.where) 102 } 103 104 q.where = []where{{clause: expect, args: []interface{}{5, 3}}} 105 if q.where[0].clause != expect { 106 t.Errorf("Expected %s, got %v", expect, q.where) 107 } 108 109 if len(q.where[0].args) != 2 { 110 t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args)) 111 } 112 113 if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 { 114 t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args) 115 } 116 117 if len(q.where) != 1 { 118 t.Errorf("%#v", q.where) 119 } 120 } 121 122 func TestSetLastWhereAsOr(t *testing.T) { 123 t.Parallel() 124 q := &Query{} 125 126 AppendWhere(q, "") 127 128 if q.where[0].orSeparator { 129 t.Errorf("Do not want or separator") 130 } 131 132 SetLastWhereAsOr(q) 133 134 if len(q.where) != 1 { 135 t.Errorf("Want len 1") 136 } 137 if !q.where[0].orSeparator { 138 t.Errorf("Want or separator") 139 } 140 141 AppendWhere(q, "") 142 SetLastWhereAsOr(q) 143 144 if len(q.where) != 2 { 145 t.Errorf("Want len 2") 146 } 147 if q.where[0].orSeparator != true { 148 t.Errorf("Expected true") 149 } 150 if q.where[1].orSeparator != true { 151 t.Errorf("Expected true") 152 } 153 } 154 155 func TestAppendIn(t *testing.T) { 156 t.Parallel() 157 158 q := &Query{} 159 expect := "col IN ?" 160 AppendIn(q, expect, 5, 3) 161 AppendIn(q, expect, 5, 3) 162 163 if len(q.where) != 2 { 164 t.Errorf("%#v", q.where) 165 } 166 167 if q.where[0].clause != expect || q.where[1].clause != expect { 168 t.Errorf("Expected %s, got %#v", expect, q.where) 169 } 170 171 if len(q.where[0].args) != 2 || len(q.where[0].args) != 2 { 172 t.Errorf("arg length wrong: %#v", q.where) 173 } 174 175 if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 { 176 t.Errorf("args wrong: %#v", q.where) 177 } 178 179 q.where = []where{{clause: expect, args: []interface{}{5, 3}}} 180 if q.where[0].clause != expect { 181 t.Errorf("Expected %s, got %v", expect, q.where) 182 } 183 184 if len(q.where[0].args) != 2 { 185 t.Errorf("Expected %d args, got %d", 2, len(q.where[0].args)) 186 } 187 188 if q.where[0].args[0].(int) != 5 || q.where[0].args[1].(int) != 3 { 189 t.Errorf("Args not set correctly, expected 5 & 3, got: %#v", q.where[0].args) 190 } 191 192 if len(q.where) != 1 { 193 t.Errorf("%#v", q.where) 194 } 195 } 196 197 func TestSetLastInAsOr(t *testing.T) { 198 t.Parallel() 199 q := &Query{} 200 201 AppendIn(q, "") 202 203 if q.where[0].orSeparator { 204 t.Errorf("Do not want or separator") 205 } 206 207 SetLastInAsOr(q) 208 209 if len(q.where) != 1 { 210 t.Errorf("Want len 1") 211 } 212 if !q.where[0].orSeparator { 213 t.Errorf("Want or separator") 214 } 215 216 AppendIn(q, "") 217 SetLastInAsOr(q) 218 219 if len(q.where) != 2 { 220 t.Errorf("Want len 2") 221 } 222 if q.where[0].orSeparator != true { 223 t.Errorf("Expected true") 224 } 225 if q.where[1].orSeparator != true { 226 t.Errorf("Expected true") 227 } 228 } 229 230 func TestAppendGroupBy(t *testing.T) { 231 t.Parallel() 232 233 q := &Query{} 234 expect := "col1, col2" 235 AppendGroupBy(q, expect) 236 AppendGroupBy(q, expect) 237 238 if len(q.groupBy) != 2 && (q.groupBy[0] != expect || q.groupBy[1] != expect) { 239 t.Errorf("Expected %s, got %s %s", expect, q.groupBy[0], q.groupBy[1]) 240 } 241 242 q.groupBy = []string{expect} 243 if len(q.groupBy) != 1 && q.groupBy[0] != expect { 244 t.Errorf("Expected %s, got %s", expect, q.groupBy[0]) 245 } 246 } 247 248 func TestAppendOrderBy(t *testing.T) { 249 t.Parallel() 250 251 q := &Query{} 252 expect := "col1 desc, col2 asc" 253 AppendOrderBy(q, expect, 10) 254 AppendOrderBy(q, expect, 10) 255 256 if len(q.orderBy) != 2 && (q.orderBy[0].clause != expect || q.orderBy[1].clause != expect) { 257 t.Errorf("Expected %s, got %s %s", expect, q.orderBy[0], q.orderBy[1]) 258 } 259 260 if q.orderBy[0].args[0] != 10 || q.orderBy[1].args[0] != 10 { 261 t.Errorf("Expected %v, got %v %v", 10, q.orderBy[0].args[0], q.orderBy[1].args[0]) 262 } 263 264 q.orderBy = []argClause{ 265 {"col1 desc, col2 asc", []interface{}{}}, 266 } 267 if len(q.orderBy) != 1 && q.orderBy[0].clause != expect { 268 t.Errorf("Expected %s, got %s", expect, q.orderBy[0].clause) 269 } 270 } 271 272 func TestAppendHaving(t *testing.T) { 273 t.Parallel() 274 275 q := &Query{} 276 expect := "count(orders.order_id) > ?" 277 AppendHaving(q, expect, 10) 278 AppendHaving(q, expect, 10) 279 280 if len(q.having) != 2 { 281 t.Errorf("Expected 2, got %d", len(q.having)) 282 } 283 284 if q.having[0].clause != expect || q.having[1].clause != expect { 285 t.Errorf("Expected %s, got %s %s", expect, q.having[0].clause, q.having[1].clause) 286 } 287 288 if q.having[0].args[0] != 10 || q.having[1].args[0] != 10 { 289 t.Errorf("Expected %v, got %v %v", 10, q.having[0].args[0], q.having[1].args[0]) 290 } 291 292 q.having = []argClause{{clause: expect, args: []interface{}{10}}} 293 if len(q.having) != 1 && (q.having[0].clause != expect || q.having[0].args[0] != 10) { 294 t.Errorf("Expected %s, got %s %v", expect, q.having[0], q.having[0].args[0]) 295 } 296 } 297 298 func TestFrom(t *testing.T) { 299 t.Parallel() 300 301 q := &Query{} 302 AppendFrom(q, "videos a", "orders b") 303 AppendFrom(q, "videos a", "orders b") 304 305 expect := []string{"videos a", "orders b", "videos a", "orders b"} 306 if !reflect.DeepEqual(q.from, expect) { 307 t.Errorf("Expected %s, got %s", expect, q.from) 308 } 309 310 SetFrom(q, "videos a", "orders b") 311 if !reflect.DeepEqual(q.from, expect[:2]) { 312 t.Errorf("Expected %s, got %s", expect, q.from) 313 } 314 } 315 316 func TestSetSelect(t *testing.T) { 317 t.Parallel() 318 319 q := &Query{selectCols: []string{"hello"}} 320 SetSelect(q, nil) 321 322 if q.selectCols != nil { 323 t.Errorf("want nil") 324 } 325 } 326 327 func TestSetCount(t *testing.T) { 328 t.Parallel() 329 330 q := &Query{} 331 SetCount(q) 332 333 if q.count != true { 334 t.Errorf("got false") 335 } 336 } 337 338 func TestSetDistinct(t *testing.T) { 339 t.Parallel() 340 341 q := &Query{} 342 SetDistinct(q, "id") 343 344 if q.distinct != "id" { 345 t.Errorf("expected id, got %v", q.distinct) 346 } 347 } 348 349 func TestSetUpdate(t *testing.T) { 350 t.Parallel() 351 352 q := &Query{} 353 SetUpdate(q, map[string]interface{}{"test": 5}) 354 355 if q.update["test"] != 5 { 356 t.Errorf("Wrong update, got %v", q.update) 357 } 358 } 359 360 func TestSetDelete(t *testing.T) { 361 t.Parallel() 362 363 q := &Query{} 364 SetDelete(q) 365 366 if q.delete != true { 367 t.Errorf("Expected %t, got %t", true, q.delete) 368 } 369 } 370 371 func TestSetArgs(t *testing.T) { 372 t.Parallel() 373 374 args := []interface{}{2} 375 q := &Query{rawSQL: rawSQL{}} 376 SetArgs(q, args...) 377 378 if q.rawSQL.args[0].(int) != 2 { 379 t.Errorf("Expected args to get set") 380 } 381 } 382 383 func TestAppendSelect(t *testing.T) { 384 t.Parallel() 385 386 q := &Query{} 387 AppendSelect(q, "col1", "col2") 388 AppendSelect(q, "col1", "col2") 389 390 if len(q.selectCols) != 4 { 391 t.Errorf("Expected selectCols len 4, got %d", len(q.selectCols)) 392 } 393 394 if q.selectCols[0] != `col1` && q.selectCols[1] != `col2` { 395 t.Errorf("select cols value mismatch: %#v", q.selectCols) 396 } 397 if q.selectCols[2] != `col1` && q.selectCols[3] != `col2` { 398 t.Errorf("select cols value mismatch: %#v", q.selectCols) 399 } 400 401 q.selectCols = []string{"col1", "col2"} 402 if q.selectCols[0] != `col1` && q.selectCols[1] != `col2` { 403 t.Errorf("select cols value mismatch: %#v", q.selectCols) 404 } 405 } 406 407 func TestSQL(t *testing.T) { 408 t.Parallel() 409 410 q := Raw("thing", 5) 411 if q.rawSQL.sql != "thing" { 412 t.Errorf("Expected %q, got %s", "thing", q.rawSQL.sql) 413 } 414 if q.rawSQL.args[0].(int) != 5 { 415 t.Errorf("Expected 5, got %v", q.rawSQL.args[0]) 416 } 417 } 418 419 func TestSQLG(t *testing.T) { 420 t.Parallel() 421 422 q := RawG("thing", 5) 423 if q.rawSQL.sql != "thing" { 424 t.Errorf("Expected %q, got %s", "thing", q.rawSQL.sql) 425 } 426 if q.rawSQL.args[0].(int) != 5 { 427 t.Errorf("Expected 5, got %v", q.rawSQL.args[0]) 428 } 429 } 430 431 func TestAppendInnerJoin(t *testing.T) { 432 t.Parallel() 433 434 q := &Query{} 435 AppendInnerJoin(q, "thing=$1 AND stuff=$2", 2, 5) 436 AppendInnerJoin(q, "thing=$1 AND stuff=$2", 2, 5) 437 438 if len(q.joins) != 2 { 439 t.Errorf("Expected len 1, got %d", len(q.joins)) 440 } 441 442 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 443 t.Errorf("Got invalid innerJoin on string: %#v", q.joins) 444 } 445 if q.joins[1].clause != "thing=$1 AND stuff=$2" { 446 t.Errorf("Got invalid innerJoin on string: %#v", q.joins) 447 } 448 449 if len(q.joins[0].args) != 2 { 450 t.Errorf("Expected len 2, got %d", len(q.joins[0].args)) 451 } 452 if len(q.joins[1].args) != 2 { 453 t.Errorf("Expected len 2, got %d", len(q.joins[1].args)) 454 } 455 456 if q.joins[0].args[0] != 2 && q.joins[0].args[1] != 5 { 457 t.Errorf("Invalid args values, got %#v", q.joins[0].args) 458 } 459 460 q.joins = []join{{kind: JoinInner, 461 clause: "thing=$1 AND stuff=$2", 462 args: []interface{}{2, 5}, 463 }} 464 465 if len(q.joins) != 1 { 466 t.Errorf("Expected len 1, got %d", len(q.joins)) 467 } 468 469 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 470 t.Errorf("Got invalid innerJoin on string: %#v", q.joins) 471 } 472 } 473 474 func TestAppendLeftOuterJoin(t *testing.T) { 475 t.Parallel() 476 477 q := &Query{} 478 AppendLeftOuterJoin(q, "thing=$1 AND stuff=$2", 2, 5) 479 AppendLeftOuterJoin(q, "thing=$1 AND stuff=$2", 2, 5) 480 481 if len(q.joins) != 2 { 482 t.Errorf("Expected len 1, got %d", len(q.joins)) 483 } 484 485 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 486 t.Errorf("Got invalid leftJoin on string: %#v", q.joins) 487 } 488 if q.joins[1].clause != "thing=$1 AND stuff=$2" { 489 t.Errorf("Got invalid leftJoin on string: %#v", q.joins) 490 } 491 492 if len(q.joins[0].args) != 2 { 493 t.Errorf("Expected len 2, got %d", len(q.joins[0].args)) 494 } 495 if len(q.joins[1].args) != 2 { 496 t.Errorf("Expected len 2, got %d", len(q.joins[1].args)) 497 } 498 499 if q.joins[0].args[0] != 2 && q.joins[0].args[1] != 5 { 500 t.Errorf("Invalid args values, got %#v", q.joins[0].args) 501 } 502 503 q.joins = []join{{kind: JoinOuterLeft, 504 clause: "thing=$1 AND stuff=$2", 505 args: []interface{}{2, 5}, 506 }} 507 508 if len(q.joins) != 1 { 509 t.Errorf("Expected len 1, got %d", len(q.joins)) 510 } 511 512 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 513 t.Errorf("Got invalid leftJoin on string: %#v", q.joins) 514 } 515 } 516 517 func TestAppendRightOuterJoin(t *testing.T) { 518 t.Parallel() 519 520 q := &Query{} 521 AppendRightOuterJoin(q, "thing=$1 AND stuff=$2", 2, 5) 522 AppendRightOuterJoin(q, "thing=$1 AND stuff=$2", 2, 5) 523 524 if len(q.joins) != 2 { 525 t.Errorf("Expected len 1, got %d", len(q.joins)) 526 } 527 528 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 529 t.Errorf("Got invalid rightJoin on string: %#v", q.joins) 530 } 531 if q.joins[1].clause != "thing=$1 AND stuff=$2" { 532 t.Errorf("Got invalid rightJoin on string: %#v", q.joins) 533 } 534 535 if len(q.joins[0].args) != 2 { 536 t.Errorf("Expected len 2, got %d", len(q.joins[0].args)) 537 } 538 if len(q.joins[1].args) != 2 { 539 t.Errorf("Expected len 2, got %d", len(q.joins[1].args)) 540 } 541 542 if q.joins[0].args[0] != 2 && q.joins[0].args[1] != 5 { 543 t.Errorf("Invalid args values, got %#v", q.joins[0].args) 544 } 545 546 q.joins = []join{{kind: JoinOuterRight, 547 clause: "thing=$1 AND stuff=$2", 548 args: []interface{}{2, 5}, 549 }} 550 551 if len(q.joins) != 1 { 552 t.Errorf("Expected len 1, got %d", len(q.joins)) 553 } 554 555 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 556 t.Errorf("Got invalid rightJoin on string: %#v", q.joins) 557 } 558 } 559 560 func TestAppendFullOuterJoin(t *testing.T) { 561 t.Parallel() 562 563 q := &Query{} 564 AppendFullOuterJoin(q, "thing=$1 AND stuff=$2", 2, 5) 565 AppendFullOuterJoin(q, "thing=$1 AND stuff=$2", 2, 5) 566 567 if len(q.joins) != 2 { 568 t.Errorf("Expected len 1, got %d", len(q.joins)) 569 } 570 571 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 572 t.Errorf("Got invalid fullJoin on string: %#v", q.joins) 573 } 574 if q.joins[1].clause != "thing=$1 AND stuff=$2" { 575 t.Errorf("Got invalid fullJoin on string: %#v", q.joins) 576 } 577 578 if len(q.joins[0].args) != 2 { 579 t.Errorf("Expected len 2, got %d", len(q.joins[0].args)) 580 } 581 if len(q.joins[1].args) != 2 { 582 t.Errorf("Expected len 2, got %d", len(q.joins[1].args)) 583 } 584 585 if q.joins[0].args[0] != 2 && q.joins[0].args[1] != 5 { 586 t.Errorf("Invalid args values, got %#v", q.joins[0].args) 587 } 588 589 q.joins = []join{{kind: JoinOuterFull, 590 clause: "thing=$1 AND stuff=$2", 591 args: []interface{}{2, 5}, 592 }} 593 594 if len(q.joins) != 1 { 595 t.Errorf("Expected len 1, got %d", len(q.joins)) 596 } 597 598 if q.joins[0].clause != "thing=$1 AND stuff=$2" { 599 t.Errorf("Got invalid fullJoin on string: %#v", q.joins) 600 } 601 } 602 603 func TestAppendWith(t *testing.T) { 604 t.Parallel() 605 606 q := &Query{} 607 AppendWith(q, "cte_0 AS (SELECT * FROM table_0 WHERE thing=$1 AND stuff=$2)", 5, 10) 608 AppendWith(q, "cte_1 AS (SELECT * FROM table_1 WHERE thing=$1 AND stuff=$2)", 5, 10) 609 610 if len(q.withs) != 2 { 611 t.Errorf("Expected len 2, got %d", len(q.withs)) 612 } 613 614 if q.withs[0].clause != "cte_0 AS (SELECT * FROM table_0 WHERE thing=$1 AND stuff=$2)" { 615 t.Errorf("Got invalid with on string: %#v", q.withs) 616 } 617 if q.withs[1].clause != "cte_1 AS (SELECT * FROM table_1 WHERE thing=$1 AND stuff=$2)" { 618 t.Errorf("Got invalid with on string: %#v", q.withs) 619 } 620 621 if len(q.withs[0].args) != 2 { 622 t.Errorf("Expected len 2, got %d", len(q.withs[0].args)) 623 } 624 if len(q.withs[1].args) != 2 { 625 t.Errorf("Expected len 2, got %d", len(q.withs[1].args)) 626 } 627 628 if q.withs[0].args[0] != 5 && q.withs[0].args[1] != 10 { 629 t.Errorf("Invalid args values, got %#v", q.withs[0].args) 630 } 631 632 q.withs = []argClause{{ 633 clause: "other_cte AS (SELECT * FROM other_table WHERE thing=$1 AND stuff=$2)", 634 args: []interface{}{3, 7}, 635 }} 636 637 if len(q.withs) != 1 { 638 t.Errorf("Expected len 1, got %d", len(q.withs)) 639 } 640 641 if q.withs[0].clause != "other_cte AS (SELECT * FROM other_table WHERE thing=$1 AND stuff=$2)" { 642 t.Errorf("Got invalid with on string: %#v", q.withs) 643 } 644 } 645 646 func TestSetComment(t *testing.T) { 647 t.Parallel() 648 649 q := &Query{} 650 SetComment(q, "my comment") 651 652 if q.comment != "my comment" { 653 t.Errorf("Got invalid comment: %s", q.comment) 654 } 655 } 656 657 func TestRemoveSoftDeleteWhere(t *testing.T) { 658 t.Parallel() 659 660 q := &Query{} 661 AppendWhere(q, "a") 662 AppendWhere(q, "b") 663 AppendWhere(q, "deleted_at = false") 664 AppendWhere(q, `"hello"."deleted_at" is null`) 665 RemoveSoftDeleteWhere(q) 666 667 q.removeSoftDeleteWhere() 668 669 if len(q.where) != 3 { 670 t.Error("should have removed one entry:", len(q.where)) 671 } 672 673 if q.where[0].clause != "a" { 674 t.Error("a was moved") 675 } 676 if q.where[1].clause != "b" { 677 t.Error("b was moved") 678 } 679 if q.where[2].clause != "deleted_at = false" { 680 t.Error("trick deleted_at was not found") 681 } 682 if t.Failed() { 683 t.Logf("%#v\n", q.where) 684 } 685 686 q = &Query{} 687 AppendWhere(q, "a") 688 AppendWhere(q, "b") 689 AppendWhere(q, `"hello"."deleted_at" is null`) 690 AppendWhere(q, "deleted_at = false") 691 RemoveSoftDeleteWhere(q) 692 693 q.removeSoftDeleteWhere() 694 695 if len(q.where) != 3 { 696 t.Error("should have removed one entry:", len(q.where)) 697 } 698 699 if q.where[0].clause != "a" { 700 t.Error("a was moved") 701 } 702 if q.where[1].clause != "b" { 703 t.Error("b was moved") 704 } 705 if q.where[2].clause != "deleted_at = false" { 706 t.Error("trick deleted at did not replace the deleted_at is null entry") 707 } 708 if t.Failed() { 709 t.Logf("%#v\n", q.where) 710 } 711 }