-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathparse_filter_test.go
More file actions
360 lines (348 loc) · 8.98 KB
/
parse_filter_test.go
File metadata and controls
360 lines (348 loc) · 8.98 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2015, 2021; oliver, DoltHub Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package jsonpath
import (
"testing"
)
// TestParseFilter tests the parse_filter function
// Covers lines 758-837 in jsonpath.go
func TestParseFilter(t *testing.T) {
tests := []struct {
name string
filter string
wantLp string
wantOp string
wantRp string
wantErr bool
coverCase string
}{
{
name: "Simple exists check",
filter: "@.isbn",
wantLp: "@.isbn",
wantOp: "exists",
wantRp: "",
wantErr: false,
coverCase: "Basic exists check without operator",
},
{
name: "String comparison with single quotes",
filter: "@.author == 'Nigel Rees'",
wantLp: "@.author",
wantOp: "==",
wantRp: "Nigel Rees",
wantErr: false,
coverCase: "Single quote handling (lines 766-773)",
},
{
name: "String comparison with single quotes - space in value",
filter: "@.author == 'John Doe'",
wantLp: "@.author",
wantOp: "==",
wantRp: "John Doe",
wantErr: false,
coverCase: "Single quote with space inside (lines 766-773)",
},
{
name: "String comparison with double quotes",
filter: `@.author == "Nigel Rees"`,
wantLp: "@.author",
wantOp: "==",
wantRp: "Nigel Rees",
wantErr: false,
coverCase: "Double quote handling (lines 775-782)",
},
{
name: "String comparison with double quotes - space in value",
filter: `@.title == "Book Title"`,
wantLp: "@.title",
wantOp: "==",
wantRp: "Book Title",
wantErr: false,
coverCase: "Double quote with space inside (lines 775-782)",
},
{
name: "Function call count with comparison",
filter: "count(@.book) > 0",
wantLp: "count(@.book)",
wantOp: ">",
wantRp: "0",
wantErr: false,
coverCase: "Nested parentheses handling (lines 784-795)",
},
{
name: "Function call with nested parentheses",
filter: "count(@.items) >= 5",
wantLp: "count(@.items)",
wantOp: ">=",
wantRp: "5",
wantErr: false,
coverCase: "Nested parentheses with >= operator (lines 784-795)",
},
{
name: "Expression with spaces around operator",
filter: "@.price > 10",
wantLp: "@.price",
wantOp: ">",
wantRp: "10",
wantErr: false,
coverCase: "Space handling as delimiter (lines 796-800)",
},
{
name: "Expression with multiple spaces",
filter: "@.price < 20",
wantLp: "@.price",
wantOp: "<",
wantRp: "20",
wantErr: false,
coverCase: "Multiple spaces handling (lines 796-800)",
},
{
name: "Function call without operator - exists check",
filter: "count(@.book)",
wantLp: "count(@.book)",
wantOp: "exists",
wantRp: "",
wantErr: false,
coverCase: "Function call without operator (lines 825-829)",
},
{
name: "Simple path without operator",
filter: "@.name",
wantLp: "@.name",
wantOp: "exists",
wantRp: "",
wantErr: false,
coverCase: "Simple path without operator (lines 825-829)",
},
{
name: "Invalid filter - too many parts",
filter: "@.a + @.b + @.c",
wantLp: "",
wantOp: "",
wantRp: "",
wantErr: true,
coverCase: "Error case - invalid extra parts (lines 813-815)",
},
{
name: "Numeric comparison less than",
filter: "@.price < 10",
wantLp: "@.price",
wantOp: "<",
wantRp: "10",
wantErr: false,
coverCase: "Less than operator",
},
{
name: "Numeric comparison less than or equal",
filter: "@.price <= 10",
wantLp: "@.price",
wantOp: "<=",
wantRp: "10",
wantErr: false,
coverCase: "Less than or equal operator",
},
{
name: "Numeric comparison greater than or equal",
filter: "@.price >= 10",
wantLp: "@.price",
wantOp: ">=",
wantRp: "10",
wantErr: false,
coverCase: "Greater than or equal operator",
},
{
name: "Numeric comparison greater than",
filter: "@.price > 10",
wantLp: "@.price",
wantOp: ">",
wantRp: "10",
wantErr: false,
coverCase: "Greater than operator",
},
{
name: "Comparison with root path on right side",
filter: "@.price < $.expensive",
wantLp: "@.price",
wantOp: "<",
wantRp: "$.expensive",
wantErr: false,
coverCase: "Root path reference on right side",
},
{
name: "Single quote with special characters",
filter: "@.code == 'A-B'",
wantLp: "@.code",
wantOp: "==",
wantRp: "A-B",
wantErr: false,
coverCase: "Single quote with hyphen (lines 766-773)",
},
{
name: "Double quote with special characters",
filter: `@.path == "a/b/c"`,
wantLp: "@.path",
wantOp: "==",
wantRp: "a/b/c",
wantErr: false,
coverCase: "Double quote with slashes (lines 775-782)",
},
{
name: "Parentheses in function with spaces",
filter: "count( @.book ) > 0",
wantLp: "count( @.book )",
wantOp: ">",
wantRp: "0",
wantErr: false,
coverCase: "Spaces inside parentheses (lines 796-800)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lp, op, rp, err := parse_filter(tt.filter)
if (err != nil) != tt.wantErr {
t.Errorf("parse_filter(%q) error = %v, wantErr %v", tt.filter, err, tt.wantErr)
return
}
if lp != tt.wantLp {
t.Errorf("parse_filter(%q) lp = %q, want %q", tt.filter, lp, tt.wantLp)
}
if op != tt.wantOp {
t.Errorf("parse_filter(%q) op = %q, want %q", tt.filter, op, tt.wantOp)
}
if rp != tt.wantRp {
t.Errorf("parse_filter(%q) rp = %q, want %q", tt.filter, rp, tt.wantRp)
}
if tt.coverCase != "" {
t.Logf("Covered: %s", tt.coverCase)
}
})
}
}
// TestParseFilterEdgeCases tests edge cases for parse_filter
func TestParseFilterEdgeCases(t *testing.T) {
tests := []struct {
name string
filter string
wantLp string
wantOp string
wantRp string
wantErr bool
coverCase string
}{
{
name: "Empty string after quotes",
filter: "@.name == ''",
wantLp: "@.name",
wantOp: "==",
wantRp: "",
wantErr: false,
coverCase: "Empty single quoted string",
},
{
name: "Empty string after double quotes",
filter: `@.name == ""`,
wantLp: "@.name",
wantOp: "==",
wantRp: "",
wantErr: false,
coverCase: "Empty double quoted string",
},
{
name: "Quote at end of filter",
filter: "@.x == 'test'",
wantLp: "@.x",
wantOp: "==",
wantRp: "test",
wantErr: false,
coverCase: "Quote at end of filter",
},
{
name: "Multiple words in quotes",
filter: "@.title == 'The Lord of the Rings'",
wantLp: "@.title",
wantOp: "==",
wantRp: "The Lord of the Rings",
wantErr: false,
coverCase: "Multiple words in quotes",
},
{
name: "Nested function call",
filter: "count(@.a.b) > 0",
wantLp: "count(@.a.b)",
wantOp: ">",
wantRp: "0",
wantErr: false,
coverCase: "Nested function call",
},
{
name: "Quote containing parenthesis",
filter: "@.formula == 'f(x)'",
wantLp: "@.formula",
wantOp: "==",
wantRp: "f(x)",
wantErr: false,
coverCase: "Quote containing parenthesis",
},
{
name: "Single quote with double quote inside",
filter: "@.text == 'say \"hello\"'",
wantLp: "@.text",
wantOp: "==",
wantRp: "say \"hello\"",
wantErr: false,
coverCase: "Single quote with double quote inside (lines 771-773)",
},
{
name: "Double quote with single quote inside",
filter: `@.text == "it's"`,
wantLp: "@.text",
wantOp: "==",
wantRp: "it's",
wantErr: false,
coverCase: "Double quote with single quote inside (lines 780-782)",
},
{
name: "Only left side no operator",
filter: "@.name",
wantLp: "@.name",
wantOp: "exists",
wantRp: "",
wantErr: false,
coverCase: "Only left side, no operator",
},
{
name: "Left and operator only",
filter: "@.x >",
wantLp: "@.x",
wantOp: ">",
wantRp: "",
wantErr: false,
coverCase: "Left and operator only (lines 830-831)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lp, op, rp, err := parse_filter(tt.filter)
if (err != nil) != tt.wantErr {
t.Errorf("parse_filter(%q) error = %v, wantErr %v", tt.filter, err, tt.wantErr)
return
}
if lp != tt.wantLp {
t.Errorf("parse_filter(%q) lp = %q, want %q", tt.filter, lp, tt.wantLp)
}
if op != tt.wantOp {
t.Errorf("parse_filter(%q) op = %q, want %q", tt.filter, op, tt.wantOp)
}
if rp != tt.wantRp {
t.Errorf("parse_filter(%q) rp = %q, want %q", tt.filter, rp, tt.wantRp)
}
if tt.coverCase != "" {
t.Logf("Covered: %s", tt.coverCase)
}
})
}
}