1 package exp_test
2
3 import (
4 "testing"
5
6 "github.com/doug-martin/goqu/v9/exp"
7 "github.com/stretchr/testify/suite"
8 )
9
10 type testSQLExpression string
11
12 func (tse testSQLExpression) Expression() exp.Expression {
13 return tse
14 }
15
16 func (tse testSQLExpression) Clone() exp.Expression {
17 return tse
18 }
19
20 func (tse testSQLExpression) ToSQL() (sql string, args []interface{}, err error) {
21 return "", nil, nil
22 }
23
24 func (tse testSQLExpression) IsPrepared() bool {
25 return false
26 }
27
28 type selectClausesSuite struct {
29 suite.Suite
30 }
31
32 func TestSelectClausesSuite(t *testing.T) {
33 suite.Run(t, new(selectClausesSuite))
34 }
35
36 func (scs *selectClausesSuite) TestHasSources() {
37 c := exp.NewSelectClauses()
38 c2 := c.SetFrom(exp.NewColumnListExpression("test"))
39
40 scs.False(c.HasSources())
41
42 scs.True(c2.HasSources())
43 }
44
45 func (scs *selectClausesSuite) TestIsDefaultSelect() {
46 c := exp.NewSelectClauses()
47 c2 := c.SelectAppend(exp.NewColumnListExpression("a"))
48
49 scs.True(c.IsDefaultSelect())
50
51 scs.False(c2.IsDefaultSelect())
52 }
53
54 func (scs *selectClausesSuite) TestSelect() {
55 c := exp.NewSelectClauses()
56 c2 := c.SetSelect(exp.NewColumnListExpression("a"))
57
58 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
59
60 scs.Equal(exp.NewColumnListExpression("a"), c2.Select())
61 }
62
63 func (scs *selectClausesSuite) TestSelectAppend() {
64 c := exp.NewSelectClauses()
65 c2 := c.SelectAppend(exp.NewColumnListExpression("a"))
66
67 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
68 scs.Equal(exp.NewColumnListExpression(exp.Star(), "a"), c2.Select())
69 }
70
71 func (scs *selectClausesSuite) TestSetSelect() {
72 c := exp.NewSelectClauses()
73 c2 := c.SetSelect(exp.NewColumnListExpression("a"))
74
75 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
76 scs.Equal(exp.NewColumnListExpression("a"), c2.Select())
77 }
78
79 func (scs *selectClausesSuite) TestDistinct() {
80 c := exp.NewSelectClauses()
81 c2 := c.SetDistinct(exp.NewColumnListExpression("a"))
82
83 scs.Nil(c.Distinct())
84 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
85
86 scs.Equal(exp.NewColumnListExpression("a"), c2.Distinct())
87 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
88 }
89
90 func (scs *selectClausesSuite) TestSetSelectDistinct() {
91 c := exp.NewSelectClauses()
92 c2 := c.SetDistinct(exp.NewColumnListExpression("a"))
93
94 scs.Nil(c.Distinct())
95 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
96
97 scs.Equal(exp.NewColumnListExpression("a"), c2.Distinct())
98 scs.Equal(exp.NewColumnListExpression(exp.Star()), c.Select())
99 }
100
101 func (scs *selectClausesSuite) TestFrom() {
102 c := exp.NewSelectClauses()
103 c2 := c.SetFrom(exp.NewColumnListExpression("a"))
104
105 scs.Nil(c.From())
106
107 scs.Equal(exp.NewColumnListExpression("a"), c2.From())
108 }
109
110 func (scs *selectClausesSuite) TestSetFrom() {
111 c := exp.NewSelectClauses()
112 c2 := c.SetFrom(exp.NewColumnListExpression("a"))
113
114 scs.Nil(c.From())
115
116 scs.Equal(exp.NewColumnListExpression("a"), c2.From())
117 }
118
119 func (scs *selectClausesSuite) TestHasAlias() {
120 c := exp.NewSelectClauses()
121 c2 := c.SetAlias(exp.NewIdentifierExpression("", "", "a"))
122
123 scs.False(c.HasAlias())
124
125 scs.True(c2.HasAlias())
126 }
127
128 func (scs *selectClausesSuite) TestAlias() {
129 c := exp.NewSelectClauses()
130 a := exp.NewIdentifierExpression("", "a", "")
131 c2 := c.SetAlias(a)
132
133 scs.Nil(c.Alias())
134
135 scs.Equal(a, c2.Alias())
136 }
137
138 func (scs *selectClausesSuite) TestSetAlias() {
139 c := exp.NewSelectClauses()
140 a := exp.NewIdentifierExpression("", "a", "")
141 c2 := c.SetAlias(a)
142
143 scs.Nil(c.Alias())
144
145 scs.Equal(a, c2.Alias())
146 }
147
148 func (scs *selectClausesSuite) TestJoins() {
149 jc := exp.NewConditionedJoinExpression(
150 exp.LeftJoinType,
151 exp.NewIdentifierExpression("", "test", ""),
152 nil,
153 )
154 c := exp.NewSelectClauses()
155 c2 := c.JoinsAppend(jc)
156
157 scs.Nil(c.Joins())
158
159 scs.Equal(exp.JoinExpressions{jc}, c2.Joins())
160 }
161
162 func (scs *selectClausesSuite) TestJoinsAppend() {
163 jc := exp.NewConditionedJoinExpression(
164 exp.LeftJoinType,
165 exp.NewIdentifierExpression("", "test1", ""),
166 nil,
167 )
168 jc2 := exp.NewUnConditionedJoinExpression(
169 exp.LeftJoinType,
170 exp.NewIdentifierExpression("", "test2", ""),
171 )
172 jc3 := exp.NewUnConditionedJoinExpression(
173 exp.InnerJoinType,
174 exp.NewIdentifierExpression("", "test3", ""),
175 )
176 c := exp.NewSelectClauses()
177 c2 := c.JoinsAppend(jc)
178 c3 := c2.JoinsAppend(jc2)
179
180 c4 := c3.JoinsAppend(jc2)
181
182 c5 := c4.JoinsAppend(jc2)
183 c6 := c4.JoinsAppend(jc3)
184
185 scs.Nil(c.Joins())
186
187 scs.Equal(exp.JoinExpressions{jc}, c2.Joins())
188 scs.Equal(exp.JoinExpressions{jc, jc2}, c3.Joins())
189 scs.Equal(exp.JoinExpressions{jc, jc2, jc2}, c4.Joins())
190 scs.Equal(exp.JoinExpressions{jc, jc2, jc2, jc2}, c5.Joins())
191 scs.Equal(exp.JoinExpressions{jc, jc2, jc2, jc3}, c6.Joins())
192 }
193
194 func (scs *selectClausesSuite) TestWhere() {
195 w := exp.Ex{"a": 1}
196
197 c := exp.NewSelectClauses()
198 c2 := c.WhereAppend(w)
199
200 scs.Nil(c.Where())
201
202 scs.Equal(exp.NewExpressionList(exp.AndType, w), c2.Where())
203 }
204
205 func (scs *selectClausesSuite) TestClearWhere() {
206 w := exp.Ex{"a": 1}
207
208 c := exp.NewSelectClauses().WhereAppend(w)
209 c2 := c.ClearWhere()
210
211 scs.Equal(exp.NewExpressionList(exp.AndType, w), c.Where())
212
213 scs.Nil(c2.Where())
214 }
215
216 func (scs *selectClausesSuite) TestWhereAppend() {
217 w := exp.Ex{"a": 1}
218 w2 := exp.Ex{"b": 2}
219
220 c := exp.NewSelectClauses()
221 c2 := c.WhereAppend(w)
222
223 c3 := c.WhereAppend(w).WhereAppend(w2)
224
225 c4 := c.WhereAppend(w, w2)
226
227 scs.Nil(c.Where())
228
229 scs.Equal(exp.NewExpressionList(exp.AndType, w), c2.Where())
230 scs.Equal(exp.NewExpressionList(exp.AndType, w).Append(w2), c3.Where())
231 scs.Equal(exp.NewExpressionList(exp.AndType, w, w2), c4.Where())
232 }
233
234 func (scs *selectClausesSuite) TestHaving() {
235 w := exp.Ex{"a": 1}
236
237 c := exp.NewSelectClauses()
238 c2 := c.HavingAppend(w)
239
240 scs.Nil(c.Having())
241
242 scs.Equal(exp.NewExpressionList(exp.AndType, w), c2.Having())
243 }
244
245 func (scs *selectClausesSuite) TestClearHaving() {
246 w := exp.Ex{"a": 1}
247
248 c := exp.NewSelectClauses().HavingAppend(w)
249 c2 := c.ClearHaving()
250
251 scs.Equal(exp.NewExpressionList(exp.AndType, w), c.Having())
252
253 scs.Nil(c2.Having())
254 }
255
256 func (scs *selectClausesSuite) TestHavingAppend() {
257 w := exp.Ex{"a": 1}
258 w2 := exp.Ex{"b": 2}
259
260 c := exp.NewSelectClauses()
261 c2 := c.HavingAppend(w)
262
263 c3 := c.HavingAppend(w).HavingAppend(w2)
264
265 c4 := c.HavingAppend(w, w2)
266
267 scs.Nil(c.Having())
268
269 scs.Equal(exp.NewExpressionList(exp.AndType, w), c2.Having())
270 scs.Equal(exp.NewExpressionList(exp.AndType, w).Append(w2), c3.Having())
271 scs.Equal(exp.NewExpressionList(exp.AndType, w, w2), c4.Having())
272 }
273
274 func (scs *selectClausesSuite) TestWindows() {
275 w := exp.NewWindowExpression(exp.NewIdentifierExpression("", "", "w"), nil, nil, nil)
276
277 c := exp.NewSelectClauses()
278 c2 := c.WindowsAppend(w)
279
280 scs.Nil(c.Windows())
281
282 scs.Equal([]exp.WindowExpression{w}, c2.Windows())
283 }
284
285 func (scs *selectClausesSuite) TestSetWindows() {
286 w := exp.NewWindowExpression(exp.NewIdentifierExpression("", "", "w"), nil, nil, nil)
287
288 c := exp.NewSelectClauses()
289 c2 := c.SetWindows([]exp.WindowExpression{w})
290
291 scs.Nil(c.Windows())
292
293 scs.Equal([]exp.WindowExpression{w}, c2.Windows())
294 }
295
296 func (scs *selectClausesSuite) TestWindowsAppend() {
297 w1 := exp.NewWindowExpression(exp.NewIdentifierExpression("", "", "w1"), nil, nil, nil)
298 w2 := exp.NewWindowExpression(exp.NewIdentifierExpression("", "", "w2"), nil, nil, nil)
299
300 c := exp.NewSelectClauses()
301 c2 := c.WindowsAppend(w1).WindowsAppend(w2)
302
303 scs.Nil(c.Windows())
304
305 scs.Equal([]exp.WindowExpression{w1, w2}, c2.Windows())
306 }
307
308 func (scs *selectClausesSuite) TestClearWindows() {
309 w := exp.NewWindowExpression(exp.NewIdentifierExpression("", "", "w"), nil, nil, nil)
310
311 c := exp.NewSelectClauses().SetWindows([]exp.WindowExpression{w})
312 scs.Nil(c.ClearWindows().Windows())
313 scs.Equal([]exp.WindowExpression{w}, c.Windows())
314 }
315
316 func (scs *selectClausesSuite) TestOrder() {
317 oe := exp.NewIdentifierExpression("", "", "a").Desc()
318
319 c := exp.NewSelectClauses()
320 c2 := c.SetOrder(oe)
321
322 scs.Nil(c.Order())
323
324 scs.Equal(exp.NewColumnListExpression(oe), c2.Order())
325 }
326
327 func (scs *selectClausesSuite) TestHasOrder() {
328 oe := exp.NewIdentifierExpression("", "", "a").Desc()
329
330 c := exp.NewSelectClauses()
331 c2 := c.SetOrder(oe)
332
333 scs.False(c.HasOrder())
334
335 scs.True(c2.HasOrder())
336 }
337
338 func (scs *selectClausesSuite) TestClearOrder() {
339 oe := exp.NewIdentifierExpression("", "", "a").Desc()
340
341 c := exp.NewSelectClauses().SetOrder(oe)
342 c2 := c.ClearOrder()
343
344 scs.Equal(exp.NewColumnListExpression(oe), c.Order())
345
346 scs.Nil(c2.Order())
347 }
348
349 func (scs *selectClausesSuite) TestSetOrder() {
350 oe := exp.NewIdentifierExpression("", "", "a").Desc()
351 oe2 := exp.NewIdentifierExpression("", "", "b").Desc()
352
353 c := exp.NewSelectClauses().SetOrder(oe)
354 c2 := c.SetOrder(oe2)
355
356 scs.Equal(exp.NewColumnListExpression(oe), c.Order())
357
358 scs.Equal(exp.NewColumnListExpression(oe2), c2.Order())
359 }
360
361 func (scs *selectClausesSuite) TestOrderAppend() {
362 oe := exp.NewIdentifierExpression("", "", "a").Desc()
363 oe2 := exp.NewIdentifierExpression("", "", "b").Desc()
364
365 c := exp.NewSelectClauses().SetOrder(oe)
366 c2 := c.OrderAppend(oe2)
367
368 scs.Equal(exp.NewColumnListExpression(oe), c.Order())
369
370 scs.Equal(exp.NewColumnListExpression(oe, oe2), c2.Order())
371 }
372
373 func (scs *selectClausesSuite) TestOrderPrepend() {
374 oe := exp.NewIdentifierExpression("", "", "a").Desc()
375 oe2 := exp.NewIdentifierExpression("", "", "b").Desc()
376
377 c := exp.NewSelectClauses().SetOrder(oe)
378 c2 := c.OrderPrepend(oe2)
379
380 scs.Equal(exp.NewColumnListExpression(oe), c.Order())
381
382 scs.Equal(exp.NewColumnListExpression(oe2, oe), c2.Order())
383 }
384
385 func (scs *selectClausesSuite) TestGroupBy() {
386 g := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "a"))
387
388 c := exp.NewSelectClauses()
389 c2 := c.SetGroupBy(g)
390
391 scs.Nil(c.GroupBy())
392
393 scs.Equal(g, c2.GroupBy())
394 }
395
396 func (scs *selectClausesSuite) TestGroupByAppend() {
397 g := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "a"))
398 g2 := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "b"))
399
400 c := exp.NewSelectClauses().SetGroupBy(g)
401 c2 := c.GroupByAppend(g2)
402
403 scs.Equal(g, c.GroupBy())
404
405 scs.Equal(exp.NewColumnListExpression(g, g2), c2.GroupBy())
406 }
407
408 func (scs *selectClausesSuite) TestGroupByAppend_NoPreviousGroupBy() {
409 g := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "a"))
410 g2 := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "b"))
411
412 c := exp.NewSelectClauses().GroupByAppend(g)
413 c2 := c.GroupByAppend(g2)
414
415 scs.Equal(g, c.GroupBy())
416
417 scs.Equal(exp.NewColumnListExpression(g, g2), c2.GroupBy())
418 }
419
420 func (scs *selectClausesSuite) TestSetGroupBy() {
421 g := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "a"))
422 g2 := exp.NewColumnListExpression(exp.NewIdentifierExpression("", "", "b"))
423
424 c := exp.NewSelectClauses().SetGroupBy(g)
425 c2 := c.SetGroupBy(g2)
426
427 scs.Equal(g, c.GroupBy())
428
429 scs.Equal(g2, c2.GroupBy())
430 }
431
432 func (scs *selectClausesSuite) TestLimit() {
433 l := 1
434
435 c := exp.NewSelectClauses()
436 c2 := c.SetLimit(l)
437
438 scs.Nil(c.Limit())
439
440 scs.Equal(l, c2.Limit())
441 }
442
443 func (scs *selectClausesSuite) TestHasLimit() {
444 l := 1
445
446 c := exp.NewSelectClauses()
447 c2 := c.SetLimit(l)
448
449 scs.False(c.HasLimit())
450
451 scs.True(c2.HasLimit())
452 }
453
454 func (scs *selectClausesSuite) TestCLearLimit() {
455 l := 1
456
457 c := exp.NewSelectClauses().SetLimit(l)
458 c2 := c.ClearLimit()
459
460 scs.True(c.HasLimit())
461
462 scs.False(c2.HasLimit())
463 }
464
465 func (scs *selectClausesSuite) TestSetLimit() {
466 l := 1
467 l2 := 2
468
469 c := exp.NewSelectClauses().SetLimit(l)
470 c2 := c.SetLimit(2)
471
472 scs.Equal(l, c.Limit())
473
474 scs.Equal(l2, c2.Limit())
475 }
476
477 func (scs *selectClausesSuite) TestOffset() {
478 o := uint(1)
479
480 c := exp.NewSelectClauses()
481 c2 := c.SetOffset(o)
482
483 scs.Equal(uint(0), c.Offset())
484
485 scs.Equal(o, c2.Offset())
486 }
487
488 func (scs *selectClausesSuite) TestClearOffset() {
489 o := uint(1)
490
491 c := exp.NewSelectClauses().SetOffset(o)
492 c2 := c.ClearOffset()
493
494 scs.Equal(o, c.Offset())
495
496 scs.Equal(uint(0), c2.Offset())
497 }
498
499 func (scs *selectClausesSuite) TestSetOffset() {
500 o := uint(1)
501 o2 := uint(2)
502
503 c := exp.NewSelectClauses().SetOffset(o)
504 c2 := c.SetOffset(2)
505
506 scs.Equal(o, c.Offset())
507
508 scs.Equal(o2, c2.Offset())
509 }
510
511 func (scs *selectClausesSuite) TestCompounds() {
512 ce := exp.NewCompoundExpression(exp.UnionCompoundType, newTestAppendableExpression("SELECT * FROM foo", []interface{}{}))
513
514 c := exp.NewSelectClauses()
515 c2 := c.CompoundsAppend(ce)
516
517 scs.Nil(c.Compounds())
518
519 scs.Equal([]exp.CompoundExpression{ce}, c2.Compounds())
520 }
521
522 func (scs *selectClausesSuite) TestCompoundsAppend() {
523 ce := exp.NewCompoundExpression(exp.UnionCompoundType, newTestAppendableExpression("SELECT * FROM foo1", []interface{}{}))
524 ce2 := exp.NewCompoundExpression(exp.UnionCompoundType, newTestAppendableExpression("SELECT * FROM foo2", []interface{}{}))
525
526 c := exp.NewSelectClauses().CompoundsAppend(ce)
527 c2 := c.CompoundsAppend(ce2)
528
529 scs.Equal([]exp.CompoundExpression{ce}, c.Compounds())
530
531 scs.Equal([]exp.CompoundExpression{ce, ce2}, c2.Compounds())
532 }
533
534 func (scs *selectClausesSuite) TestLock() {
535 l := exp.NewLock(exp.ForUpdate, exp.Wait)
536
537 c := exp.NewSelectClauses()
538 c2 := c.SetLock(l)
539
540 scs.Nil(c.Lock())
541
542 scs.Equal(l, c2.Lock())
543 }
544
545 func (scs *selectClausesSuite) TestSetLock() {
546 l := exp.NewLock(exp.ForUpdate, exp.Wait)
547 l2 := exp.NewLock(exp.ForUpdate, exp.NoWait)
548
549 c := exp.NewSelectClauses().SetLock(l)
550 c2 := c.SetLock(l2)
551
552 scs.Equal(l, c.Lock())
553
554 scs.Equal(l2, c2.Lock())
555 }
556
557 func (scs *selectClausesSuite) TestCommonTables() {
558 cte := exp.NewCommonTableExpression(true, "test", newTestAppendableExpression(`SELECT * FROM "foo"`, []interface{}{}))
559
560 c := exp.NewSelectClauses()
561 c2 := c.CommonTablesAppend(cte)
562
563 scs.Nil(c.CommonTables())
564
565 scs.Equal([]exp.CommonTableExpression{cte}, c2.CommonTables())
566 }
567
568 func (scs *selectClausesSuite) TestAddCommonTablesAppend() {
569 cte := exp.NewCommonTableExpression(true, "test", testSQLExpression("test_cte"))
570 cte2 := exp.NewCommonTableExpression(true, "test", testSQLExpression("test_cte2"))
571
572 c := exp.NewSelectClauses().CommonTablesAppend(cte)
573 c2 := c.CommonTablesAppend(cte2)
574
575 scs.Equal([]exp.CommonTableExpression{cte}, c.CommonTables())
576
577 scs.Equal([]exp.CommonTableExpression{cte, cte2}, c2.CommonTables())
578 }
579
View as plain text