-
Notifications
You must be signed in to change notification settings - Fork 72
/
operators_test.go
231 lines (204 loc) · 6.05 KB
/
operators_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package kallax
import (
"database/sql"
"testing"
"github.com/stretchr/testify/suite"
"gopkg.in/src-d/go-kallax.v1/types"
)
type OpsSuite struct {
suite.Suite
db *sql.DB
store *Store
}
func (s *OpsSuite) SetupTest() {
var err error
s.db, err = openTestDB()
s.Nil(err)
s.store = NewStore(s.db)
}
func (s *OpsSuite) create(sql string) {
_, err := s.db.Exec(sql)
s.NoError(err)
}
func (s *OpsSuite) remove(table string) {
_, err := s.db.Exec("DROP TABLE IF EXISTS " + table)
s.NoError(err)
}
func (s *OpsSuite) TestOperators() {
s.create(`CREATE TABLE model (
id serial PRIMARY KEY,
name varchar(255) not null,
email varchar(255) not null,
age int not null
)`)
defer s.remove("model")
customGt := NewOperator(":col: > :arg:")
customIn := NewMultiOperator(":col: IN :arg:")
cases := []struct {
name string
cond Condition
count int64
}{
{"Eq", Eq(f("name"), "Joe"), 1},
{"Gt", Gt(f("age"), 1), 2},
{"customGt", customGt(f("age"), 1), 2},
{"Lt", Lt(f("age"), 2), 1},
{"Neq", Neq(f("name"), "Joe"), 2},
{"Like upper", Like(f("name"), "J%"), 2},
{"Like lower", Like(f("name"), "j%"), 0},
{"Ilike upper", Ilike(f("name"), "J%"), 2},
{"Ilike lower", Ilike(f("name"), "j%"), 2},
{"SimilarTo", SimilarTo(f("name"), "An{2}a"), 1},
{"NotSimilarTo", NotSimilarTo(f("name"), "An{2}a"), 2},
{"GtOrEq", GtOrEq(f("age"), 2), 2},
{"LtOrEq", LtOrEq(f("age"), 3), 3},
{"Not", Not(Eq(f("name"), "Joe")), 2},
{"And", And(Neq(f("name"), "Joe"), Gt(f("age"), 1)), 2},
{"Or", Or(Neq(f("name"), "Joe"), Eq(f("age"), 1)), 3},
{"In", In(f("name"), "Joe", "Jane"), 2},
{"customIn", customIn(f("name"), "Joe", "Jane"), 2},
{"NotIn", NotIn(f("name"), "Joe", "Jane"), 1},
{"MatchRegexCase upper", MatchRegexCase(f("name"), "J.*"), 2},
{"MatchRegexCase lower", MatchRegexCase(f("name"), "j.*"), 0},
{"MatchRegex upper", MatchRegex(f("name"), "J.*"), 2},
{"MatchRegex lower", MatchRegex(f("name"), "j.*"), 2},
{"NotMatchRegexCase upper", NotMatchRegexCase(f("name"), "J.*"), 1},
{"NotMatchRegexCase lower", NotMatchRegexCase(f("name"), "j.*"), 3},
{"NotMatchRegex upper", NotMatchRegex(f("name"), "J.*"), 1},
{"NotMatchRegex lower", NotMatchRegex(f("name"), "j.*"), 1},
}
s.Nil(s.store.Insert(ModelSchema, newModel("Joe", "", 1)))
s.Nil(s.store.Insert(ModelSchema, newModel("Jane", "", 2)))
s.Nil(s.store.Insert(ModelSchema, newModel("Anna", "", 2)))
for _, c := range cases {
q := NewBaseQuery(ModelSchema)
q.Where(c.cond)
s.Equal(c.count, s.store.Debug().MustCount(q), c.name)
}
}
func (s *OpsSuite) TestArrayOperators() {
s.create(`CREATE TABLE slices (
id uuid PRIMARY KEY,
elems bigint[]
)`)
defer s.remove("slices")
f := f("elems")
cases := []struct {
name string
cond Condition
ok bool
}{
{"ArrayEq", ArrayEq(f, 1, 2, 3), true},
{"ArrayEq fail", ArrayEq(f, 1, 2, 2), false},
{"ArrayNotEq", ArrayNotEq(f, 1, 2, 2), true},
{"ArrayNotEq fail", ArrayNotEq(f, 1, 2, 3), false},
{"ArrayGt", ArrayGt(f, 1, 2, 2), true},
{"ArrayGt all eq", ArrayGt(f, 1, 2, 3), false},
{"ArrayGt some lt", ArrayGt(f, 1, 3, 1), false},
{"ArrayLt", ArrayLt(f, 1, 2, 4), true},
{"ArrayLt all eq", ArrayLt(f, 1, 2, 3), false},
{"ArrayLt some gt", ArrayLt(f, 1, 1, 4), false},
{"ArrayGtOrEq", ArrayGtOrEq(f, 1, 2, 2), true},
{"ArrayGtOrEq all eq", ArrayGtOrEq(f, 1, 2, 3), true},
{"ArrayGtOrEq some lt", ArrayGtOrEq(f, 1, 3, 1), false},
{"ArrayLtOrEq", ArrayLtOrEq(f, 1, 2, 4), true},
{"ArrayLtOrEq all eq", ArrayLtOrEq(f, 1, 2, 3), true},
{"ArrayLtOrEq some gt", ArrayLtOrEq(f, 1, 1, 4), false},
{"ArrayContains", ArrayContains(f, 1, 2), true},
{"ArrayContains fail", ArrayContains(f, 5, 6), false},
{"ArrayContainedBy", ArrayContainedBy(f, 1, 2, 3, 5, 6), true},
{"ArrayContainedBy fail", ArrayContainedBy(f, 1, 2, 5, 6), false},
{"ArrayOverlap", ArrayOverlap(f, 5, 1, 7), true},
{"ArrayOverlap fail", ArrayOverlap(f, 6, 7, 8, 9), false},
}
_, err := s.db.Exec("INSERT INTO slices (id,elems) VALUES ($1, $2)", NewULID(), types.Slice([]int64{1, 2, 3}))
s.NoError(err)
for _, c := range cases {
q := NewBaseQuery(SlicesSchema)
q.Where(c.cond)
cnt, err := s.store.Count(q)
s.NoError(err, c.name)
s.Equal(c.ok, cnt > 0, "success: %s", c.name)
}
}
type object map[string]interface{}
type array []interface{}
func (s *OpsSuite) TestJSONOperators() {
s.create(`CREATE TABLE jsons (
id uuid primary key,
elem jsonb
)`)
defer s.remove("jsons")
f := f("elem")
cases := []struct {
name string
cond Condition
n int64
}{
{"JSONIsObject", JSONIsObject(f), 2},
{"JSONIsArray", JSONIsArray(f), 3},
{"JSONContains", JSONContains(f, object{"a": 1}), 1},
{"JSONContainedBy", JSONContainedBy(f, object{
"a": 1,
"b": 2,
"c": 3,
"d": 1,
}), 1},
{"JSONContainsAnyKey with array match", JSONContainsAnyKey(f, "a", "c"), 3},
{"JSONContainsAnyKey", JSONContainsAnyKey(f, "b", "e"), 2},
{"JSONContainsAllKeys with array match", JSONContainsAllKeys(f, "a", "c"), 3},
{"JSONContainsAllKeys", JSONContainsAllKeys(f, "b", "e"), 0},
{"JSONContainsAllKeys only objects", JSONContainsAllKeys(f, "a", "b", "c"), 2},
{"JSONContainsAny", JSONContainsAny(f,
object{"a": 1},
object{"a": true},
), 2},
}
var records = []interface{}{
array{"a", "c", "d"},
object{
"a": true,
"b": array{1, 2, 3},
"c": object{"d": "foo"},
},
object{
"a": 1,
"b": 2,
"c": 3,
},
array{.5, 1., 1.5},
array{1, 2, 3},
}
for _, r := range records {
_, err := s.db.Exec("INSERT INTO jsons (id,elem) VALUES ($1, $2)", NewULID(), types.JSON(r))
s.NoError(err)
}
for _, c := range cases {
q := NewBaseQuery(JsonsSchema)
q.Where(c.cond)
cnt, err := s.store.Count(q)
s.NoError(err, c.name)
s.Equal(c.n, cnt, "should retrieve %d records: %s", c.n, c.name)
}
}
func TestOperators(t *testing.T) {
suite.Run(t, new(OpsSuite))
}
var SlicesSchema = &BaseSchema{
alias: "_sl",
table: "slices",
id: f("id"),
columns: []SchemaField{
f("id"),
f("elems"),
},
}
var JsonsSchema = &BaseSchema{
alias: "_js",
table: "jsons",
id: f("id"),
columns: []SchemaField{
f("id"),
f("elem"),
},
}