-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcondition_test.go
167 lines (135 loc) · 4.13 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
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
164
165
166
167
package godb
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestQ(t *testing.T) {
Convey("Q build", t, func() {
Convey("A simple condition with one placeholder and one argument", func() {
sql := "id = ?"
q := Q(sql, 123)
So(q.sql, ShouldEqual, sql)
})
Convey("A simple condition with multiple placeholders arguments", func() {
sql := "id = ? AND is_deleted = ?"
q := Q(sql, 123, 0)
So(q.sql, ShouldEqual, sql)
})
Convey("A condition expanding slice argument", func() {
q := Q("id IN (?)", []int{123, 456})
So(q.sql, ShouldEqual, "id IN (?,?)")
})
Convey("A condition with slice and non slice arguments", func() {
q := Q("id IN (?) AND is_deleted = ?", []int{123, 456}, 0)
So(q.sql, ShouldEqual, "id IN (?,?) AND is_deleted = ?")
})
})
Convey("Q set error field ...", t, func() {
Convey("Error is set if arguments and placeholders count does not math", func() {
sql := "id = ?"
q := Q(sql, 123, 456)
So(q.err, ShouldNotBeNil)
})
Convey("Error is set if a given slice is empty", func() {
q := Q("id IN (?)", []int{})
So(q.err, ShouldNotBeNil)
})
Convey("Error is set if a given slice is nil", func() {
q := Q("id IN (?)", nil)
So(q.err, ShouldNotBeNil)
})
})
}
func TestErr(t *testing.T) {
Convey("Err returns the condition error", t, func() {
q := Q("?", 123, 456)
So(q.Err(), ShouldNotBeNil)
})
}
func TestAND(t *testing.T) {
Convey("Given multiple conditions", t, func() {
c1 := Q("id = ?", 123)
c2 := Q("is_deleted = ?", 0)
Convey("And conjunction joins conditions with 'AND'", func() {
So(And(c1, c2).sql, ShouldEqual, "id = ? AND is_deleted = ?")
})
Convey("And conjunction joins arguments", func() {
So(len(And(c1, c2).args), ShouldEqual, 2)
})
})
Convey("Given one condition", t, func() {
sql := "id = ?"
c := Q(sql, 123)
Convey("And conjunction return a similar condition", func() {
So(And(c).sql, ShouldEqual, sql)
So(len(And(c).args), ShouldEqual, 1)
})
})
Convey("Given conditions with at least one error", t, func() {
c := &Condition{err: fmt.Errorf("A mysterious error has occurred")}
Convey("And return a condition with an error", func() {
So(And(c).err, ShouldNotBeNil)
})
})
}
func TestOR(t *testing.T) {
Convey("Given multiple conditions", t, func() {
c1 := Q("id = ?", 123)
c2 := Q("id = ?", 456)
Convey("Or conjunction joins conditions with 'OR'", func() {
So(Or(c1, c2).sql, ShouldContainSubstring, "id = ? OR id = ?")
})
Convey("Or conjunction joins arguments", func() {
So(len(Or(c1, c2).args), ShouldEqual, 2)
})
})
Convey("Given one condition", t, func() {
sql := "id = ?"
c := Q(sql, 123)
Convey("Or conjunction return a similar condition", func() {
So(Or(c).sql, ShouldEqual, sql)
So(len(Or(c).args), ShouldEqual, 1)
})
})
Convey("Given conditions with at least one error", t, func() {
c := &Condition{err: fmt.Errorf("A mysterious error has occurred")}
Convey("Or return a condition with an error", func() {
So(Or(c).err, ShouldNotBeNil)
})
})
}
func TestNOT(t *testing.T) {
Convey("Given a condition", t, func() {
c := Q("id = ?", 123)
Convey("Not surround the condition with 'NOT (' and ')'", func() {
So(Not(c).sql, ShouldEqual, "NOT (id = ?)")
})
Convey("Not return a condition with the original arguments", func() {
So(len(Not(c).args), ShouldEqual, len(c.args))
})
})
Convey("Given a condition with an error", t, func() {
c := &Condition{err: fmt.Errorf("A mysterious error has occurred")}
Convey("Not return a condition with an error", func() {
So(Not(c).err, ShouldNotBeNil)
})
})
}
func TestAllTogether(t *testing.T) {
Convey("Given a 'complex' condition", t, func() {
q := And(Q("id IN (?)", []int{1, 2, 3, 4, 5}),
Not(Q("is_deleted = ?", 1)),
Or(Q("category_id = ?", 123),
Q("tag IN (?)", []string{"foo", "bar", "baz"}),
),
)
Convey("The SQL string is correct", func() {
sql := "id IN (?,?,?,?,?) AND NOT (is_deleted = ?) AND (category_id = ? OR tag IN (?,?,?))"
So(q.sql, ShouldEqual, sql)
})
Convey("The aruments count is correct", func() {
So(len(q.args), ShouldEqual, 10)
})
})
}