-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-search-tests.js
537 lines (535 loc) · 21.3 KB
/
advanced-search-tests.js
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
(function(global, $) {
const AdvancedSearchTests = {
testCases: [
{
id: 1,
name: 'Test Case 1: User ID and Username',
query: 'id = 1 AND username = "john_doe"',
expected: {
filters: [
{ property: "id", operator: "=", value: "1", andor: null },
{ property: "username", operator: "=", value: "john_doe", andor: "and" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 2,
name: 'Test Case 2: User Email or Created At',
query: 'email = "[email protected]" OR created_at >= "2023-01-01"',
expected: {
filters: [
{ property: "email", operator: "=", value: "[email protected]", andor: null },
{ property: "created_at", operator: ">=", value: "2023-01-01", andor: "or" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 3,
name: 'Test Case 3: Post Title and Content',
query: 'title = "My First Post" AND content ~ "hello world"',
expected: {
filters: [
{ property: "title", operator: "=", value: "My First Post", andor: null },
{ property: "content", operator: "~", value: "hello world", andor: "and" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 4,
name: 'Test Case 4: Post Title, Content or Created At',
query: 'title = "My First Post" OR content ~ "hello world" OR created_at >= "2023-01-01"',
expected: {
filters: [
{ property: "title", operator: "=", value: "My First Post", andor: null },
{ property: "content", operator: "~", value: "hello world", andor: "or" },
{ property: "created_at", operator: ">=", value: "2023-01-01", andor: "or" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 5,
name: 'Test Case 5: Comment Author and Content',
query: 'author = "jane_doe" AND content ~ "great post"',
expected: {
filters: [
{ property: "author", operator: "=", value: "jane_doe", andor: null },
{ property: "content", operator: "~", value: "great post", andor: "and" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 6,
name: 'Test Case 6: Comment Post ID and Created At',
query: 'post_id = 1 AND created_at >= "2023-01-01"',
expected: {
filters: [
{ property: "post_id", operator: "=", value: "1", andor: null },
{ property: "created_at", operator: ">=", value: "2023-01-01", andor: "and" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 7,
name: 'Test Case 7: User Created At Descending',
query: 'ORDER BY created_at DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 8,
name: 'Test Case 8: User Limited and Offset',
query: 'LIMIT 5 OFFSET 10',
expected: {
filters: [],
sort: [],
limit: 5,
offset: 10
}
},
{
id: 9,
name: 'Test Case 9: Combined Query with Sorting and Pagination',
query: 'username = "john_doe" AND email = "[email protected]" ORDER BY created_at DESC LIMIT 5 OFFSET 10',
expected: {
filters: [
{ property: "username", operator: "=", value: "john_doe", andor: null },
{ property: "email", operator: "=", value: "[email protected]", andor: "and" }
],
sort: [
{ property: "created_at", direction: "DESC" }
],
limit: 5,
offset: 10
}
},
// Test Cases for Related Attributes
{
id: 10,
name: 'Test Case 10: Single Relation Attribute',
query: 'posts.created_at = "2023-01-01"',
expected: {
filters: [
{ property: "posts.created_at", operator: "=", value: "2023-01-01", andor: null }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 11,
name: 'Test Case 11: Multiple Relation Attributes',
query: 'posts.created_at = "2023-01-01" AND posts.comments.author = "jane_doe"',
expected: {
filters: [
{ property: "posts.created_at", operator: "=", value: "2023-01-01", andor: null },
{ property: "posts.comments.author", operator: "=", value: "jane_doe", andor: "and" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 12,
name: 'Test Case 12: Mixed Attributes',
query: 'username = "john_doe" AND posts.title = "My First Post"',
expected: {
filters: [
{ property: "username", operator: "=", value: "john_doe", andor: null },
{ property: "posts.title", operator: "=", value: "My First Post", andor: "and" }
],
sort: [],
limit: null,
offset: null
}
},
{
id: 13,
name: 'Test Case 13: Multiple Relations with Logical Operators',
query: 'posts.title = "My First Post" OR posts.comments.content ~ "great"',
expected: {
filters: [
{ property: "posts.title", operator: "=", value: "My First Post", andor: null },
{ property: "posts.comments.content", operator: "~", value: "great", andor: "or" }
],
sort: [],
limit: null,
offset: null
}
},
// Test Cases for Relation Attributes in ORDER BY
{
id: 14,
name: 'Test Case 14: Single Relation Attribute in ORDER BY',
query: 'ORDER BY posts.created_at DESC',
expected: {
filters: [],
sort: [
{ property: "posts.created_at", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 15,
name: 'Test Case 15: Multiple Relation Attributes in ORDER BY',
query: 'ORDER BY posts.created_at DESC, posts.comments.created_at ASC',
expected: {
filters: [],
sort: [
{ property: "posts.created_at", direction: "DESC" },
{ property: "posts.comments.created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 16,
name: 'Test Case 16: Combined Filters and Relation Attributes in ORDER BY',
query: 'username = "john_doe" AND ORDER BY posts.created_at DESC',
expected: {
filters: [
{ property: "username", operator: "=", value: "john_doe", andor: null }
],
sort: [
{ property: "posts.created_at", direction: "DESC" }
],
limit: null,
offset: null
}
},
// Extended Test Cases for ORDER BY Clauses
{
id: 17,
name: 'Test Case 17: ORDER BY created_at',
query: 'ORDER BY created_at',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 18,
name: 'Test Case 18: ORDER BY created_at ASC',
query: 'ORDER BY created_at ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 19,
name: 'Test Case 19: ORDER BY created_at DESC',
query: 'ORDER BY created_at DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 20,
name: 'Test Case 20: ORDER BY created_at, posts.created_at',
query: 'ORDER BY created_at, posts.created_at',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 21,
name: 'Test Case 21: ORDER BY created_at, posts.created_at ASC',
query: 'ORDER BY created_at, posts.created_at ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 22,
name: 'Test Case 22: ORDER BY created_at ASC, posts.created_at ASC',
query: 'ORDER BY created_at ASC, posts.created_at ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 23,
name: 'Test Case 23: ORDER BY created_at ASC, posts.created_at',
query: 'ORDER BY created_at ASC, posts.created_at',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 24,
name: 'Test Case 24: ORDER BY created_at ASC, posts.created_at DESC',
query: 'ORDER BY created_at ASC, posts.created_at DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 25,
name: 'Test Case 25: ORDER BY created_at DESC, posts.created_at ASC',
query: 'ORDER BY created_at DESC, posts.created_at ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 26,
name: 'Test Case 26: ORDER BY created_at DESC, posts.created_at DESC',
query: 'ORDER BY created_at DESC, posts.created_at DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 27,
name: 'Test Case 27: ORDER BY created_at DESC, posts.created_at DESC, title ASC',
query: 'ORDER BY created_at DESC, posts.created_at DESC, title ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "DESC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 28,
name: 'Test Case 28: ORDER BY created_at, posts.created_at DESC, title ASC',
query: 'ORDER BY created_at, posts.created_at DESC, title ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "DESC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 29,
name: 'Test Case 29: ORDER BY created_at DESC, posts.created_at, title ASC',
query: 'ORDER BY created_at DESC, posts.created_at, title ASC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "ASC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 30,
name: 'Test Case 30: ORDER BY created_at DESC, posts.created_at DESC, title',
query: 'ORDER BY created_at DESC, posts.created_at DESC, title',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "DESC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 31,
name: 'Test Case 31: ORDER BY created_at ASC, posts.created_at DESC, title',
query: 'ORDER BY created_at ASC, posts.created_at DESC, title',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "DESC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 32,
name: 'Test Case 32: ORDER BY created_at DESC, posts.created_at ASC, title',
query: 'ORDER BY created_at DESC, posts.created_at ASC, title',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "ASC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 33,
name: 'Test Case 33: ORDER BY created_at, posts.created_at ASC, title DESC',
query: 'ORDER BY created_at, posts.created_at ASC, title DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" },
{ property: "title", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 34,
name: 'Test Case 34: ORDER BY created_at ASC, posts.created_at ASC, title DESC',
query: 'ORDER BY created_at ASC, posts.created_at ASC, title DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" },
{ property: "title", direction: "DESC" }
],
limit: null,
offset: null
}
},
{
id: 35,
name: 'Test Case 35: ORDER BY created_at, posts.created_at, title',
query: 'ORDER BY created_at, posts.created_at, title',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "ASC" },
{ property: "posts.created_at", direction: "ASC" },
{ property: "title", direction: "ASC" }
],
limit: null,
offset: null
}
},
{
id: 36,
name: 'Test Case 36: ORDER BY created_at, posts.created_at, title DESC',
query: 'ORDER BY created_at DESC, posts.created_at DESC, title DESC',
expected: {
filters: [],
sort: [
{ property: "created_at", direction: "DESC" },
{ property: "posts.created_at", direction: "DESC" },
{ property: "title", direction: "DESC" }
],
limit: null,
offset: null
}
}
],
run(parseQuery, config) {
// Filter tests based on the config
const testsToRun = config.testIds && config.testIds.length > 0
? this.testCases.filter(test => config.testIds.includes(test.id))
: this.testCases;
testsToRun.forEach((test, index) => {
const result = parseQuery(test.query);
const passed = JSON.stringify(result) === JSON.stringify(test.expected);
console.log(`${test.name} (ID: ${test.id}): ${passed ? 'Passed' : 'Failed'}`);
if (!passed) {
console.log(`Test ID: ${test.id}`);
console.log('Expected:', JSON.stringify(test.expected, null, 2));
console.log('Received:', JSON.stringify(result, null, 2));
}
});
}
}
global.AdvancedSearchTests = AdvancedSearchTests;
}(window, jQuery));