-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecursion_depth_test.go
More file actions
392 lines (348 loc) · 8.34 KB
/
recursion_depth_test.go
File metadata and controls
392 lines (348 loc) · 8.34 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package cel2sql
import (
"context"
"log/slog"
"os"
"testing"
"time"
"github.com/google/cel-go/cel"
"github.com/spandigital/cel2sql/v3/pg"
"github.com/stretchr/testify/require"
)
func TestMaxRecursionDepth(t *testing.T) {
tests := []struct {
name string
depth int
maxDepth int
shouldErr bool
}{
{
name: "within default limit",
depth: 50,
maxDepth: 0, // use default (100)
shouldErr: false,
},
{
name: "exceeds default limit",
depth: 150,
maxDepth: 0, // use default (100)
shouldErr: true,
},
{
name: "within custom limit",
depth: 20,
maxDepth: 100,
shouldErr: false,
},
{
name: "exceeds custom limit",
depth: 55,
maxDepth: 50,
shouldErr: true,
},
{
name: "very deep with high custom limit",
depth: 200,
maxDepth: 250,
shouldErr: false,
},
{
name: "shallow expression with low limit",
depth: 5,
maxDepth: 10,
shouldErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Build deeply nested expression: (((x + 1) + 1) + 1)...
expr := "x"
for i := 0; i < tt.depth; i++ {
expr = "(" + expr + " + 1)"
}
schema := pg.NewSchema([]pg.FieldSchema{{Name: "x", Type: "integer"}})
provider := pg.NewTypeProvider(map[string]pg.Schema{"vars": schema})
env, err := cel.NewEnv(
cel.CustomTypeProvider(provider),
cel.Variable("x", cel.IntType),
)
require.NoError(t, err)
ast, issues := env.Compile(expr)
require.NoError(t, issues.Err())
var opts []ConvertOption
if tt.maxDepth > 0 {
opts = append(opts, WithMaxDepth(tt.maxDepth))
}
_, err = Convert(ast, opts...)
if tt.shouldErr {
require.Error(t, err)
require.Contains(t, err.Error(), "recursion depth")
} else {
require.NoError(t, err)
}
})
}
}
func TestMaxDepthWithOtherOptions(t *testing.T) {
// Test combining WithMaxDepth with other options
schema := pg.NewSchema([]pg.FieldSchema{{Name: "x", Type: "integer"}})
provider := pg.NewTypeProvider(map[string]pg.Schema{"vars": schema})
schemas := provider.GetSchemas()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
env, err := cel.NewEnv(
cel.CustomTypeProvider(provider),
cel.Variable("x", cel.IntType),
)
require.NoError(t, err)
tests := []struct {
name string
expr string
opts []ConvertOption
}{
{
name: "all options together",
expr: "((x + 1) + 2) + 3",
opts: []ConvertOption{
WithMaxDepth(50),
WithContext(ctx),
WithSchemas(schemas),
WithLogger(logger),
},
},
{
name: "maxDepth with context",
expr: "(x + 1) + 2",
opts: []ConvertOption{
WithMaxDepth(100),
WithContext(ctx),
},
},
{
name: "maxDepth with schemas",
expr: "x > 5",
opts: []ConvertOption{
WithMaxDepth(75),
WithSchemas(schemas),
},
},
{
name: "maxDepth with logger",
expr: "x == 10",
opts: []ConvertOption{
WithMaxDepth(150),
WithLogger(logger),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ast, issues := env.Compile(tt.expr)
require.NoError(t, issues.Err())
sql, err := Convert(ast, tt.opts...)
require.NoError(t, err)
require.NotEmpty(t, sql)
})
}
}
func TestRecursionDepthErrorMessage(t *testing.T) {
// Verify error message format
expr := "x"
for range 150 {
expr = "(" + expr + " + 1)"
}
schema := pg.NewSchema([]pg.FieldSchema{{Name: "x", Type: "integer"}})
provider := pg.NewTypeProvider(map[string]pg.Schema{"vars": schema})
env, err := cel.NewEnv(
cel.CustomTypeProvider(provider),
cel.Variable("x", cel.IntType),
)
require.NoError(t, err)
ast, issues := env.Compile(expr)
require.NoError(t, issues.Err())
// Test default error message
_, err = Convert(ast)
require.Error(t, err)
require.Contains(t, err.Error(), "exceeds limit of 100")
// Test custom depth error message
_, err = Convert(ast, WithMaxDepth(50))
require.Error(t, err)
require.Contains(t, err.Error(), "exceeds limit of 50")
}
func TestDeeplyNestedExpressions(t *testing.T) {
tests := []struct {
name string
buildExpr func() string
maxDepth int
shouldErr bool
}{
{
name: "nested AND conditions",
buildExpr: func() string {
expr := "x > 0"
for range 60 {
expr = "(" + expr + " && x < 100)"
}
return expr
},
maxDepth: 0, // default (100)
shouldErr: false,
},
{
name: "nested OR conditions",
buildExpr: func() string {
expr := "x == 1"
for range 60 {
expr = "(" + expr + " || x == 2)"
}
return expr
},
maxDepth: 0, // default (100)
shouldErr: false,
},
{
name: "nested function calls",
buildExpr: func() string {
expr := "x"
for range 60 {
expr = "int(" + expr + ")"
}
return expr + " > 0"
},
maxDepth: 0, // default (100)
shouldErr: false,
},
{
name: "deeply nested ternary",
buildExpr: func() string {
expr := "x"
for range 25 {
expr = "(" + expr + " > 0 ? 1 : 0)"
}
return expr + " == 1"
},
maxDepth: 0, // default (100)
shouldErr: false,
},
{
name: "exceeds limit nested arithmetic",
buildExpr: func() string {
expr := "x"
for range 150 {
expr = "(" + expr + " + 1)"
}
return expr + " > 0"
},
maxDepth: 0, // default (100)
shouldErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
schema := pg.NewSchema([]pg.FieldSchema{{Name: "x", Type: "integer"}})
provider := pg.NewTypeProvider(map[string]pg.Schema{"vars": schema})
env, err := cel.NewEnv(
cel.CustomTypeProvider(provider),
cel.Variable("x", cel.IntType),
)
require.NoError(t, err)
expr := tt.buildExpr()
ast, issues := env.Compile(expr)
require.NoError(t, issues.Err())
var opts []ConvertOption
if tt.maxDepth > 0 {
opts = append(opts, WithMaxDepth(tt.maxDepth))
}
_, err = Convert(ast, opts...)
if tt.shouldErr {
require.Error(t, err)
require.Contains(t, err.Error(), "recursion depth")
} else {
require.NoError(t, err)
}
})
}
}
func TestDepthResetBetweenCalls(t *testing.T) {
// Ensure depth counter resets between Convert() calls
schema := pg.NewSchema([]pg.FieldSchema{{Name: "x", Type: "integer"}})
provider := pg.NewTypeProvider(map[string]pg.Schema{"vars": schema})
env, err := cel.NewEnv(
cel.CustomTypeProvider(provider),
cel.Variable("x", cel.IntType),
)
require.NoError(t, err)
// Build an expression at 75% of default limit
expr := "x"
for range 75 {
expr = "(" + expr + " + 1)"
}
ast, issues := env.Compile(expr)
require.NoError(t, issues.Err())
// First conversion should succeed
_, err = Convert(ast)
require.NoError(t, err)
// Second conversion should also succeed (depth was reset)
_, err = Convert(ast)
require.NoError(t, err)
// Third conversion should also succeed
_, err = Convert(ast)
require.NoError(t, err)
}
func TestMaxDepthWithComprehensions(t *testing.T) {
// Test that comprehensions are counted in recursion depth
tests := []struct {
name string
expr string
maxDepth int
shouldErr bool
}{
{
name: "simple all comprehension",
expr: "[1, 2, 3].all(x, x > 0)",
maxDepth: 0, // default
shouldErr: false,
},
{
name: "simple exists comprehension",
expr: "[1, 2, 3].exists(x, x == 2)",
maxDepth: 0, // default
shouldErr: false,
},
{
name: "nested comprehension in condition",
expr: "x > 0 && [1, 2, 3].all(y, y < 10)",
maxDepth: 0, // default
shouldErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
schema := pg.NewSchema([]pg.FieldSchema{
{Name: "x", Type: "integer"},
})
provider := pg.NewTypeProvider(map[string]pg.Schema{"vars": schema})
env, err := cel.NewEnv(
cel.CustomTypeProvider(provider),
cel.Variable("x", cel.IntType),
)
require.NoError(t, err)
ast, issues := env.Compile(tt.expr)
require.NoError(t, issues.Err())
var opts []ConvertOption
if tt.maxDepth > 0 {
opts = append(opts, WithMaxDepth(tt.maxDepth))
}
_, err = Convert(ast, opts...)
if tt.shouldErr {
require.Error(t, err)
require.Contains(t, err.Error(), "recursion depth")
} else {
require.NoError(t, err)
}
})
}
}