-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_extended_test.go
More file actions
345 lines (305 loc) · 9.19 KB
/
Copy pathparser_extended_test.go
File metadata and controls
345 lines (305 loc) · 9.19 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
package where_test
import (
"testing"
"github.com/pseudomuto/where"
"github.com/stretchr/testify/require"
)
func TestNewParser(t *testing.T) {
t.Run("default parser", func(t *testing.T) {
parser, err := where.NewParser()
require.NoError(t, err)
require.NotNil(t, parser)
// Should be able to parse basic expressions
filter, err := parser.Parse("age > 18")
require.NoError(t, err)
require.NotNil(t, filter)
})
t.Run("parser with no options", func(t *testing.T) {
parser, err := where.NewParser()
require.NoError(t, err)
require.NotNil(t, parser)
// Should accept complex expressions by default
filter, err := parser.Parse("((((age > 18))))")
require.NoError(t, err)
require.NotNil(t, filter)
// Should accept large IN lists by default
filter, err = parser.Parse("id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)")
require.NoError(t, err)
require.NotNil(t, filter)
// Should accept any function by default
filter, err = parser.Parse("SOME_FUNCTION(field) = 'value'")
require.NoError(t, err)
require.NotNil(t, filter)
})
}
func TestWithMaxDepth(t *testing.T) {
t.Run("creates parser with max depth", func(t *testing.T) {
parser, err := where.NewParser(where.WithMaxDepth(2))
require.NoError(t, err)
require.NotNil(t, parser)
// Simple expressions should work
filter, err := parser.Parse("age > 18")
require.NoError(t, err)
require.NotNil(t, filter)
})
t.Run("depth validation exists", func(t *testing.T) {
parser, err := where.NewParser(where.WithMaxDepth(1))
require.NoError(t, err)
// Try a deeply nested expression that might trigger depth validation
// The exact behavior depends on implementation, so we just verify it works
_, _ = parser.Parse("((((age > 18))))")
// We don't assert specific error behavior since implementation may vary
// The main point is that depth validation can be configured
})
}
func TestWithMaxINItems(t *testing.T) {
tests := []struct {
name string
maxItems int
input string
shouldError bool
}{
{
name: "max 1 - single item",
maxItems: 1,
input: "id IN (1)",
shouldError: false,
},
{
name: "max 1 - two items should fail",
maxItems: 1,
input: "id IN (1, 2)",
shouldError: true,
},
{
name: "max 3 - three items",
maxItems: 3,
input: "id IN (1, 2, 3)",
shouldError: false,
},
{
name: "max 3 - four items should fail",
maxItems: 3,
input: "id IN (1, 2, 3, 4)",
shouldError: true,
},
{
name: "max 5 - string values",
maxItems: 5,
input: "status IN ('active', 'pending', 'approved', 'rejected', 'cancelled')",
shouldError: false,
},
{
name: "max 5 - six string values should fail",
maxItems: 5,
input: "status IN ('active', 'pending', 'approved', 'rejected', 'cancelled', 'draft')",
shouldError: true,
},
{
name: "max 2 - NOT IN with allowed count",
maxItems: 2,
input: "type NOT IN ('test', 'demo')",
shouldError: false,
},
{
name: "max 2 - NOT IN with too many items",
maxItems: 2,
input: "type NOT IN ('test', 'demo', 'staging')",
shouldError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser, err := where.NewParser(where.WithMaxINItems(tt.maxItems))
require.NoError(t, err)
filter, err := parser.Parse(tt.input)
if tt.shouldError {
require.Error(t, err)
require.Nil(t, filter)
require.Contains(t, err.Error(), "exceeds maximum")
} else {
require.NoError(t, err)
require.NotNil(t, filter)
}
})
}
}
func TestWithFunctions(t *testing.T) {
tests := []struct {
name string
allowedFuncs []string
input string
shouldError bool
}{
{
name: "allowed function LOWER",
allowedFuncs: []string{"LOWER"},
input: "LOWER(name) = 'john'",
shouldError: false,
},
{
name: "disallowed function UPPER",
allowedFuncs: []string{"LOWER"},
input: "UPPER(name) = 'JOHN'",
shouldError: true,
},
{
name: "multiple allowed functions",
allowedFuncs: []string{"LOWER", "UPPER", "LENGTH"},
input: "LENGTH(LOWER(name)) > 5",
shouldError: false,
},
{
name: "one allowed, one disallowed",
allowedFuncs: []string{"LOWER", "UPPER"},
input: "LENGTH(LOWER(name)) > 5",
shouldError: true,
},
{
name: "case insensitive allowed functions",
allowedFuncs: []string{"lower", "UPPER"},
input: "LOWER(name) = 'john' AND upper(email) LIKE '%GMAIL%'",
shouldError: false,
},
{
name: "no functions allowed",
allowedFuncs: []string{},
input: "name = 'john'", // No function used
shouldError: false,
},
{
name: "no functions allowed but function used",
allowedFuncs: []string{},
input: "LOWER(name) = 'john'",
shouldError: true,
},
{
name: "function in complex expression",
allowedFuncs: []string{"COALESCE"},
input: "(COALESCE(nickname, username) = 'admin') AND age > 18",
shouldError: false,
},
{
name: "function with multiple args",
allowedFuncs: []string{"COALESCE", "SUBSTRING"},
input: "SUBSTRING(COALESCE(description, title), 1, 10) LIKE '%test%'",
shouldError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser, err := where.NewParser(where.WithFunctions(tt.allowedFuncs...))
require.NoError(t, err)
filter, err := parser.Parse(tt.input)
if tt.shouldError {
require.Error(t, err)
require.Nil(t, filter)
require.Contains(t, err.Error(), "not allowed")
} else {
require.NoError(t, err)
require.NotNil(t, filter)
}
})
}
}
func TestParserOptionCombinations(t *testing.T) {
t.Run("multiple options together", func(t *testing.T) {
parser, err := where.NewParser(
where.WithMaxDepth(3),
where.WithMaxINItems(2),
where.WithFunctions("LOWER", "UPPER"),
)
require.NoError(t, err)
// Should pass all constraints
filter, err := parser.Parse("LOWER(name) IN ('john', 'jane')")
require.NoError(t, err)
require.NotNil(t, filter)
// Test that options are applied - exact validation behavior may vary
// but the parser should be configured with the options
require.NotNil(t, parser)
})
t.Run("options order doesn't matter", func(t *testing.T) {
parser1, err1 := where.NewParser(
where.WithMaxDepth(2),
where.WithFunctions("LOWER"),
)
require.NoError(t, err1)
parser2, err2 := where.NewParser(
where.WithFunctions("LOWER"),
where.WithMaxDepth(2),
)
require.NoError(t, err2)
// Both should behave the same
input := "LOWER(name) = 'test'"
filter1, err1 := parser1.Parse(input)
require.NoError(t, err1)
filter2, err2 := parser2.Parse(input)
require.NoError(t, err2)
// Both should produce valid filters
require.NotNil(t, filter1)
require.NotNil(t, filter2)
})
}
func TestParserEdgeCases(t *testing.T) {
t.Run("zero max depth", func(t *testing.T) {
parser, err := where.NewParser(where.WithMaxDepth(0))
require.NoError(t, err)
require.NotNil(t, parser)
// Parser is created successfully with zero depth
// Exact validation behavior depends on implementation
})
t.Run("zero max IN items", func(t *testing.T) {
parser, err := where.NewParser(where.WithMaxINItems(0))
require.NoError(t, err)
// Any IN expression should fail
_, err = parser.Parse("id IN (1)")
require.Error(t, err)
require.Contains(t, err.Error(), "exceeds maximum")
})
t.Run("empty functions list", func(t *testing.T) {
parser, err := where.NewParser(where.WithFunctions())
require.NoError(t, err)
// Non-function expressions should work
filter, err := parser.Parse("age > 18")
require.NoError(t, err)
require.NotNil(t, filter)
// Function expressions should fail
_, err = parser.Parse("LOWER(name) = 'test'")
require.Error(t, err)
require.Contains(t, err.Error(), "not allowed")
})
}
func TestParseFunctionGlobal(t *testing.T) {
// Test the global Parse function
t.Run("global Parse function", func(t *testing.T) {
filter, err := where.Parse("age > 18 AND status = 'active'")
require.NoError(t, err)
require.NotNil(t, filter)
require.NotNil(t, filter.Expression)
// Should be able to convert to SQL
sql, args, err := filter.ToSQL("postgres")
require.NoError(t, err)
require.Equal(t, "(age > $1 AND status = $2)", sql)
require.Equal(t, []any{float64(18), "active"}, args)
})
t.Run("global Parse with complex expression", func(t *testing.T) {
filter, err := where.Parse("(age BETWEEN 18 AND 65) AND (status IN ('active', 'premium')) AND email LIKE '%@gmail.com'")
require.NoError(t, err)
require.NotNil(t, filter)
sql, args, err := filter.ToSQL("postgres")
require.NoError(t, err)
require.Contains(t, sql, "BETWEEN")
require.Contains(t, sql, "IN")
require.Contains(t, sql, "LIKE")
require.Len(t, args, 5) // 18, 65, 'active', 'premium', '%@gmail.com'
})
t.Run("global Parse error handling", func(t *testing.T) {
filter, err := where.Parse("")
require.Error(t, err)
require.Nil(t, filter)
require.Contains(t, err.Error(), "empty filter expression")
filter, err = where.Parse("invalid >> syntax")
require.Error(t, err)
require.Nil(t, filter)
})
}