forked from slicebit/qb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional_test.go
157 lines (112 loc) · 4.24 KB
/
conditional_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
package qb
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestConditionals(t *testing.T) {
sqlite := NewDialect("sqlite3")
sqlite.SetEscaping(true)
mysql := NewDialect("mysql")
mysql.SetEscaping(true)
postgres := NewDialect("postgres")
postgres.SetEscaping(true)
country := Column("country", Varchar().NotNull())
var sql string
var bindings interface{}
like := Like(country, "%land%")
sql, _ = like.Build(sqlite)
assert.Equal(t, sql, "(country LIKE '%land%')")
sql, _ = like.Build(mysql)
assert.Equal(t, sql, "(`country` LIKE '%land%')")
sql, _ = like.Build(postgres)
assert.Equal(t, sql, "(\"country\" LIKE '%land%')")
notIn := NotIn(country, "USA", "England", "Sweden")
sql, bindings = notIn.Build(sqlite)
assert.Equal(t, sql, "(country NOT IN (?, ?, ?))")
assert.Equal(t, bindings, []interface{}{"USA", "England", "Sweden"})
sql, bindings = notIn.Build(mysql)
assert.Equal(t, sql, "(`country` NOT IN (?, ?, ?))")
assert.Equal(t, bindings, []interface{}{"USA", "England", "Sweden"})
sql, bindings = notIn.Build(postgres)
assert.Equal(t, sql, "(\"country\" NOT IN ($1, $2, $3))")
assert.Equal(t, bindings, []interface{}{"USA", "England", "Sweden"})
postgres.Reset()
in := In(country, "USA", "England", "Sweden")
sql, bindings = in.Build(sqlite)
assert.Equal(t, sql, "(country IN (?, ?, ?))")
assert.Equal(t, bindings, []interface{}{"USA", "England", "Sweden"})
sql, bindings = in.Build(mysql)
assert.Equal(t, sql, "(`country` IN (?, ?, ?))")
assert.Equal(t, bindings, []interface{}{"USA", "England", "Sweden"})
sql, bindings = in.Build(postgres)
assert.Equal(t, sql, "(\"country\" IN ($1, $2, $3))")
assert.Equal(t, bindings, []interface{}{"USA", "England", "Sweden"})
postgres.Reset()
notEq := NotEq(country, "USA")
sql, bindings = notEq.Build(sqlite)
assert.Equal(t, sql, "(country != ?)")
assert.Equal(t, bindings, []interface{}{"USA"})
sql, bindings = notEq.Build(mysql)
assert.Equal(t, sql, "(`country` != ?)")
assert.Equal(t, bindings, []interface{}{"USA"})
sql, bindings = notEq.Build(postgres)
assert.Equal(t, sql, "(\"country\" != $1)")
assert.Equal(t, bindings, []interface{}{"USA"})
postgres.Reset()
eq := Eq(country, "Turkey")
sql, bindings = eq.Build(sqlite)
assert.Equal(t, sql, "(country = ?)")
assert.Equal(t, bindings, []interface{}{"Turkey"})
sql, bindings = eq.Build(mysql)
assert.Equal(t, sql, "(`country` = ?)")
assert.Equal(t, bindings, []interface{}{"Turkey"})
sql, bindings = eq.Build(postgres)
assert.Equal(t, sql, "(\"country\" = $1)")
assert.Equal(t, bindings, []interface{}{"Turkey"})
postgres.Reset()
score := Column("score", BigInt().NotNull())
gt := Gt(score, 1500)
sql, bindings = gt.Build(sqlite)
assert.Equal(t, sql, "(score > ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = gt.Build(mysql)
assert.Equal(t, sql, "(`score` > ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = gt.Build(postgres)
assert.Equal(t, sql, "(\"score\" > $1)")
assert.Equal(t, bindings, []interface{}{1500})
postgres.Reset()
st := St(score, 1500)
sql, bindings = st.Build(sqlite)
assert.Equal(t, sql, "(score < ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = st.Build(mysql)
assert.Equal(t, sql, "(`score` < ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = st.Build(postgres)
assert.Equal(t, sql, "(\"score\" < $1)")
assert.Equal(t, bindings, []interface{}{1500})
postgres.Reset()
gte := Gte(score, 1500)
sql, bindings = gte.Build(sqlite)
assert.Equal(t, sql, "(score >= ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = gte.Build(mysql)
assert.Equal(t, sql, "(`score` >= ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = gte.Build(postgres)
assert.Equal(t, sql, "(\"score\" >= $1)")
assert.Equal(t, bindings, []interface{}{1500})
postgres.Reset()
ste := Ste(score, 1500)
sql, bindings = ste.Build(sqlite)
assert.Equal(t, sql, "(score <= ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = ste.Build(mysql)
assert.Equal(t, sql, "(`score` <= ?)")
assert.Equal(t, bindings, []interface{}{1500})
sql, bindings = ste.Build(postgres)
assert.Equal(t, sql, "(\"score\" <= $1)")
assert.Equal(t, bindings, []interface{}{1500})
postgres.Reset()
}