1 package goqu_test
2
3 import (
4 "testing"
5
6 "github.com/DATA-DOG/go-sqlmock"
7 "github.com/doug-martin/goqu/v9"
8 "github.com/doug-martin/goqu/v9/exp"
9 "github.com/doug-martin/goqu/v9/internal/errors"
10 "github.com/doug-martin/goqu/v9/internal/sb"
11 "github.com/doug-martin/goqu/v9/mocks"
12 "github.com/stretchr/testify/mock"
13 "github.com/stretchr/testify/suite"
14 )
15
16 type (
17 selectTestCase struct {
18 ds *goqu.SelectDataset
19 clauses exp.SelectClauses
20 }
21 dsTestActionItem struct {
22 Address string `db:"address"`
23 Name string `db:"name"`
24 }
25 dsUntaggedTestActionItem struct {
26 Address string `db:"address"`
27 Name string `db:"name"`
28 Untagged string
29 }
30 selectDatasetSuite struct {
31 suite.Suite
32 }
33 )
34
35 func (sds *selectDatasetSuite) assertCases(cases ...selectTestCase) {
36 for _, s := range cases {
37 sds.Equal(s.clauses, s.ds.GetClauses())
38 }
39 }
40
41 func (sds *selectDatasetSuite) TestReturnsColumns() {
42 ds := goqu.Select(goqu.L("NOW()"))
43 sds.True(ds.ReturnsColumns())
44 }
45
46 func (sds *selectDatasetSuite) TestClone() {
47 ds := goqu.From("test")
48 sds.Equal(ds, ds.Clone())
49 }
50
51 func (sds *selectDatasetSuite) TestExpression() {
52 ds := goqu.From("test")
53 sds.Equal(ds, ds.Expression())
54 }
55
56 func (sds *selectDatasetSuite) TestDialect() {
57 ds := goqu.From("test")
58 sds.NotNil(ds.Dialect())
59 }
60
61 func (sds *selectDatasetSuite) TestWithDialect() {
62 ds := goqu.From("test")
63 md := new(mocks.SQLDialect)
64 ds = ds.SetDialect(md)
65
66 dialect := goqu.GetDialect("default")
67 dialectDs := ds.WithDialect("default")
68 sds.Equal(md, ds.Dialect())
69 sds.Equal(dialect, dialectDs.Dialect())
70 }
71
72 func (sds *selectDatasetSuite) TestPrepared() {
73 ds := goqu.From("test")
74 preparedDs := ds.Prepared(true)
75 sds.True(preparedDs.IsPrepared())
76 sds.False(ds.IsPrepared())
77
78 sds.True(preparedDs.Where(goqu.Ex{"a": 1}).IsPrepared())
79
80 defer goqu.SetDefaultPrepared(false)
81 goqu.SetDefaultPrepared(true)
82
83
84 ds = goqu.From("test")
85 sds.True(ds.IsPrepared())
86 }
87
88 func (sds *selectDatasetSuite) TestGetClauses() {
89 ds := goqu.From("test")
90 ce := exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression(goqu.I("test")))
91 sds.Equal(ce, ds.GetClauses())
92 }
93
94 func (sds *selectDatasetSuite) TestUpdate() {
95 where := goqu.Ex{"a": 1}
96 from := goqu.From("cte")
97 limit := uint(1)
98 order := []exp.OrderedExpression{goqu.C("a").Asc(), goqu.C("b").Desc()}
99 ds := goqu.From("test").
100 With("test-cte", from).
101 Where(where).
102 Limit(limit).
103 Order(order...)
104 ec := exp.NewUpdateClauses().
105 SetTable(goqu.C("test")).
106 CommonTablesAppend(exp.NewCommonTableExpression(false, "test-cte", from)).
107 WhereAppend(ds.GetClauses().Where()).
108 SetLimit(limit).
109 SetOrder(order...)
110 sds.Equal(ec, ds.Update().GetClauses())
111 }
112
113 func (sds *selectDatasetSuite) TestInsert() {
114 where := goqu.Ex{"a": 1}
115 from := goqu.From("cte")
116 limit := uint(1)
117 order := []exp.OrderedExpression{goqu.C("a").Asc(), goqu.C("b").Desc()}
118 ds := goqu.From("test").
119 With("test-cte", from).
120 Where(where).
121 Limit(limit).
122 Order(order...)
123 ec := exp.NewInsertClauses().
124 SetInto(goqu.C("test")).
125 CommonTablesAppend(exp.NewCommonTableExpression(false, "test-cte", from))
126 sds.Equal(ec, ds.Insert().GetClauses())
127 }
128
129 func (sds *selectDatasetSuite) TestDelete() {
130 where := goqu.Ex{"a": 1}
131 from := goqu.From("cte")
132 limit := uint(1)
133 order := []exp.OrderedExpression{goqu.C("a").Asc(), goqu.C("b").Desc()}
134 ds := goqu.From("test").
135 With("test-cte", from).
136 Where(where).
137 Limit(limit).
138 Order(order...)
139 ec := exp.NewDeleteClauses().
140 SetFrom(goqu.C("test")).
141 CommonTablesAppend(exp.NewCommonTableExpression(false, "test-cte", from)).
142 WhereAppend(ds.GetClauses().Where()).
143 SetLimit(limit).
144 SetOrder(order...)
145 sds.Equal(ec, ds.Delete().GetClauses())
146 }
147
148 func (sds *selectDatasetSuite) TestTruncate() {
149 where := goqu.Ex{"a": 1}
150 from := goqu.From("cte")
151 limit := uint(1)
152 order := []exp.OrderedExpression{goqu.C("a").Asc(), goqu.C("b").Desc()}
153 ds := goqu.From("test").
154 With("test-cte", from).
155 Where(where).
156 Limit(limit).
157 Order(order...)
158 ec := exp.NewTruncateClauses().
159 SetTable(exp.NewColumnListExpression("test"))
160 sds.Equal(ec, ds.Truncate().GetClauses())
161 }
162
163 func (sds *selectDatasetSuite) TestWith() {
164 from := goqu.From("cte")
165 bd := goqu.From("test")
166 sds.assertCases(
167 selectTestCase{
168 ds: bd.With("test-cte", from),
169 clauses: exp.NewSelectClauses().
170 SetFrom(exp.NewColumnListExpression("test")).
171 CommonTablesAppend(exp.NewCommonTableExpression(false, "test-cte", from)),
172 },
173 selectTestCase{
174 ds: bd,
175 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
176 },
177 )
178 }
179
180 func (sds *selectDatasetSuite) TestWithRecursive() {
181 from := goqu.From("cte")
182 bd := goqu.From("test")
183 sds.assertCases(
184 selectTestCase{
185 ds: bd.WithRecursive("test-cte", from),
186 clauses: exp.NewSelectClauses().
187 SetFrom(exp.NewColumnListExpression("test")).
188 CommonTablesAppend(exp.NewCommonTableExpression(true, "test-cte", from)),
189 },
190 selectTestCase{
191 ds: bd,
192 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
193 },
194 )
195 }
196
197 func (sds *selectDatasetSuite) TestSelect() {
198 bd := goqu.From("test")
199 sds.assertCases(
200 selectTestCase{
201 ds: bd.Select("a", "b"),
202 clauses: exp.NewSelectClauses().
203 SetFrom(exp.NewColumnListExpression("test")).
204 SetSelect(exp.NewColumnListExpression("a", "b")),
205 },
206 selectTestCase{
207 ds: bd.Select("a").Select("b"),
208 clauses: exp.NewSelectClauses().
209 SetFrom(exp.NewColumnListExpression("test")).
210 SetSelect(exp.NewColumnListExpression("b")),
211 },
212 selectTestCase{
213 ds: bd.Select("a").Select(),
214 clauses: exp.NewSelectClauses().
215 SetFrom(exp.NewColumnListExpression("test")),
216 },
217 selectTestCase{
218 ds: bd,
219 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
220 },
221 )
222 }
223
224 func (sds *selectDatasetSuite) TestSelectDistinct() {
225 bd := goqu.From("test")
226 sds.assertCases(
227 selectTestCase{
228 ds: bd.SelectDistinct("a", "b"),
229 clauses: exp.NewSelectClauses().
230 SetFrom(exp.NewColumnListExpression("test")).
231 SetSelect(exp.NewColumnListExpression("a", "b")).
232 SetDistinct(exp.NewColumnListExpression()),
233 },
234 selectTestCase{
235 ds: bd.SelectDistinct("a").SelectDistinct("b"),
236 clauses: exp.NewSelectClauses().
237 SetFrom(exp.NewColumnListExpression("test")).
238 SetSelect(exp.NewColumnListExpression("b")).
239 SetDistinct(exp.NewColumnListExpression()),
240 },
241 selectTestCase{
242 ds: bd.Select("a").SelectDistinct("b"),
243 clauses: exp.NewSelectClauses().
244 SetFrom(exp.NewColumnListExpression("test")).
245 SetSelect(exp.NewColumnListExpression("b")).
246 SetDistinct(exp.NewColumnListExpression()),
247 },
248 selectTestCase{
249 ds: bd.Select("a").SelectDistinct(),
250 clauses: exp.NewSelectClauses().
251 SetFrom(exp.NewColumnListExpression("test")).
252 SetSelect(exp.NewColumnListExpression(goqu.Star())).
253 SetDistinct(nil),
254 },
255 selectTestCase{
256 ds: bd.SelectDistinct("a").SelectDistinct(),
257 clauses: exp.NewSelectClauses().
258 SetFrom(exp.NewColumnListExpression("test")).
259 SetSelect(exp.NewColumnListExpression(goqu.Star())).
260 SetDistinct(nil),
261 },
262 selectTestCase{
263 ds: bd,
264 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
265 },
266 )
267 }
268
269 func (sds *selectDatasetSuite) TestClearSelect() {
270 bd := goqu.From("test").Select("a")
271 sds.assertCases(
272 selectTestCase{
273 ds: bd.ClearSelect(),
274 clauses: exp.NewSelectClauses().
275 SetFrom(exp.NewColumnListExpression("test")),
276 },
277 selectTestCase{
278 ds: bd,
279 clauses: exp.NewSelectClauses().
280 SetFrom(exp.NewColumnListExpression("test")).
281 SetSelect(exp.NewColumnListExpression("a")),
282 },
283 )
284 }
285
286 func (sds *selectDatasetSuite) TestSelectAppend() {
287 bd := goqu.From("test").Select("a")
288 sds.assertCases(
289 selectTestCase{
290 ds: bd.SelectAppend("b"),
291 clauses: exp.NewSelectClauses().
292 SetFrom(exp.NewColumnListExpression("test")).
293 SetSelect(exp.NewColumnListExpression("a", "b")),
294 },
295 selectTestCase{
296 ds: bd,
297 clauses: exp.NewSelectClauses().
298 SetFrom(exp.NewColumnListExpression("test")).
299 SetSelect(exp.NewColumnListExpression("a")),
300 },
301 )
302 }
303
304 func (sds *selectDatasetSuite) TestDistinct() {
305 bd := goqu.From("test")
306 sds.assertCases(
307 selectTestCase{
308 ds: bd.Distinct("a", "b"),
309 clauses: exp.NewSelectClauses().
310 SetFrom(exp.NewColumnListExpression("test")).
311 SetDistinct(exp.NewColumnListExpression("a", "b")),
312 },
313 selectTestCase{
314 ds: bd.Distinct("a").Distinct("b"),
315 clauses: exp.NewSelectClauses().
316 SetFrom(exp.NewColumnListExpression("test")).
317 SetDistinct(exp.NewColumnListExpression("b")),
318 },
319 selectTestCase{
320 ds: bd,
321 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
322 },
323 )
324 }
325
326 func (sds *selectDatasetSuite) TestFrom() {
327 bd := goqu.From("test")
328 sds.assertCases(
329 selectTestCase{
330 ds: bd.From(goqu.T("test2")),
331 clauses: exp.NewSelectClauses().
332 SetFrom(exp.NewColumnListExpression(goqu.T("test2"))),
333 },
334 selectTestCase{
335 ds: bd.From(goqu.From("test")),
336 clauses: exp.NewSelectClauses().
337 SetFrom(exp.NewColumnListExpression(goqu.From("test").As("t1"))),
338 },
339 selectTestCase{
340 ds: bd,
341 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
342 },
343 )
344 }
345
346 func (sds *selectDatasetSuite) TestFromSelf() {
347 bd := goqu.From("test")
348 sds.assertCases(
349 selectTestCase{
350 ds: bd.FromSelf(),
351 clauses: exp.NewSelectClauses().
352 SetFrom(exp.NewColumnListExpression(bd.As("t1"))),
353 },
354 selectTestCase{
355 ds: bd.As("alias").FromSelf(),
356 clauses: exp.NewSelectClauses().
357 SetFrom(exp.NewColumnListExpression(bd.As("alias"))),
358 },
359 selectTestCase{
360 ds: bd,
361 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
362 },
363 )
364 }
365
366 func (sds *selectDatasetSuite) TestCompoundFromSelf() {
367 bd := goqu.From("test")
368 sds.assertCases(
369 selectTestCase{
370 ds: bd.CompoundFromSelf(),
371 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
372 },
373 selectTestCase{
374 ds: bd.Limit(10).CompoundFromSelf(),
375 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression(bd.Limit(10).As("t1"))),
376 },
377 selectTestCase{
378 ds: bd.Order(goqu.C("a").Asc()).CompoundFromSelf(),
379 clauses: exp.NewSelectClauses().
380 SetFrom(exp.NewColumnListExpression(bd.Order(goqu.C("a").Asc()).As("t1"))),
381 },
382 selectTestCase{
383 ds: bd.As("alias").FromSelf(),
384 clauses: exp.NewSelectClauses().
385 SetFrom(exp.NewColumnListExpression(bd.As("alias"))),
386 },
387 selectTestCase{
388 ds: bd,
389 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
390 },
391 )
392 }
393
394 func (sds *selectDatasetSuite) TestJoin() {
395 bd := goqu.From("test")
396 sds.assertCases(
397 selectTestCase{
398 ds: bd.Join(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
399 clauses: exp.NewSelectClauses().
400 SetFrom(exp.NewColumnListExpression("test")).
401 JoinsAppend(
402 exp.NewConditionedJoinExpression(exp.InnerJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
403 ),
404 },
405 selectTestCase{
406 ds: bd,
407 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
408 },
409 )
410 }
411
412 func (sds *selectDatasetSuite) TestInnerJoin() {
413 bd := goqu.From("test")
414 sds.assertCases(
415 selectTestCase{
416 ds: bd.InnerJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
417 clauses: exp.NewSelectClauses().
418 SetFrom(exp.NewColumnListExpression("test")).
419 JoinsAppend(
420 exp.NewConditionedJoinExpression(exp.InnerJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
421 ),
422 },
423 selectTestCase{
424 ds: bd,
425 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
426 },
427 )
428 }
429
430 func (sds *selectDatasetSuite) TestFullOuterJoin() {
431 bd := goqu.From("test")
432 sds.assertCases(
433 selectTestCase{
434 ds: bd.FullOuterJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
435 clauses: exp.NewSelectClauses().
436 SetFrom(exp.NewColumnListExpression("test")).
437 JoinsAppend(
438 exp.NewConditionedJoinExpression(exp.FullOuterJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
439 ),
440 },
441 selectTestCase{
442 ds: bd,
443 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
444 },
445 )
446 }
447
448 func (sds *selectDatasetSuite) TestRightOuterJoin() {
449 bd := goqu.From("test")
450 sds.assertCases(
451 selectTestCase{
452 ds: bd.RightOuterJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
453 clauses: exp.NewSelectClauses().
454 SetFrom(exp.NewColumnListExpression("test")).
455 JoinsAppend(
456 exp.NewConditionedJoinExpression(exp.RightOuterJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
457 ),
458 },
459 selectTestCase{
460 ds: bd,
461 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
462 },
463 )
464 }
465
466 func (sds *selectDatasetSuite) TestLeftOuterJoin() {
467 bd := goqu.From("test")
468 sds.assertCases(
469 selectTestCase{
470 ds: bd.LeftOuterJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
471 clauses: exp.NewSelectClauses().
472 SetFrom(exp.NewColumnListExpression("test")).
473 JoinsAppend(
474 exp.NewConditionedJoinExpression(exp.LeftOuterJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
475 ),
476 },
477 selectTestCase{
478 ds: bd,
479 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
480 },
481 )
482 }
483
484 func (sds *selectDatasetSuite) TestFullJoin() {
485 bd := goqu.From("test")
486 sds.assertCases(
487 selectTestCase{
488 ds: bd.FullJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
489 clauses: exp.NewSelectClauses().
490 SetFrom(exp.NewColumnListExpression("test")).
491 JoinsAppend(
492 exp.NewConditionedJoinExpression(exp.FullJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
493 ),
494 },
495 selectTestCase{
496 ds: bd,
497 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
498 },
499 )
500 }
501
502 func (sds *selectDatasetSuite) TestRightJoin() {
503 bd := goqu.From("test")
504 sds.assertCases(
505 selectTestCase{
506 ds: bd.RightJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
507 clauses: exp.NewSelectClauses().
508 SetFrom(exp.NewColumnListExpression("test")).
509 JoinsAppend(
510 exp.NewConditionedJoinExpression(exp.RightJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
511 ),
512 },
513 selectTestCase{
514 ds: bd,
515 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
516 },
517 )
518 }
519
520 func (sds *selectDatasetSuite) TestLeftJoin() {
521 bd := goqu.From("test")
522 sds.assertCases(
523 selectTestCase{
524 ds: bd.LeftJoin(goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
525 clauses: exp.NewSelectClauses().
526 SetFrom(exp.NewColumnListExpression("test")).
527 JoinsAppend(
528 exp.NewConditionedJoinExpression(exp.LeftJoinType, goqu.T("foo"), goqu.On(goqu.C("a").IsNull())),
529 ),
530 },
531 selectTestCase{
532 ds: bd,
533 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
534 },
535 )
536 }
537
538 func (sds *selectDatasetSuite) TestNaturalJoin() {
539 bd := goqu.From("test")
540 sds.assertCases(
541 selectTestCase{
542 ds: bd.NaturalJoin(goqu.T("foo")),
543 clauses: exp.NewSelectClauses().
544 SetFrom(exp.NewColumnListExpression("test")).
545 JoinsAppend(
546 exp.NewUnConditionedJoinExpression(exp.NaturalJoinType, goqu.T("foo")),
547 ),
548 },
549 selectTestCase{
550 ds: bd,
551 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
552 },
553 )
554 }
555
556 func (sds *selectDatasetSuite) TestNaturalLeftJoin() {
557 bd := goqu.From("test")
558 sds.assertCases(
559 selectTestCase{
560 ds: bd.NaturalLeftJoin(goqu.T("foo")),
561 clauses: exp.NewSelectClauses().
562 SetFrom(exp.NewColumnListExpression("test")).
563 JoinsAppend(
564 exp.NewUnConditionedJoinExpression(exp.NaturalLeftJoinType, goqu.T("foo")),
565 ),
566 },
567 selectTestCase{
568 ds: bd,
569 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
570 },
571 )
572 }
573
574 func (sds *selectDatasetSuite) TestNaturalRightJoin() {
575 bd := goqu.From("test")
576 sds.assertCases(
577 selectTestCase{
578 ds: bd.NaturalRightJoin(goqu.T("foo")),
579 clauses: exp.NewSelectClauses().
580 SetFrom(exp.NewColumnListExpression("test")).
581 JoinsAppend(
582 exp.NewUnConditionedJoinExpression(exp.NaturalRightJoinType, goqu.T("foo")),
583 ),
584 },
585 selectTestCase{
586 ds: bd,
587 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
588 },
589 )
590 }
591
592 func (sds *selectDatasetSuite) TestNaturalFullJoin() {
593 bd := goqu.From("test")
594 sds.assertCases(
595 selectTestCase{
596 ds: bd.NaturalFullJoin(goqu.T("foo")),
597 clauses: exp.NewSelectClauses().
598 SetFrom(exp.NewColumnListExpression("test")).
599 JoinsAppend(
600 exp.NewUnConditionedJoinExpression(exp.NaturalFullJoinType, goqu.T("foo")),
601 ),
602 },
603 selectTestCase{
604 ds: bd,
605 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
606 },
607 )
608 }
609
610 func (sds *selectDatasetSuite) TestCrossJoin() {
611 bd := goqu.From("test")
612 sds.assertCases(
613 selectTestCase{
614 ds: bd.CrossJoin(goqu.T("foo")),
615 clauses: exp.NewSelectClauses().
616 SetFrom(exp.NewColumnListExpression("test")).
617 JoinsAppend(
618 exp.NewUnConditionedJoinExpression(exp.CrossJoinType, goqu.T("foo")),
619 ),
620 },
621 selectTestCase{
622 ds: bd,
623 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
624 },
625 )
626 }
627
628 func (sds *selectDatasetSuite) TestWhere() {
629 w := goqu.Ex{"a": 1}
630 w2 := goqu.Ex{"b": "c"}
631 bd := goqu.From("test")
632 sds.assertCases(
633 selectTestCase{
634 ds: bd.Where(w),
635 clauses: exp.NewSelectClauses().
636 SetFrom(exp.NewColumnListExpression("test")).
637 WhereAppend(w),
638 },
639 selectTestCase{
640 ds: bd.Where(w).Where(w2),
641 clauses: exp.NewSelectClauses().
642 SetFrom(exp.NewColumnListExpression("test")).
643 WhereAppend(w).WhereAppend(w2),
644 },
645 selectTestCase{
646 ds: bd,
647 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
648 },
649 )
650 }
651
652 func (sds *selectDatasetSuite) TestClearWhere() {
653 w := goqu.Ex{"a": 1}
654 bd := goqu.From("test").Where(w)
655 sds.assertCases(
656 selectTestCase{
657 ds: bd.ClearWhere(),
658 clauses: exp.NewSelectClauses().
659 SetFrom(exp.NewColumnListExpression("test")),
660 },
661 selectTestCase{
662 ds: bd,
663 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).WhereAppend(w),
664 },
665 )
666 }
667
668 func (sds *selectDatasetSuite) TestForUpdate() {
669 bd := goqu.From("test")
670 sds.assertCases(
671 selectTestCase{
672 ds: bd.ForUpdate(goqu.NoWait),
673 clauses: exp.NewSelectClauses().
674 SetFrom(exp.NewColumnListExpression("test")).
675 SetLock(exp.NewLock(exp.ForUpdate, goqu.NoWait)),
676 },
677 selectTestCase{
678 ds: bd.ForUpdate(goqu.NoWait, goqu.T("table1")),
679 clauses: exp.NewSelectClauses().
680 SetFrom(exp.NewColumnListExpression("test")).
681 SetLock(exp.NewLock(exp.ForUpdate, goqu.NoWait, goqu.T("table1"))),
682 },
683 selectTestCase{
684 ds: bd.ForUpdate(goqu.NoWait, goqu.T("table1"), goqu.T("table2")),
685 clauses: exp.NewSelectClauses().
686 SetFrom(exp.NewColumnListExpression("test")).
687 SetLock(exp.NewLock(exp.ForUpdate, goqu.NoWait, goqu.T("table1"), goqu.T("table2"))),
688 },
689 selectTestCase{
690 ds: bd,
691 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
692 },
693 )
694 }
695
696 func (sds *selectDatasetSuite) TestForNoKeyUpdate() {
697 bd := goqu.From("test")
698 sds.assertCases(
699 selectTestCase{
700 ds: bd.ForNoKeyUpdate(goqu.NoWait),
701 clauses: exp.NewSelectClauses().
702 SetFrom(exp.NewColumnListExpression("test")).
703 SetLock(exp.NewLock(exp.ForNoKeyUpdate, goqu.NoWait)),
704 },
705 selectTestCase{
706 ds: bd.ForNoKeyUpdate(goqu.NoWait, goqu.T("table1")),
707 clauses: exp.NewSelectClauses().
708 SetFrom(exp.NewColumnListExpression("test")).
709 SetLock(exp.NewLock(exp.ForNoKeyUpdate, goqu.NoWait, goqu.T("table1"))),
710 },
711 selectTestCase{
712 ds: bd.ForNoKeyUpdate(goqu.NoWait, goqu.T("table1"), goqu.T("table2")),
713 clauses: exp.NewSelectClauses().
714 SetFrom(exp.NewColumnListExpression("test")).
715 SetLock(exp.NewLock(exp.ForNoKeyUpdate, goqu.NoWait, goqu.T("table1"), goqu.T("table2"))),
716 },
717 selectTestCase{
718 ds: bd,
719 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
720 },
721 )
722 }
723
724 func (sds *selectDatasetSuite) TestForKeyShare() {
725 bd := goqu.From("test")
726 sds.assertCases(
727 selectTestCase{
728 ds: bd.ForKeyShare(goqu.NoWait),
729 clauses: exp.NewSelectClauses().
730 SetFrom(exp.NewColumnListExpression("test")).
731 SetLock(exp.NewLock(exp.ForKeyShare, goqu.NoWait)),
732 },
733 selectTestCase{
734 ds: bd.ForKeyShare(goqu.NoWait, goqu.T("table1")),
735 clauses: exp.NewSelectClauses().
736 SetFrom(exp.NewColumnListExpression("test")).
737 SetLock(exp.NewLock(exp.ForKeyShare, goqu.NoWait, goqu.T("table1"))),
738 },
739 selectTestCase{
740 ds: bd.ForKeyShare(goqu.NoWait, goqu.T("table1"), goqu.T("table2")),
741 clauses: exp.NewSelectClauses().
742 SetFrom(exp.NewColumnListExpression("test")).
743 SetLock(exp.NewLock(exp.ForKeyShare, goqu.NoWait, goqu.T("table1"), goqu.T("table2"))),
744 },
745 selectTestCase{
746 ds: bd,
747 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
748 },
749 )
750 }
751
752 func (sds *selectDatasetSuite) TestForShare() {
753 bd := goqu.From("test")
754 sds.assertCases(
755 selectTestCase{
756 ds: bd.ForShare(goqu.NoWait),
757 clauses: exp.NewSelectClauses().
758 SetFrom(exp.NewColumnListExpression("test")).
759 SetLock(exp.NewLock(exp.ForShare, goqu.NoWait)),
760 },
761 selectTestCase{
762 ds: bd.ForShare(goqu.NoWait, goqu.T("table1")),
763 clauses: exp.NewSelectClauses().
764 SetFrom(exp.NewColumnListExpression("test")).
765 SetLock(exp.NewLock(exp.ForShare, goqu.NoWait, goqu.T("table1"))),
766 },
767 selectTestCase{
768 ds: bd.ForShare(goqu.NoWait, goqu.T("table1"), goqu.T("table2")),
769 clauses: exp.NewSelectClauses().
770 SetFrom(exp.NewColumnListExpression("test")).
771 SetLock(exp.NewLock(exp.ForShare, goqu.NoWait, goqu.T("table1"), goqu.T("table2"))),
772 },
773 selectTestCase{
774 ds: bd,
775 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
776 },
777 )
778 }
779
780 func (sds *selectDatasetSuite) TestGroupBy() {
781 bd := goqu.From("test")
782 sds.assertCases(
783 selectTestCase{
784 ds: bd.GroupBy("a"),
785 clauses: exp.NewSelectClauses().
786 SetFrom(exp.NewColumnListExpression("test")).
787 SetGroupBy(exp.NewColumnListExpression("a")),
788 },
789 selectTestCase{
790 ds: bd.GroupBy("a").GroupBy("b"),
791 clauses: exp.NewSelectClauses().
792 SetFrom(exp.NewColumnListExpression("test")).
793 SetGroupBy(exp.NewColumnListExpression("b")),
794 },
795 selectTestCase{
796 ds: bd,
797 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
798 },
799 )
800 }
801
802 func (sds *selectDatasetSuite) TestWindow() {
803 w1 := goqu.W("w1").PartitionBy("a").OrderBy("b")
804 w2 := goqu.W("w2").PartitionBy("a").OrderBy("b")
805
806 bd := goqu.From("test")
807 sds.assertCases(
808 selectTestCase{
809 ds: bd.Window(w1),
810 clauses: exp.NewSelectClauses().
811 SetFrom(exp.NewColumnListExpression("test")).
812 WindowsAppend(w1),
813 },
814 selectTestCase{
815 ds: bd.Window(w1).Window(w2),
816 clauses: exp.NewSelectClauses().
817 SetFrom(exp.NewColumnListExpression("test")).
818 WindowsAppend(w2),
819 },
820 selectTestCase{
821 ds: bd.Window(w1, w2),
822 clauses: exp.NewSelectClauses().
823 SetFrom(exp.NewColumnListExpression("test")).
824 WindowsAppend(w1, w2),
825 },
826 selectTestCase{
827 ds: bd,
828 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
829 },
830 )
831 }
832
833 func (sds *selectDatasetSuite) TestWindowAppend() {
834 w1 := goqu.W("w1").PartitionBy("a").OrderBy("b")
835 w2 := goqu.W("w2").PartitionBy("a").OrderBy("b")
836
837 bd := goqu.From("test").Window(w1)
838 sds.assertCases(
839 selectTestCase{
840 ds: bd.WindowAppend(w2),
841 clauses: exp.NewSelectClauses().
842 SetFrom(exp.NewColumnListExpression("test")).
843 WindowsAppend(w1, w2),
844 },
845 selectTestCase{
846 ds: bd,
847 clauses: exp.NewSelectClauses().
848 SetFrom(exp.NewColumnListExpression("test")).
849 WindowsAppend(w1),
850 },
851 )
852 }
853
854 func (sds *selectDatasetSuite) TestClearWindow() {
855 w1 := goqu.W("w1").PartitionBy("a").OrderBy("b")
856
857 bd := goqu.From("test").Window(w1)
858 sds.assertCases(
859 selectTestCase{
860 ds: bd.ClearWindow(),
861 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
862 },
863 selectTestCase{
864 ds: bd,
865 clauses: exp.NewSelectClauses().
866 SetFrom(exp.NewColumnListExpression("test")).
867 WindowsAppend(w1),
868 },
869 )
870 }
871
872 func (sds *selectDatasetSuite) TestHaving() {
873 bd := goqu.From("test")
874 sds.assertCases(
875 selectTestCase{
876 ds: bd.Having(goqu.C("a").Gt(1)),
877 clauses: exp.NewSelectClauses().
878 SetFrom(exp.NewColumnListExpression("test")).
879 HavingAppend(goqu.C("a").Gt(1)),
880 },
881 selectTestCase{
882 ds: bd.Having(goqu.C("a").Gt(1)).Having(goqu.Ex{"b": "c"}),
883 clauses: exp.NewSelectClauses().
884 SetFrom(exp.NewColumnListExpression("test")).
885 HavingAppend(goqu.C("a").Gt(1)).HavingAppend(goqu.Ex{"b": "c"}),
886 },
887 selectTestCase{
888 ds: bd,
889 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
890 },
891 )
892 }
893
894 func (sds *selectDatasetSuite) TestOrder() {
895 bd := goqu.From("test")
896 sds.assertCases(
897 selectTestCase{
898 ds: bd.Order(goqu.C("a").Asc()),
899 clauses: exp.NewSelectClauses().
900 SetFrom(exp.NewColumnListExpression("test")).
901 SetOrder(goqu.C("a").Asc()),
902 },
903 selectTestCase{
904 ds: bd.Order(goqu.C("a").Asc()).Order(goqu.C("b").Asc()),
905 clauses: exp.NewSelectClauses().
906 SetFrom(exp.NewColumnListExpression("test")).
907 SetOrder(goqu.C("b").Asc()),
908 },
909 selectTestCase{
910 ds: bd,
911 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
912 },
913 )
914 }
915
916 func (sds *selectDatasetSuite) TestOrderAppend() {
917 bd := goqu.From("test").Order(goqu.C("a").Asc())
918 sds.assertCases(
919 selectTestCase{
920 ds: bd.OrderAppend(goqu.C("b").Asc()),
921 clauses: exp.NewSelectClauses().
922 SetFrom(exp.NewColumnListExpression("test")).
923 SetOrder(goqu.C("a").Asc(), goqu.C("b").Asc()),
924 },
925 selectTestCase{
926 ds: bd,
927 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
928 SetOrder(goqu.C("a").Asc()),
929 },
930 )
931 }
932
933 func (sds *selectDatasetSuite) TestOrderPrepend() {
934 bd := goqu.From("test").Order(goqu.C("a").Asc())
935 sds.assertCases(
936 selectTestCase{
937 ds: bd.OrderPrepend(goqu.C("b").Asc()),
938 clauses: exp.NewSelectClauses().
939 SetFrom(exp.NewColumnListExpression("test")).
940 SetOrder(goqu.C("b").Asc(), goqu.C("a").Asc()),
941 },
942 selectTestCase{
943 ds: bd,
944 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
945 SetOrder(goqu.C("a").Asc()),
946 },
947 )
948 }
949
950 func (sds *selectDatasetSuite) TestClearOrder() {
951 bd := goqu.From("test").Order(goqu.C("a").Asc())
952 sds.assertCases(
953 selectTestCase{
954 ds: bd.ClearOrder(),
955 clauses: exp.NewSelectClauses().
956 SetFrom(exp.NewColumnListExpression("test")),
957 },
958 selectTestCase{
959 ds: bd,
960 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
961 SetOrder(goqu.C("a").Asc()),
962 },
963 )
964 }
965
966 func (sds *selectDatasetSuite) TestLimit() {
967 bd := goqu.From("test")
968 sds.assertCases(
969 selectTestCase{
970 ds: bd.Limit(10),
971 clauses: exp.NewSelectClauses().
972 SetFrom(exp.NewColumnListExpression("test")).
973 SetLimit(uint(10)),
974 },
975 selectTestCase{
976 ds: bd.Limit(0),
977 clauses: exp.NewSelectClauses().
978 SetFrom(exp.NewColumnListExpression("test")),
979 },
980 selectTestCase{
981 ds: bd.Limit(10).Limit(2),
982 clauses: exp.NewSelectClauses().
983 SetFrom(exp.NewColumnListExpression("test")).
984 SetLimit(uint(2)),
985 },
986 selectTestCase{
987 ds: bd.Limit(10).Limit(0),
988 clauses: exp.NewSelectClauses().
989 SetFrom(exp.NewColumnListExpression("test")),
990 },
991 selectTestCase{
992 ds: bd,
993 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
994 },
995 )
996 }
997
998 func (sds *selectDatasetSuite) TestLimitAll() {
999 bd := goqu.From("test")
1000 sds.assertCases(
1001 selectTestCase{
1002 ds: bd.LimitAll(),
1003 clauses: exp.NewSelectClauses().
1004 SetFrom(exp.NewColumnListExpression("test")).
1005 SetLimit(goqu.L("ALL")),
1006 },
1007 selectTestCase{
1008 ds: bd.Limit(10).LimitAll(),
1009 clauses: exp.NewSelectClauses().
1010 SetFrom(exp.NewColumnListExpression("test")).
1011 SetLimit(goqu.L("ALL")),
1012 },
1013 selectTestCase{
1014 ds: bd,
1015 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1016 },
1017 )
1018 }
1019
1020 func (sds *selectDatasetSuite) TestClearLimit() {
1021 bd := goqu.From("test").Limit(10)
1022 sds.assertCases(
1023 selectTestCase{
1024 ds: bd.ClearLimit(),
1025 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1026 },
1027 selectTestCase{
1028 ds: bd,
1029 clauses: exp.NewSelectClauses().
1030 SetFrom(exp.NewColumnListExpression("test")).
1031 SetLimit(uint(10)),
1032 },
1033 )
1034 }
1035
1036 func (sds *selectDatasetSuite) TestOffset() {
1037 bd := goqu.From("test")
1038 sds.assertCases(
1039 selectTestCase{
1040 ds: bd.Offset(10),
1041 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).SetOffset(10),
1042 },
1043 selectTestCase{
1044 ds: bd,
1045 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1046 },
1047 )
1048 }
1049
1050 func (sds *selectDatasetSuite) TestClearOffset() {
1051 bd := goqu.From("test").Offset(10)
1052 sds.assertCases(
1053 selectTestCase{
1054 ds: bd.ClearOffset(),
1055 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1056 },
1057 selectTestCase{
1058 ds: bd,
1059 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).SetOffset(10),
1060 },
1061 )
1062 }
1063
1064 func (sds *selectDatasetSuite) TestUnion() {
1065 uds := goqu.From("union_test")
1066 bd := goqu.From("test")
1067 sds.assertCases(
1068 selectTestCase{
1069 ds: bd.Union(uds),
1070 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
1071 CompoundsAppend(exp.NewCompoundExpression(exp.UnionCompoundType, uds)),
1072 },
1073 selectTestCase{
1074 ds: bd,
1075 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1076 },
1077 )
1078 }
1079
1080 func (sds *selectDatasetSuite) TestUnionAll() {
1081 uds := goqu.From("union_test")
1082 bd := goqu.From("test")
1083 sds.assertCases(
1084 selectTestCase{
1085 ds: bd.UnionAll(uds),
1086 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
1087 CompoundsAppend(exp.NewCompoundExpression(exp.UnionAllCompoundType, uds)),
1088 },
1089 selectTestCase{
1090 ds: bd,
1091 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1092 },
1093 )
1094 }
1095
1096 func (sds *selectDatasetSuite) TestIntersect() {
1097 uds := goqu.From("union_test")
1098 bd := goqu.From("test")
1099 sds.assertCases(
1100 selectTestCase{
1101 ds: bd.Intersect(uds),
1102 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
1103 CompoundsAppend(exp.NewCompoundExpression(exp.IntersectCompoundType, uds)),
1104 },
1105 selectTestCase{
1106 ds: bd,
1107 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1108 },
1109 )
1110 }
1111
1112 func (sds *selectDatasetSuite) TestIntersectAll() {
1113 uds := goqu.From("union_test")
1114 bd := goqu.From("test")
1115 sds.assertCases(
1116 selectTestCase{
1117 ds: bd.IntersectAll(uds),
1118 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
1119 CompoundsAppend(exp.NewCompoundExpression(exp.IntersectAllCompoundType, uds)),
1120 },
1121 selectTestCase{
1122 ds: bd,
1123 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1124 },
1125 )
1126 }
1127
1128 func (sds *selectDatasetSuite) TestAs() {
1129 bd := goqu.From("test")
1130 sds.assertCases(
1131 selectTestCase{
1132 ds: bd.As("t"),
1133 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")).
1134 SetAlias(goqu.T("t")),
1135 },
1136 selectTestCase{
1137 ds: bd,
1138 clauses: exp.NewSelectClauses().SetFrom(exp.NewColumnListExpression("test")),
1139 },
1140 )
1141 }
1142
1143 func (sds *selectDatasetSuite) TestToSQL() {
1144 md := new(mocks.SQLDialect)
1145 ds := goqu.From("test").SetDialect(md)
1146 c := ds.GetClauses()
1147 sqlB := sb.NewSQLBuilder(false)
1148 md.On("ToSelectSQL", sqlB, c).Return(nil).Once()
1149 sql, args, err := ds.ToSQL()
1150 sds.Empty(sql)
1151 sds.Empty(args)
1152 sds.Nil(err)
1153 md.AssertExpectations(sds.T())
1154 }
1155
1156 func (sds *selectDatasetSuite) TestToSQL_prepared() {
1157 md := new(mocks.SQLDialect)
1158 ds := goqu.From("test").Prepared(true).SetDialect(md)
1159 c := ds.GetClauses()
1160 sqlB := sb.NewSQLBuilder(true)
1161 md.On("ToSelectSQL", sqlB, c).Return(nil).Once()
1162 sql, args, err := ds.ToSQL()
1163 sds.Empty(sql)
1164 sds.Empty(args)
1165 sds.Nil(err)
1166 md.AssertExpectations(sds.T())
1167 }
1168
1169 func (sds *selectDatasetSuite) TestToSQL_ReturnedError() {
1170 md := new(mocks.SQLDialect)
1171 ds := goqu.From("test").SetDialect(md)
1172 c := ds.GetClauses()
1173 sqlB := sb.NewSQLBuilder(false)
1174 ee := errors.New("expected error")
1175 md.On("ToSelectSQL", sqlB, c).Run(func(args mock.Arguments) {
1176 args.Get(0).(sb.SQLBuilder).SetError(ee)
1177 }).Once()
1178
1179 sql, args, err := ds.ToSQL()
1180 sds.Empty(sql)
1181 sds.Empty(args)
1182 sds.Equal(ee, err)
1183 md.AssertExpectations(sds.T())
1184 }
1185
1186 func (sds *selectDatasetSuite) TestAppendSQL() {
1187 md := new(mocks.SQLDialect)
1188 ds := goqu.From("test").SetDialect(md)
1189 c := ds.GetClauses()
1190 sqlB := sb.NewSQLBuilder(false)
1191 md.On("ToSelectSQL", sqlB, c).Return(nil).Once()
1192 ds.AppendSQL(sqlB)
1193 sds.NoError(sqlB.Error())
1194 md.AssertExpectations(sds.T())
1195 }
1196
1197 func (sds *selectDatasetSuite) TestScanStructs() {
1198 mDB, sqlMock, err := sqlmock.New()
1199 sds.NoError(err)
1200 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items"`).
1201 WithArgs().
1202 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).
1203 FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))
1204
1205 sqlMock.ExpectQuery(`SELECT DISTINCT "name" FROM "items"`).
1206 WithArgs().
1207 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).
1208 FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))
1209 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items"`).
1210 WithArgs().
1211 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1212 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items"`).
1213 WithArgs().
1214 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1215 sqlMock.ExpectQuery(`SELECT "test" FROM "items"`).
1216 WithArgs().
1217 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1218
1219 db := goqu.New("mock", mDB)
1220 var items []dsTestActionItem
1221 sds.NoError(db.From("items").ScanStructs(&items))
1222 sds.Equal([]dsTestActionItem{
1223 {Address: "111 Test Addr", Name: "Test1"},
1224 {Address: "211 Test Addr", Name: "Test2"},
1225 }, items)
1226
1227 items = items[0:0]
1228 sds.NoError(db.From("items").Select("name").Distinct().ScanStructs(&items))
1229 sds.Equal([]dsTestActionItem{
1230 {Address: "111 Test Addr", Name: "Test1"},
1231 {Address: "211 Test Addr", Name: "Test2"},
1232 }, items)
1233
1234 items = items[0:0]
1235 sds.EqualError(db.From("items").ScanStructs(items),
1236 "goqu: type must be a pointer to a slice when scanning into structs")
1237 sds.EqualError(db.From("items").ScanStructs(&dsTestActionItem{}),
1238 "goqu: type must be a pointer to a slice when scanning into structs")
1239 sds.EqualError(db.From("items").Select("test").ScanStructs(&items),
1240 `goqu: unable to find corresponding field to column "test" returned by query`)
1241
1242 sds.Equal(goqu.ErrQueryFactoryNotFoundError, goqu.From("items").ScanStructs(items))
1243 }
1244
1245 func (sds *selectDatasetSuite) TestScanStructs_WithPreparedStatements() {
1246 mDB, sqlMock, err := sqlmock.New()
1247 sds.NoError(err)
1248 sqlMock.ExpectQuery(
1249 `SELECT "address", "name" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`,
1250 ).
1251 WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
1252 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).
1253 FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))
1254
1255 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items"`).
1256 WithArgs().
1257 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1258 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items"`).
1259 WithArgs().
1260 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1261
1262 sqlMock.ExpectQuery(
1263 `SELECT "test" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`,
1264 ).
1265 WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
1266 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1267
1268 db := goqu.New("mock", mDB)
1269 var items []dsTestActionItem
1270 sds.NoError(db.From("items").Prepared(true).Where(goqu.Ex{
1271 "name": []string{"Bob", "Sally", "Billy"},
1272 "address": "111 Test Addr",
1273 }).ScanStructs(&items))
1274 sds.Equal(items, []dsTestActionItem{
1275 {Address: "111 Test Addr", Name: "Test1"},
1276 {Address: "211 Test Addr", Name: "Test2"},
1277 })
1278
1279 items = items[0:0]
1280 sds.EqualError(db.From("items").ScanStructs(items),
1281 "goqu: type must be a pointer to a slice when scanning into structs")
1282 sds.EqualError(db.From("items").ScanStructs(&dsTestActionItem{}),
1283 "goqu: type must be a pointer to a slice when scanning into structs")
1284 sds.EqualError(db.From("items").
1285 Prepared(true).
1286 Select("test").
1287 Where(goqu.Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
1288 ScanStructs(&items), `goqu: unable to find corresponding field to column "test" returned by query`)
1289 }
1290
1291 func (sds *selectDatasetSuite) TestScanStruct() {
1292 mDB, sqlMock, err := sqlmock.New()
1293 sds.NoError(err)
1294 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items" LIMIT 1`).
1295 WithArgs().
1296 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))
1297
1298 sqlMock.ExpectQuery(`SELECT DISTINCT "name" FROM "items" LIMIT 1`).
1299 WithArgs().
1300 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))
1301
1302 sqlMock.ExpectQuery(`SELECT "test" FROM "items" LIMIT 1`).
1303 WithArgs().
1304 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1305
1306 db := goqu.New("mock", mDB)
1307 var item dsTestActionItem
1308 found, err := db.From("items").ScanStruct(&item)
1309 sds.NoError(err)
1310 sds.True(found)
1311 sds.Equal("111 Test Addr", item.Address)
1312 sds.Equal("Test1", item.Name)
1313
1314 item = dsTestActionItem{}
1315 found, err = db.From("items").Select("name").Distinct().ScanStruct(&item)
1316 sds.NoError(err)
1317 sds.True(found)
1318 sds.Equal("111 Test Addr", item.Address)
1319 sds.Equal("Test1", item.Name)
1320
1321 _, err = db.From("items").ScanStruct(item)
1322 sds.EqualError(err, "goqu: type must be a pointer to a struct when scanning into a struct")
1323 _, err = db.From("items").ScanStruct([]dsTestActionItem{})
1324 sds.EqualError(err, "goqu: type must be a pointer to a struct when scanning into a struct")
1325 _, err = db.From("items").Select("test").ScanStruct(&item)
1326 sds.EqualError(err, `goqu: unable to find corresponding field to column "test" returned by query`)
1327
1328 _, err = goqu.From("items").ScanStruct(item)
1329 sds.Equal(goqu.ErrQueryFactoryNotFoundError, err)
1330 }
1331
1332 func (sds *selectDatasetSuite) TestScanStruct_WithPreparedStatements() {
1333 mDB, sqlMock, err := sqlmock.New()
1334 sds.NoError(err)
1335 sqlMock.ExpectQuery(
1336 `SELECT "address", "name" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\) LIMIT \?`,
1337 ).
1338 WithArgs("111 Test Addr", "Bob", "Sally", "Billy", 1).
1339 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))
1340
1341 sqlMock.ExpectQuery(`SELECT "test" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\) LIMIT \?`).
1342 WithArgs("111 Test Addr", "Bob", "Sally", "Billy", 1).
1343 WillReturnRows(sqlmock.NewRows([]string{"test"}).FromCSVString("test1\ntest2"))
1344
1345 db := goqu.New("mock", mDB)
1346 var item dsTestActionItem
1347 found, err := db.From("items").Prepared(true).Where(goqu.Ex{
1348 "name": []string{"Bob", "Sally", "Billy"},
1349 "address": "111 Test Addr",
1350 }).ScanStruct(&item)
1351 sds.NoError(err)
1352 sds.True(found)
1353 sds.Equal("111 Test Addr", item.Address)
1354 sds.Equal("Test1", item.Name)
1355
1356 _, err = db.From("items").ScanStruct(item)
1357 sds.EqualError(err, "goqu: type must be a pointer to a struct when scanning into a struct")
1358 _, err = db.From("items").ScanStruct([]dsTestActionItem{})
1359 sds.EqualError(err, "goqu: type must be a pointer to a struct when scanning into a struct")
1360 _, err = db.From("items").
1361 Prepared(true).
1362 Select("test").
1363 Where(goqu.Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
1364 ScanStruct(&item)
1365 sds.EqualError(err, `goqu: unable to find corresponding field to column "test" returned by query`)
1366 }
1367
1368 func (sds *selectDatasetSuite) TestScanStructUntagged() {
1369 defer goqu.SetIgnoreUntaggedFields(false)
1370
1371 mDB, sqlMock, err := sqlmock.New()
1372 sds.NoError(err)
1373 sqlMock.ExpectQuery(`SELECT "address", "name", "untagged" FROM "items" LIMIT 1`).
1374 WithArgs().
1375 WillReturnRows(sqlmock.NewRows([]string{"address", "name", "untagged"}).FromCSVString("111 Test Addr,Test1,Test2"))
1376
1377 sqlMock.ExpectQuery(`SELECT "address", "name" FROM "items" LIMIT 1`).
1378 WithArgs().
1379 WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).FromCSVString("111 Test Addr,Test1"))
1380
1381 db := goqu.New("mock", mDB)
1382 var item dsUntaggedTestActionItem
1383
1384 found, err := db.From("items").ScanStruct(&item)
1385 sds.NoError(err)
1386 sds.True(found)
1387 sds.Equal("111 Test Addr", item.Address)
1388 sds.Equal("Test1", item.Name)
1389 sds.Equal("Test2", item.Untagged)
1390
1391
1392 goqu.SetIgnoreUntaggedFields(true)
1393
1394 item = dsUntaggedTestActionItem{}
1395 found, err = db.From("items").ScanStruct(&item)
1396 sds.NoError(err)
1397 sds.True(found)
1398 sds.Equal("111 Test Addr", item.Address)
1399 sds.Equal("Test1", item.Name)
1400 sds.Equal("", item.Untagged)
1401 }
1402
1403 func (sds *selectDatasetSuite) TestScanVals() {
1404 mDB, sqlMock, err := sqlmock.New()
1405 sds.NoError(err)
1406 sqlMock.ExpectQuery(`SELECT "id" FROM "items"`).
1407 WithArgs().
1408 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))
1409 sqlMock.ExpectQuery(`SELECT \* FROM "items"`).
1410 WithArgs().
1411 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))
1412 sqlMock.ExpectQuery(`SELECT \* FROM "items"`).
1413 WithArgs().
1414 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))
1415
1416 db := goqu.New("mock", mDB)
1417 var ids []uint32
1418 sds.NoError(db.From("items").Select("id").ScanVals(&ids))
1419 sds.Equal(ids, []uint32{1, 2, 3, 4, 5})
1420
1421 sds.EqualError(db.From("items").ScanVals([]uint32{}),
1422 "goqu: type must be a pointer to a slice when scanning into vals")
1423 sds.EqualError(db.From("items").ScanVals(dsTestActionItem{}),
1424 "goqu: type must be a pointer to a slice when scanning into vals")
1425
1426 err = goqu.From("items").ScanVals(&ids)
1427 sds.Equal(goqu.ErrQueryFactoryNotFoundError, err)
1428 }
1429
1430 func (sds *selectDatasetSuite) TestScanVals_WithPreparedStatment() {
1431 mDB, sqlMock, err := sqlmock.New()
1432 sds.NoError(err)
1433 sqlMock.ExpectQuery(
1434 `SELECT "id" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`,
1435 ).
1436 WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
1437 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))
1438
1439 sqlMock.ExpectQuery(`SELECT \* FROM "items"`).
1440 WithArgs().
1441 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))
1442 sqlMock.ExpectQuery(`SELECT \* FROM "items"`).
1443 WithArgs().
1444 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("1\n2\n3\n4\n5"))
1445
1446 db := goqu.New("mock", mDB)
1447 var ids []uint32
1448 sds.NoError(db.From("items").
1449 Prepared(true).
1450 Select("id").
1451 Where(goqu.Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
1452 ScanVals(&ids))
1453 sds.Equal([]uint32{1, 2, 3, 4, 5}, ids)
1454
1455 sds.EqualError(db.From("items").ScanVals([]uint32{}),
1456 "goqu: type must be a pointer to a slice when scanning into vals")
1457
1458 sds.EqualError(db.From("items").ScanVals(dsTestActionItem{}),
1459 "goqu: type must be a pointer to a slice when scanning into vals")
1460 }
1461
1462 func (sds *selectDatasetSuite) TestScanVal() {
1463 mDB, sqlMock, err := sqlmock.New()
1464 sds.NoError(err)
1465 sqlMock.ExpectQuery(`SELECT "id" FROM "items" LIMIT 1`).
1466 WithArgs().
1467 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("10"))
1468
1469 db := goqu.New("mock", mDB)
1470 var id int64
1471 found, err := db.From("items").Select("id").ScanVal(&id)
1472 sds.NoError(err)
1473 sds.Equal(id, int64(10))
1474 sds.True(found)
1475
1476 found, err = db.From("items").ScanVal([]int64{})
1477 sds.False(found)
1478 sds.EqualError(err, "goqu: type must be a pointer when scanning into val")
1479 found, err = db.From("items").ScanVal(10)
1480 sds.False(found)
1481 sds.EqualError(err, "goqu: type must be a pointer when scanning into val")
1482
1483 _, err = goqu.From("items").ScanVal(&id)
1484 sds.Equal(goqu.ErrQueryFactoryNotFoundError, err)
1485 }
1486
1487 func (sds *selectDatasetSuite) TestScanVal_WithPreparedStatement() {
1488 mDB, sqlMock, err := sqlmock.New()
1489 sds.NoError(err)
1490 sqlMock.ExpectQuery(
1491 `SELECT "id" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\) LIMIT ?`,
1492 ).
1493 WithArgs("111 Test Addr", "Bob", "Sally", "Billy", 1).
1494 WillReturnRows(sqlmock.NewRows([]string{"id"}).FromCSVString("10"))
1495
1496 db := goqu.New("mock", mDB)
1497 var id int64
1498 found, err := db.From("items").
1499 Prepared(true).
1500 Select("id").
1501 Where(goqu.Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
1502 ScanVal(&id)
1503 sds.NoError(err)
1504 sds.Equal(int64(10), id)
1505 sds.True(found)
1506
1507 found, err = db.From("items").ScanVal([]int64{})
1508 sds.False(found)
1509 sds.EqualError(err, "goqu: type must be a pointer when scanning into val")
1510 found, err = db.From("items").ScanVal(10)
1511 sds.False(found)
1512 sds.EqualError(err, "goqu: type must be a pointer when scanning into val")
1513 }
1514
1515 func (sds *selectDatasetSuite) TestCount() {
1516 mDB, sqlMock, err := sqlmock.New()
1517 sds.NoError(err)
1518 sqlMock.ExpectQuery(`SELECT COUNT\(\*\) AS "count" FROM "items"`).
1519 WithArgs().
1520 WillReturnRows(sqlmock.NewRows([]string{"count"}).FromCSVString("10"))
1521
1522 db := goqu.New("mock", mDB)
1523 count, err := db.From("items").Count()
1524 sds.NoError(err)
1525 sds.Equal(count, int64(10))
1526 }
1527
1528 func (sds *selectDatasetSuite) TestCount_WithPreparedStatement() {
1529 mDB, sqlMock, err := sqlmock.New()
1530 sds.NoError(err)
1531 sqlMock.ExpectQuery(
1532 `SELECT COUNT\(\*\) AS "count" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`,
1533 ).
1534 WithArgs("111 Test Addr", "Bob", "Sally", "Billy", 1).
1535 WillReturnRows(sqlmock.NewRows([]string{"count"}).FromCSVString("10"))
1536
1537 ds := goqu.New("mock", mDB)
1538 count, err := ds.From("items").
1539 Prepared(true).
1540 Where(goqu.Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
1541 Count()
1542 sds.NoError(err)
1543 sds.Equal(int64(10), count)
1544 }
1545
1546 func (sds *selectDatasetSuite) TestPluck() {
1547 mDB, sqlMock, err := sqlmock.New()
1548 sds.NoError(err)
1549 sqlMock.ExpectQuery(`SELECT "name" FROM "items"`).
1550 WithArgs().
1551 WillReturnRows(sqlmock.NewRows([]string{"name"}).FromCSVString("test1\ntest2\ntest3\ntest4\ntest5"))
1552
1553 db := goqu.New("mock", mDB)
1554 var names []string
1555 sds.NoError(db.From("items").Pluck(&names, "name"))
1556 sds.Equal([]string{"test1", "test2", "test3", "test4", "test5"}, names)
1557 }
1558
1559 func (sds *selectDatasetSuite) TestPluck_WithPreparedStatement() {
1560 mDB, sqlMock, err := sqlmock.New()
1561 sds.NoError(err)
1562 sqlMock.ExpectQuery(
1563 `SELECT "name" FROM "items" WHERE \(\("address" = \?\) AND \("name" IN \(\?, \?, \?\)\)\)`,
1564 ).
1565 WithArgs("111 Test Addr", "Bob", "Sally", "Billy").
1566 WillReturnRows(sqlmock.NewRows([]string{"name"}).FromCSVString("Bob\nSally\nBilly"))
1567
1568 db := goqu.New("mock", mDB)
1569 var names []string
1570 sds.NoError(db.From("items").
1571 Prepared(true).
1572 Where(goqu.Ex{"name": []string{"Bob", "Sally", "Billy"}, "address": "111 Test Addr"}).
1573 Pluck(&names, "name"))
1574 sds.Equal([]string{"Bob", "Sally", "Billy"}, names)
1575 }
1576
1577 func (sds *selectDatasetSuite) TestSetError() {
1578 err1 := errors.New("error #1")
1579 err2 := errors.New("error #2")
1580 err3 := errors.New("error #3")
1581
1582
1583 md := new(mocks.SQLDialect)
1584 ds := goqu.From("test").SetDialect(md)
1585 ds = ds.SetError(err1)
1586 sds.Equal(err1, ds.Error())
1587 sql, args, err := ds.ToSQL()
1588 sds.Empty(sql)
1589 sds.Empty(args)
1590 sds.Equal(err1, err)
1591
1592
1593 ds = ds.SetError(err2)
1594 sds.Equal(err1, ds.Error())
1595 sql, args, err = ds.ToSQL()
1596 sds.Empty(sql)
1597 sds.Empty(args)
1598 sds.Equal(err1, err)
1599
1600
1601 ds = ds.ClearWindow()
1602 sds.Equal(err1, ds.Error())
1603 sql, args, err = ds.ToSQL()
1604 sds.Empty(sql)
1605 sds.Empty(args)
1606 sds.Equal(err1, err)
1607
1608
1609 c := ds.GetClauses()
1610 sqlB := sb.NewSQLBuilder(false)
1611 md.On("ToInsertSQL", sqlB, c).Run(func(args mock.Arguments) {
1612 args.Get(0).(sb.SQLBuilder).SetError(err3)
1613 }).Once()
1614
1615 sql, args, err = ds.ToSQL()
1616 sds.Empty(sql)
1617 sds.Empty(args)
1618 sds.Equal(err1, err)
1619 }
1620
1621 func TestSelectDataset(t *testing.T) {
1622 suite.Run(t, new(selectDatasetSuite))
1623 }
1624
View as plain text