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 sqlFunctionExpressionSuite struct {
11 suite.Suite
12 fn exp.SQLFunctionExpression
13 }
14
15 func TestSQLFunctionExpressionSuite(t *testing.T) {
16 suite.Run(t, &sqlFunctionExpressionSuite{
17 fn: exp.NewSQLFunctionExpression("COUNT", exp.Star()),
18 })
19 }
20
21 func (sfes *sqlFunctionExpressionSuite) TestClone() {
22 sfes.Equal(sfes.fn, sfes.fn.Clone())
23 }
24
25 func (sfes *sqlFunctionExpressionSuite) TestExpression() {
26 sfes.Equal(sfes.fn, sfes.fn.Expression())
27 }
28
29 func (sfes *sqlFunctionExpressionSuite) TestArgs() {
30 sfes.Equal([]interface{}{exp.Star()}, sfes.fn.Args())
31 }
32
33 func (sfes *sqlFunctionExpressionSuite) TestName() {
34 sfes.Equal("COUNT", sfes.fn.Name())
35 }
36
37 func (sfes *sqlFunctionExpressionSuite) TestAllOthers() {
38 fn := sfes.fn
39
40 rv := exp.NewRangeVal(1, 2)
41 pattern := "func like%"
42 inVals := []interface{}{1, 2}
43 testCases := []struct {
44 Ex exp.Expression
45 Expected exp.Expression
46 }{
47 {Ex: fn.As("a"), Expected: exp.NewAliasExpression(fn, "a")},
48 {Ex: fn.Eq(1), Expected: exp.NewBooleanExpression(exp.EqOp, fn, 1)},
49 {Ex: fn.Neq(1), Expected: exp.NewBooleanExpression(exp.NeqOp, fn, 1)},
50 {Ex: fn.Gt(1), Expected: exp.NewBooleanExpression(exp.GtOp, fn, 1)},
51 {Ex: fn.Gte(1), Expected: exp.NewBooleanExpression(exp.GteOp, fn, 1)},
52 {Ex: fn.Lt(1), Expected: exp.NewBooleanExpression(exp.LtOp, fn, 1)},
53 {Ex: fn.Lte(1), Expected: exp.NewBooleanExpression(exp.LteOp, fn, 1)},
54 {Ex: fn.Between(rv), Expected: exp.NewRangeExpression(exp.BetweenOp, fn, rv)},
55 {Ex: fn.NotBetween(rv), Expected: exp.NewRangeExpression(exp.NotBetweenOp, fn, rv)},
56 {Ex: fn.Like(pattern), Expected: exp.NewBooleanExpression(exp.LikeOp, fn, pattern)},
57 {Ex: fn.NotLike(pattern), Expected: exp.NewBooleanExpression(exp.NotLikeOp, fn, pattern)},
58 {Ex: fn.ILike(pattern), Expected: exp.NewBooleanExpression(exp.ILikeOp, fn, pattern)},
59 {Ex: fn.NotILike(pattern), Expected: exp.NewBooleanExpression(exp.NotILikeOp, fn, pattern)},
60 {Ex: fn.RegexpLike(pattern), Expected: exp.NewBooleanExpression(exp.RegexpLikeOp, fn, pattern)},
61 {Ex: fn.RegexpNotLike(pattern), Expected: exp.NewBooleanExpression(exp.RegexpNotLikeOp, fn, pattern)},
62 {Ex: fn.RegexpILike(pattern), Expected: exp.NewBooleanExpression(exp.RegexpILikeOp, fn, pattern)},
63 {Ex: fn.RegexpNotILike(pattern), Expected: exp.NewBooleanExpression(exp.RegexpNotILikeOp, fn, pattern)},
64 {Ex: fn.In(inVals), Expected: exp.NewBooleanExpression(exp.InOp, fn, inVals)},
65 {Ex: fn.NotIn(inVals), Expected: exp.NewBooleanExpression(exp.NotInOp, fn, inVals)},
66 {Ex: fn.Is(true), Expected: exp.NewBooleanExpression(exp.IsOp, fn, true)},
67 {Ex: fn.IsNot(true), Expected: exp.NewBooleanExpression(exp.IsNotOp, fn, true)},
68 {Ex: fn.IsNull(), Expected: exp.NewBooleanExpression(exp.IsOp, fn, nil)},
69 {Ex: fn.IsNotNull(), Expected: exp.NewBooleanExpression(exp.IsNotOp, fn, nil)},
70 {Ex: fn.IsTrue(), Expected: exp.NewBooleanExpression(exp.IsOp, fn, true)},
71 {Ex: fn.IsNotTrue(), Expected: exp.NewBooleanExpression(exp.IsNotOp, fn, true)},
72 {Ex: fn.IsFalse(), Expected: exp.NewBooleanExpression(exp.IsOp, fn, false)},
73 {Ex: fn.IsNotFalse(), Expected: exp.NewBooleanExpression(exp.IsNotOp, fn, false)},
74 {Ex: fn.Desc(), Expected: exp.NewOrderedExpression(fn, exp.DescSortDir, exp.NoNullsSortType)},
75 {Ex: fn.Asc(), Expected: exp.NewOrderedExpression(fn, exp.AscDir, exp.NoNullsSortType)},
76 }
77
78 for _, tc := range testCases {
79 sfes.Equal(tc.Expected, tc.Ex)
80 }
81 }
82
View as plain text