-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate_test.go
290 lines (256 loc) · 10.7 KB
/
translate_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"strings"
"testing"
)
func TestTranslateTextQuerySimple(t *testing.T) {
out, err := translateTextQuery("foobar")
if err != nil {
t.Fatal(err)
}
if out.Type != "text" || out.Text != "foobar" {
t.Fatal("unexpected text query")
}
// Works with spaces.
out, err = translateTextQuery("foo\nbar whee")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" || out.Children[0].Text != "foo" || out.Children[1].Text != "bar" || out.Children[2].Text != "whee" {
t.Fatal("unexpected text query")
}
// Works with fields.
out, err = translateTextQuery("stuff:blah")
if err != nil {
t.Fatal(err)
}
if out.Type != "text" || out.Text != "blah" || out.Field != "stuff" {
t.Fatal("unexpected text query")
}
// Works with a space between the colon.
out, err = translateTextQuery("stuff: yay blah")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" ||
out.Children[0].Text != "yay" || out.Children[0].Field != "stuff" ||
out.Children[1].Text != "blah" || out.Children[1].Field != "stuff" {
t.Fatal("unexpected text query")
}
// Recognizes partial hits.
out, err = translateTextQuery("foo*")
if err != nil {
t.Fatal(err)
}
if out.Type != "text" || out.Text != "foo*" || !out.IsPattern {
t.Fatal("unexpected text query")
}
out, err = translateTextQuery("foo* ?bar whee")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" ||
out.Children[0].Text != "foo*" || !out.Children[0].IsPattern ||
out.Children[1].Text != "?bar" || !out.Children[1].IsPattern ||
out.Children[2].Text != "whee" || out.Children[2].IsPattern {
t.Fatal("unexpected text query")
}
// Fails correctly.
out, err = translateTextQuery("\n")
if err == nil || strings.Index(err.Error(), "no search terms") < 0 {
t.Fatal("should have failed")
}
}
func TestTranslateTextQueryNot(t *testing.T) {
out, err := translateTextQuery("NOT foobar")
if err != nil {
t.Fatal(err)
}
if out.Type != "not" || out.Child.Type != "text" || out.Child.Text != "foobar" {
t.Fatal("unexpected NOT query")
}
// Works with multiple words.
out, err = translateTextQuery("NOT foo bar")
if err != nil {
t.Fatal(err)
}
if out.Type != "not" || out.Child.Type != "and" || out.Child.Children[0].Text != "foo" || out.Child.Children[1].Text != "bar" {
t.Fatal("unexpected NOT query")
}
// Adding some parentheses and spaces.
out, err = translateTextQuery("NOT ( foobar )")
if err != nil {
t.Fatal(err)
}
if out.Type != "not" || out.Child.Type != "text" || out.Child.Text != "foobar" {
t.Fatal("unexpected NOT query")
}
// Fails correctly.
out, err = translateTextQuery("NOT ")
if err == nil || strings.Index(err.Error(), "no search terms") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("foo NOT bar") // ... should be foo AND NOT bar
if err == nil || strings.Index(err.Error(), "illegal placement") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("(foo) NOT (bar)") // ... should be (foo) AND NOT (bar)
if err == nil || strings.Index(err.Error(), "illegal placement") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("(foo)NOT(bar)") // ... should be (foo) AND NOT (bar)
if err == nil || strings.Index(err.Error(), "illegal placement") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("(foo)NOT bar") // ... should be (foo) AND NOT bar
if err == nil || strings.Index(err.Error(), "illegal placement") < 0 {
t.Fatal("should have failed")
}
}
func TestTranslateTextQueryAnd(t *testing.T) {
out, err := translateTextQuery("foo AND bar")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" || len(out.Children) != 2 || out.Children[0].Type != "text" || out.Children[0].Text != "foo" || out.Children[1].Type != "text" || out.Children[1].Text != "bar" {
t.Fatal("unexpected AND query")
}
// Works with multiple words and conditions.
out, err = translateTextQuery("foo bar AND whee stuff AND other")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" || len(out.Children) != 3 ||
out.Children[0].Type != "and" || out.Children[0].Children[0].Text != "foo" || out.Children[0].Children[1].Text != "bar" ||
out.Children[1].Type != "and" || out.Children[1].Children[0].Text != "whee" || out.Children[1].Children[1].Text != "stuff" ||
out.Children[2].Type != "text" || out.Children[2].Text != "other" {
t.Fatal("unexpected AND query")
}
// Works with parentheses.
out, err = translateTextQuery("(foo) AND (bar)")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" || len(out.Children) != 2 || out.Children[0].Type != "text" || out.Children[0].Text != "foo" || out.Children[1].Type != "text" || out.Children[1].Text != "bar" {
t.Fatal("unexpected AND query")
}
// Fails correctly.
out, err = translateTextQuery("AND")
if err == nil || strings.Index(err.Error(), "trailing") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("asdasd AND")
if err == nil || strings.Index(err.Error(), "trailing") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("AND asdasd")
if err == nil || strings.Index(err.Error(), "no search terms") < 0 {
t.Fatal("should have failed")
}
}
func TestTranslateTextQueryOr(t *testing.T) {
out, err := translateTextQuery("foo OR bar")
if err != nil {
t.Fatal(err)
}
if out.Type != "or" || len(out.Children) != 2 || out.Children[0].Type != "text" || out.Children[0].Text != "foo" || out.Children[1].Type != "text" || out.Children[1].Text != "bar" {
t.Fatal("unexpected OR query")
}
// Works with multiple words and conditions.
out, err = translateTextQuery("foo bar OR whee stuff OR other")
if err != nil {
t.Fatal(err)
}
if out.Type != "or" || len(out.Children) != 3 ||
out.Children[0].Type != "and" || out.Children[0].Children[0].Text != "foo" || out.Children[0].Children[1].Text != "bar" ||
out.Children[1].Type != "and" || out.Children[1].Children[0].Text != "whee" || out.Children[1].Children[1].Text != "stuff" ||
out.Children[2].Type != "text" || out.Children[2].Text != "other" {
t.Fatal("unexpected OR query")
}
// Works with parentheses.
out, err = translateTextQuery("(foo) OR (bar)")
if err != nil {
t.Fatal(err)
}
if out.Type != "or" || len(out.Children) != 2 || out.Children[0].Type != "text" || out.Children[0].Text != "foo" || out.Children[1].Type != "text" || out.Children[1].Text != "bar" {
t.Fatal("unexpected OR query")
}
// Fails correctly.
out, err = translateTextQuery("OR")
if err == nil || strings.Index(err.Error(), "trailing") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("asdasd OR")
if err == nil || strings.Index(err.Error(), "trailing") < 0 {
t.Fatal("should have failed")
}
out, err = translateTextQuery("OR asdasd")
if err == nil || strings.Index(err.Error(), "no search terms") < 0 {
t.Fatal("should have failed")
}
}
func TestTranslateTextQueryComplex(t *testing.T) {
out, err := translateTextQuery("foo AND bar OR NOT whee")
if err != nil {
t.Fatal(err)
}
if out.Type != "or" || len(out.Children) != 2 ||
out.Children[0].Type != "and" || len(out.Children[0].Children) != 2 ||
out.Children[0].Children[0].Type != "text" || out.Children[0].Children[0].Text != "foo" ||
out.Children[0].Children[1].Type != "text" || out.Children[0].Children[1].Text != "bar" ||
out.Children[1].Type != "not" || out.Children[1].Child.Type != "text" || out.Children[1].Child.Text != "whee" {
t.Fatal("unexpected complex query")
}
out, err = translateTextQuery("foo AND (bar OR NOT (whee))")
if err != nil {
t.Fatal(err)
}
if out.Type != "and" || len(out.Children) != 2 ||
out.Children[0].Type != "text" || out.Children[0].Text != "foo" ||
out.Children[1].Type != "or" || len(out.Children[1].Children) != 2 ||
out.Children[1].Children[0].Type != "text" || out.Children[1].Children[0].Text != "bar" ||
out.Children[1].Children[1].Type != "not" || out.Children[1].Children[1].Child.Type != "text" || out.Children[1].Children[1].Child.Text != "whee" {
t.Fatal("unexpected complex query")
}
}
func TestTranslateQuery(t *testing.T) {
out, err := translateQuery(&searchClause{ Type: "text", Text: "(foo AND bar) OR (NOT whee)" })
if err != nil {
t.Fatal(err)
}
if out.Type != "or" || len(out.Children) != 2 ||
out.Children[0].Type != "and" || len(out.Children[0].Children) != 2 ||
out.Children[0].Children[0].Type != "text" || out.Children[0].Children[0].Text != "foo" ||
out.Children[0].Children[1].Type != "text" || out.Children[0].Children[1].Text != "bar" ||
out.Children[1].Type != "not" || out.Children[1].Child.Type != "text" || out.Children[1].Child.Text != "whee" {
t.Fatal("unexpected translation of a full query")
}
// Works with AND operations.
out, err = translateQuery(&searchClause{ Type: "and", Children: []*searchClause{ &searchClause{ Type: "user", User: "Aaron" }, &searchClause{ Type: "text", Text: "NOT whee" } } })
if err != nil {
t.Fatal(err)
}
if out.Type != "and" || len(out.Children) != 2 ||
out.Children[0].Type != "user" ||
out.Children[1].Type != "not" || out.Children[1].Child.Type != "text" || out.Children[1].Child.Text != "whee" {
t.Fatal("unexpected translation of a full query")
}
// Works with OR operations.
out, err = translateQuery(&searchClause{ Type: "or", Children: []*searchClause{ &searchClause{ Type: "text", Text: "NOT whee" }, &searchClause{ Type: "time", Time: 123456 } } })
if err != nil {
t.Fatal(err)
}
if out.Type != "or" || len(out.Children) != 2 ||
out.Children[0].Type != "not" || out.Children[0].Child.Type != "text" || out.Children[0].Child.Text != "whee" ||
out.Children[1].Type != "time" {
t.Fatal("unexpected translation of a full query")
}
// Works with NOT operations.
out, err = translateQuery(&searchClause{ Type: "not", Child: &searchClause{ Type: "text", Text: "aaron OR foo" } })
if err != nil {
t.Fatal(err)
}
if out.Type != "not" || out.Child.Type != "or" {
t.Fatal("unexpected translation of a full query")
}
}