forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition_test.go
103 lines (100 loc) · 1.87 KB
/
condition_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
package dbr
import (
"testing"
"github.com/jasonsong0/dbr/v2/dialect"
"github.com/stretchr/testify/require"
)
func TestCondition(t *testing.T) {
for _, test := range []struct {
cond Builder
query string
value []interface{}
}{
{
cond: Eq("col", 1),
query: "`col` = ?",
value: []interface{}{1},
},
{
cond: Eq("col", nil),
query: "`col` IS NULL",
value: nil,
},
{
cond: Eq("col", []int{}),
query: "0",
value: nil,
},
{
cond: Neq("col", 1),
query: "`col` != ?",
value: []interface{}{1},
},
{
cond: Neq("col", nil),
query: "`col` IS NOT NULL",
value: nil,
},
{
cond: Gt("col", 1),
query: "`col` > ?",
value: []interface{}{1},
},
{
cond: Gte("col", 1),
query: "`col` >= ?",
value: []interface{}{1},
},
{
cond: Lt("col", 1),
query: "`col` < ?",
value: []interface{}{1},
},
{
cond: Lte("col", 1),
query: "`col` <= ?",
value: []interface{}{1},
},
{
cond: And(Lt("a", 1), Or(Gt("b", 2), Neq("c", 3))),
query: "(`a` < ?) AND ((`b` > ?) OR (`c` != ?))",
value: []interface{}{1, 2, 3},
},
{
cond: Like("a", "%BLAH%", "#"),
query: "`a` LIKE '%BLAH%' ESCAPE '#'",
value: nil,
},
{
cond: Like("a", "%50#%%", "#"),
query: "`a` LIKE '%50#%%' ESCAPE '#'",
value: nil,
},
{
cond: NotLike("a", "%BLAH%", "#"),
query: "`a` NOT LIKE '%BLAH%' ESCAPE '#'",
value: nil,
},
{
cond: NotLike("a", "%50#%%", "#"),
query: "`a` NOT LIKE '%50#%%' ESCAPE '#'",
value: nil,
},
{
cond: Like("a", "_x_"),
query: "`a` LIKE '_x_'",
value: nil,
},
{
cond: NotLike("a", "_x_"),
query: "`a` NOT LIKE '_x_'",
value: nil,
},
} {
buf := NewBuffer()
err := test.cond.Build(dialect.MySQL, buf)
require.NoError(t, err)
require.Equal(t, test.query, buf.String())
require.Equal(t, test.value, buf.Value())
}
}