-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_test.go
163 lines (128 loc) · 6.12 KB
/
chain_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sqlb
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRebindSQL(t *testing.T) {
sql := "SELECT * FROM table where foo = ? AND bar = ?"
SetPlaceholder(Dollar)
assert.Equal(t, "SELECT * FROM table where foo = $1 AND bar = $2", Rebind(sql), "they should be equal")
SetPlaceholder(Question)
}
func TestChainBuilderWithEmpty(t *testing.T) {
chain := ChainBuilder{}.Chain
sql, args, err := chain.ToExpr()
assert.Equal(t, "", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithRaw(t *testing.T) {
chain := ChainBuilder{}.Raw("EXPLAIN")
sql, args, err := chain.ToExpr()
assert.Equal(t, "EXPLAIN", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithRawAndArgs(t *testing.T) {
chain := ChainBuilder{}.Raw("EXPLAIN", "a", "b")
sql, args, err := chain.ToExpr()
assert.Equal(t, "EXPLAIN", sql, "they should be equal")
assert.Equal(t, []interface{}{"a", "b"}, args, "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithSelect(t *testing.T) {
chain := ChainBuilder{}.Select("a", "b")
sql, args, err := chain.ToExpr()
assert.Equal(t, "SELECT a, b", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithFrom(t *testing.T) {
chain := ChainBuilder{}.From("a", "b")
sql, args, err := chain.ToExpr()
assert.Equal(t, "FROM a, b", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithOffset(t *testing.T) {
chain := ChainBuilder{}.Offset(0)
sql, args, err := chain.ToExpr()
assert.Equal(t, "OFFSET 0", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithLimit(t *testing.T) {
chain := ChainBuilder{}.Limit(10)
sql, args, err := chain.ToExpr()
assert.Equal(t, "LIMIT 10", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithOrderBy(t *testing.T) {
chain := ChainBuilder{}.OrderBy(Order{"foo", false, nil})
sql, args, err := chain.ToExpr()
assert.Equal(t, "ORDER BY foo ASC", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestChainBuilderWithOrderBy2(t *testing.T) {
pqNullsLast := true
chain := ChainBuilder{}.OrderBy(Order{"foo", false, &pqNullsLast})
sql, args, err := chain.ToExpr()
assert.Equal(t, "ORDER BY foo ASC NULLS LAST", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
assert.Equal(t, nil, err, "they should be equal")
}
func TestSelect(t *testing.T) {
sql, args, _ := Select("id", "name", "abc").ToExpr()
assert.Equal(t, "SELECT id, name, abc", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
}
func TestSelectFrom(t *testing.T) {
sql, args, _ := Select("id", "name", "abc").From("table").ToExpr()
assert.Equal(t, "SELECT id, name, abc FROM table", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
}
func TestRawSelectFrom(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table", sql, "they should be equal")
assert.Equal(t, 0, len(args), "they should be equal")
}
func TestRawSelectFromWithArgs(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").Raw("WHERE foo = ?", "bar").ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table WHERE foo = ?", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar"}, args, "they should be equal")
}
func TestRawSelectFromWhereWithArgs(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").Where(Eq{"foo", "bar"}).ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table WHERE foo = ?", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar"}, args, "they should be equal")
}
func TestRawSelectFromWhereWithArgs2(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").Where(And{Eq{"foo", "bar"}, Eq{"a", "b"}}).ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table WHERE (foo = ? AND a = ?)", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar", "b"}, args, "they should be equal")
}
func TestRawSelectFromWhereWithArgs3(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").Where(Eq{"foo", "bar"}, Eq{"a", "b"}).ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table WHERE (foo = ? AND a = ?)", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar", "b"}, args, "they should be equal")
}
func TestRawSelectFromWhereOrWithArgs2(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").Where(Or{Eq{"foo", "bar"}, Eq{"a", "b"}}).ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table WHERE (foo = ? OR a = ?)", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar", "b"}, args, "they should be equal")
}
func TestUsage1(t *testing.T) {
sql, args, _ := Raw("EXPLAIN").Select("id", "name", "abc").From("table").Where(Or{Eq{"foo", "bar"}, Eq{"a", "b"}}).Offset(0).Limit(10).ToExpr()
assert.Equal(t, "EXPLAIN SELECT id, name, abc FROM table WHERE (foo = ? OR a = ?) OFFSET 0 LIMIT 10", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar", "b"}, args, "they should be equal")
}
func TestAppendChain(t *testing.T) {
chain1 := From("table").Where(Eq{"foo", "bar"})
chain2 := Select("*")
finalChain := chain2.Append(chain1)
sql, args, _ := finalChain.ToExpr()
assert.Equal(t, "SELECT * FROM table WHERE foo = ?", sql, "they should be equal")
assert.Equal(t, []interface{}{"bar"}, args, "they should be equal")
}