-
Notifications
You must be signed in to change notification settings - Fork 36
/
condition_test.go
103 lines (100 loc) · 1.85 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/mailru/dbr/dialect"
"github.com/stretchr/testify/assert"
)
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: Eq("col", map[int]int{}),
query: "0",
value: nil,
},
{
cond: Eq("col", []int{1}),
query: "`col` IN ?",
value: []interface{}{[]int{1}},
},
{
cond: Eq("col", map[int]int{1: 2}),
query: "`col` IN ?",
value: []interface{}{map[int]int{1: 2}},
},
{
cond: Neq("col", 1),
query: "`col` != ?",
value: []interface{}{1},
},
{
cond: Neq("col", nil),
query: "`col` IS NOT NULL",
value: nil,
},
{
cond: Neq("col", []int{}),
query: "1",
value: nil,
},
{
cond: Neq("col", []int{1}),
query: "`col` NOT IN ?",
value: []interface{}{[]int{1}},
},
{
cond: Neq("col", map[int]int{1: 2}),
query: "`col` NOT IN ?",
value: []interface{}{map[int]int{1: 2}},
},
{
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},
},
} {
buf := NewBuffer()
err := test.cond.Build(dialect.MySQL, buf)
assert.NoError(t, err)
assert.Equal(t, test.query, buf.String())
assert.Equal(t, test.value, buf.Value())
}
}