-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconstraint_test.go
62 lines (52 loc) · 1.39 KB
/
constraint_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
package sqlbuilder
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestConstraints(t *testing.T) {
Convey("Constraints SQL generation", t, func() {
cache := &VarCache{}
Convey("and combined", func() {
c := new(constraint)
c.gate = gate_and
c.addChild(Equal{"foo", 10})
c.addChild(Equal{"bar", "bar"})
sql := c.GetSQL(cache)
So(sql, ShouldEqual, `(foo = $1 AND bar = $2)`)
So(len(cache.vars), ShouldEqual, 2)
So(cache.vars[0], ShouldEqual, 10)
So(cache.vars[1], ShouldEqual, "bar")
})
Convey("or combined", func() {
c := new(constraint)
c.gate = gate_or
c.addChild(Equal{"foo", 10})
c.addChild(Equal{"bar", "bar"})
sql := c.GetSQL(cache)
So(sql, ShouldEqual, `(foo = $1 OR bar = $2)`)
So(len(cache.vars), ShouldEqual, 2)
So(cache.vars[0], ShouldEqual, 10)
So(cache.vars[1], ShouldEqual, "bar")
})
Convey("complex", func() {
c := new(constraint)
c.gate = gate_or
c.children = []SQLProvider{
Equal{"foo", 10},
&constraint{
gate: gate_and,
children: []SQLProvider{
Equal{"bar", "bar"},
Equal{"baz", "baz"},
},
},
}
sql := c.GetSQL(cache)
So(sql, ShouldEqual, `(foo = $1 OR (bar = $2 AND baz = $3))`)
So(len(cache.vars), ShouldEqual, 3)
So(cache.vars[0], ShouldEqual, 10)
So(cache.vars[1], ShouldEqual, "bar")
So(cache.vars[2], ShouldEqual, "baz")
})
})
}