Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit a0a6095

Browse files
committed
add more tests
1 parent 9d86bc6 commit a0a6095

File tree

3 files changed

+200
-1
lines changed

3 files changed

+200
-1
lines changed

builder_test.go

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,91 @@ func TestBuilderCond(t *testing.T) {
3535
"d IN (?,?)",
3636
[]interface{}{"e", "f"},
3737
},
38+
{
39+
Eq{"e": Select("id").From("f").Where(Eq{"g": 1})},
40+
"e=(SELECT id FROM f WHERE g=?)",
41+
[]interface{}{1},
42+
},
43+
{
44+
Eq{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
45+
"e=(SELECT id FROM f WHERE g=?)",
46+
[]interface{}{1},
47+
},
3848
{
3949
Neq{"d": []string{"e", "f"}},
4050
"d NOT IN (?,?)",
4151
[]interface{}{"e", "f"},
4252
},
53+
{
54+
Neq{"e": Select("id").From("f").Where(Eq{"g": 1})},
55+
"e<>(SELECT id FROM f WHERE g=?)",
56+
[]interface{}{1},
57+
},
58+
{
59+
Neq{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
60+
"e<>(SELECT id FROM f WHERE g=?)",
61+
[]interface{}{1},
62+
},
4363
{
4464
Lt{"d": 3},
4565
"d<?",
4666
[]interface{}{3},
4767
},
68+
{
69+
Lt{"e": Select("id").From("f").Where(Eq{"g": 1})},
70+
"e<(SELECT id FROM f WHERE g=?)",
71+
[]interface{}{1},
72+
},
73+
{
74+
Lt{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
75+
"e<(SELECT id FROM f WHERE g=?)",
76+
[]interface{}{1},
77+
},
4878
{
4979
Lte{"d": 3},
5080
"d<=?",
5181
[]interface{}{3},
5282
},
83+
{
84+
Lte{"e": Select("id").From("f").Where(Eq{"g": 1})},
85+
"e<=(SELECT id FROM f WHERE g=?)",
86+
[]interface{}{1},
87+
},
88+
{
89+
Lte{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
90+
"e<=(SELECT id FROM f WHERE g=?)",
91+
[]interface{}{1},
92+
},
5393
{
5494
Gt{"d": 3},
5595
"d>?",
5696
[]interface{}{3},
5797
},
98+
{
99+
Gt{"e": Select("id").From("f").Where(Eq{"g": 1})},
100+
"e>(SELECT id FROM f WHERE g=?)",
101+
[]interface{}{1},
102+
},
103+
{
104+
Gt{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
105+
"e>(SELECT id FROM f WHERE g=?)",
106+
[]interface{}{1},
107+
},
58108
{
59109
Gte{"d": 3},
60110
"d>=?",
61111
[]interface{}{3},
62112
},
113+
{
114+
Gte{"e": Select("id").From("f").Where(Eq{"g": 1})},
115+
"e>=(SELECT id FROM f WHERE g=?)",
116+
[]interface{}{1},
117+
},
118+
{
119+
Gte{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
120+
"e>=(SELECT id FROM f WHERE g=?)",
121+
[]interface{}{1},
122+
},
63123
{
64124
Between{"d", 0, 2},
65125
"d BETWEEN ? AND ?",
@@ -100,6 +160,61 @@ func TestBuilderCond(t *testing.T) {
100160
"0=1",
101161
[]interface{}{},
102162
},
163+
{
164+
In("a", []int8{}),
165+
"0=1",
166+
[]interface{}{},
167+
},
168+
{
169+
In("a", []int16{}),
170+
"0=1",
171+
[]interface{}{},
172+
},
173+
{
174+
In("a", []int32{}),
175+
"0=1",
176+
[]interface{}{},
177+
},
178+
{
179+
In("a", []int64{}),
180+
"0=1",
181+
[]interface{}{},
182+
},
183+
{
184+
In("a", []uint{}),
185+
"0=1",
186+
[]interface{}{},
187+
},
188+
{
189+
In("a", []uint8{}),
190+
"0=1",
191+
[]interface{}{},
192+
},
193+
{
194+
In("a", []uint16{}),
195+
"0=1",
196+
[]interface{}{},
197+
},
198+
{
199+
In("a", []uint32{}),
200+
"0=1",
201+
[]interface{}{},
202+
},
203+
{
204+
In("a", []uint64{}),
205+
"0=1",
206+
[]interface{}{},
207+
},
208+
{
209+
In("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
210+
"a IN (?,?,?) AND b=?",
211+
[]interface{}{1, 2, 3, "c"},
212+
},
213+
{
214+
In("a", Select("id").From("b").Where(Eq{"c": 1})),
215+
"a IN (SELECT id FROM b WHERE c=?)",
216+
[]interface{}{1},
217+
},
103218
{
104219
NotIn("a", Expr("select id from x where name > ?", "b")),
105220
"a NOT IN (select id from x where name > ?)",
@@ -110,11 +225,71 @@ func TestBuilderCond(t *testing.T) {
110225
"0=0",
111226
[]interface{}{},
112227
},
228+
{
229+
NotIn("a", []int8{}),
230+
"0=0",
231+
[]interface{}{},
232+
},
233+
{
234+
NotIn("a", []int16{}),
235+
"0=0",
236+
[]interface{}{},
237+
},
238+
{
239+
NotIn("a", []int32{}),
240+
"0=0",
241+
[]interface{}{},
242+
},
243+
{
244+
NotIn("a", []int64{}),
245+
"0=0",
246+
[]interface{}{},
247+
},
248+
{
249+
NotIn("a", []uint{}),
250+
"0=0",
251+
[]interface{}{},
252+
},
253+
{
254+
NotIn("a", []uint8{}),
255+
"0=0",
256+
[]interface{}{},
257+
},
258+
{
259+
NotIn("a", []uint16{}),
260+
"0=0",
261+
[]interface{}{},
262+
},
263+
{
264+
NotIn("a", []uint32{}),
265+
"0=0",
266+
[]interface{}{},
267+
},
268+
{
269+
NotIn("a", []uint64{}),
270+
"0=0",
271+
[]interface{}{},
272+
},
273+
{
274+
NotIn("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
275+
"a NOT IN (?,?,?) AND b=?",
276+
[]interface{}{1, 2, 3, "c"},
277+
},
278+
{
279+
NotIn("a", Select("id").From("b").Where(Eq{"c": 1})),
280+
"a NOT IN (SELECT id FROM b WHERE c=?)",
281+
[]interface{}{1},
282+
},
113283
{
114284
Or(Eq{"a": 1, "b": 2}, Eq{"c": 3, "d": 4}),
115285
"(a=? AND b=?) OR (c=? AND d=?)",
116286
[]interface{}{1, 2, 3, 4},
117287
},
288+
{
289+
Not{Eq{"a": 1, "b": 2}},
290+
"NOT (a=? AND b=?)",
291+
[]interface{}{1, 2},
292+
},
118293
}
119294

120295
for _, k := range cases {

cond_like.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (like Like) WriteTo(w Writer) error {
1616
if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
1717
return err
1818
}
19-
// FIXME: if use other regular express, this will be failed. but for compitable, keep this
19+
// FIXME: if use other regular express, this will be failed. but for compatible, keep this
2020
if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
2121
w.Append(like[1])
2222
} else {

cond_not.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ func (not Not) WriteTo(w Writer) error {
2121
if _, err := fmt.Fprint(w, "("); err != nil {
2222
return err
2323
}
24+
case Eq:
25+
if len(not[0].(Eq)) > 1 {
26+
if _, err := fmt.Fprint(w, "("); err != nil {
27+
return err
28+
}
29+
}
30+
case Neq:
31+
if len(not[0].(Neq)) > 1 {
32+
if _, err := fmt.Fprint(w, "("); err != nil {
33+
return err
34+
}
35+
}
2436
}
2537

2638
if err := not[0].WriteTo(w); err != nil {
@@ -32,6 +44,18 @@ func (not Not) WriteTo(w Writer) error {
3244
if _, err := fmt.Fprint(w, ")"); err != nil {
3345
return err
3446
}
47+
case Eq:
48+
if len(not[0].(Eq)) > 1 {
49+
if _, err := fmt.Fprint(w, ")"); err != nil {
50+
return err
51+
}
52+
}
53+
case Neq:
54+
if len(not[0].(Neq)) > 1 {
55+
if _, err := fmt.Fprint(w, ")"); err != nil {
56+
return err
57+
}
58+
}
3559
}
3660

3761
return nil

0 commit comments

Comments
 (0)