-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathSimpleQueryParserJavaTest.js
More file actions
606 lines (570 loc) · 23.5 KB
/
SimpleQueryParserJavaTest.js
File metadata and controls
606 lines (570 loc) · 23.5 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
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/**
* @license
* Copyright 2026 The FOAM Authors. All Rights Reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*/
foam.CLASS({
package: 'foam.parse.test',
name: 'SimpleQueryParserJavaTest',
extends: 'foam.core.test.Test',
javaImports: [
'foam.parse.SimpleQueryParser',
'foam.mlang.predicate.Predicate',
'foam.core.auth.User',
'java.util.Calendar',
'java.util.Date',
'java.util.TimeZone'
],
documentation: 'Comprehensive Java tests for SimpleQueryParser mirroring the JS test suite',
methods: [
{
name: 'runTest',
javaCode: `
testStringProperties();
testStringArrayProperties();
testFloatFObjectProperties();
testDateProperties();
testInvalidDateFormats();
testCombinedDateProperties();
testNumberProperties();
testNumberCombinedProperties();
testEnumProperties();
testBooleanProperties();
testParentheses();
`
},
{
name: 'buildPredicate',
type: 'String',
args: [ { name: 'query', type: 'String' } ],
javaCode: `
SimpleQueryParser parser = new SimpleQueryParser(User.getOwnClassInfo());
Predicate pred = parser.parseString(query);
if ( pred == null ) return null;
return pred.toString().trim();
`
},
{
name: 'assertQuery',
args: [
{ name: 'query', type: 'String' },
{ name: 'expected', type: 'String' },
{ name: 'message', type: 'String' }
],
javaCode: `
String result = buildPredicate(query);
if ( result == null ) {
test(false, message + " — got null");
return;
}
test(
result.toLowerCase().equals(expected.toLowerCase()),
message + " — expected: " + expected + ", got: " + result
);
`
},
{
name: 'assertQueryContains',
args: [
{ name: 'query', type: 'String' },
{ name: 'fragment', type: 'String' },
{ name: 'message', type: 'String' }
],
javaCode: `
String result = buildPredicate(query);
if ( result == null ) {
test(false, message + " — got null");
return;
}
test(
result.toLowerCase().contains(fragment.toLowerCase()),
message + " — expected to contain: " + fragment + ", got: " + result
);
`
},
{
name: 'assertQueryNull',
args: [
{ name: 'query', type: 'String' },
{ name: 'message', type: 'String' }
],
javaCode: `
String result = buildPredicate(query);
test(result == null, message + " — expected null, got: " + result);
`
},
// ───────── String property tests (matches JS Test8-Test15) ─────────
{
name: 'testStringProperties',
javaCode: `
// JS Test8: The name equal to the value
assertQuery(
"firstName = SomeName",
"Eq(foam.core.auth.User.firstName, SomeName)",
"String Test8: The name equal to the value"
);
// JS Test9: The name not equal to the value
assertQuery(
"firstName!=SomeName",
"Neq(foam.core.auth.User.firstName, SomeName)",
"String Test9: The name not equal to the value"
);
// JS Test10: The name contains the value (CONTAINS)
assertQuery(
"firstName CONTAINS SomeName",
"ContainsIC(foam.core.auth.User.firstName, SomeName)",
"String Test10: The name contains the value"
);
// JS Test10b: The name contains the value with : operator
assertQuery(
"firstName:SomeName",
"ContainsIC(foam.core.auth.User.firstName, SomeName)",
"String Test10b: The name contains the value with : operator"
);
// JS Test11: The name contains the value with ~ operator
assertQuery(
"firstName~SomeName",
"ContainsIC(foam.core.auth.User.firstName, SomeName)",
"String Test11: The name contains the value with ~ operator"
);
// JS Test12: The name exactly matches any of the listed values
assertQuery(
"firstName IN (SomeName,AnotherName)",
"In(foam.core.auth.User.firstName, len: 2,SomeName,AnotherName)",
"String Test12: The name exactly matches any of the listed values"
);
// JS Test13: The name does not exactly match any of the listed values
assertQuery(
"firstName NOT IN (SomeName,AnotherName)",
"Not(In(foam.core.auth.User.firstName, len: 2,SomeName,AnotherName))",
"String Test13: The name does not exactly match any of the listed values"
);
// JS Test14: The name is empty (Has predicate toString lacks arg info)
assertQueryContains(
"firstName IS EMPTY",
"not(",
"String Test14: The name is empty wraps in Not"
);
// JS Test15: The name is not empty
{
String isNotEmpty = buildPredicate("firstName IS NOT EMPTY");
test(isNotEmpty != null, "String Test15: The name is not empty parses");
test(
! isNotEmpty.toLowerCase().contains("not("),
"String Test15: The name is not empty is not negated — got: " + isNotEmpty
);
}
`
},
// ───────── StringArray property tests (matches JS StringArray Test1-Test5) ─────────
{
name: 'testStringArrayProperties',
javaCode: `
// JS StringArray Test1
assertQuery(
"disabledTopics IN (tag1,tag2,tag3)",
"In(foam.core.auth.User.disabledTopics, len: 3,tag1,tag2,tag3)",
"StringArray Test1: The disabledTopics exactly matches any of the listed values"
);
// JS StringArray Test2
assertQuery(
"disabledTopics NOT IN (tag1,tag2,tag3)",
"Not(In(foam.core.auth.User.disabledTopics, len: 3,tag1,tag2,tag3))",
"StringArray Test2: The disabledTopics does not exactly match any of the listed values"
);
// JS StringArray Test3
assertQuery(
"disabledTopics = tag1",
"In(foam.core.auth.User.disabledTopics, [tag1])",
"StringArray Test3: The disabledTopics exactly matches one value"
);
// JS StringArray Test4
assertQuery(
"disabledTopics HAS tag1",
"In(foam.core.auth.User.disabledTopics, [tag1])",
"StringArray Test4: The disabledTopics HAS one value"
);
// JS StringArray Test5
assertQuery(
"disabledTopics != tag1",
"Not(In(foam.core.auth.User.disabledTopics, [tag1]))",
"StringArray Test5: The disabledTopics does not exactly match one value"
);
`
},
// ───────── Float + FObjectProperty tests (matches JS Float Test5-Test12) ─────────
{
name: 'testFloatFObjectProperties',
javaCode: `
// Float Test5: Inner property equal (produces AND(GTE, LT) range with epsilon)
String ft5 = buildPredicate("address.longitude = 6.5");
test(ft5 != null, "Float Test5: Inner property equal parses");
test(
ft5.toLowerCase().contains("gte(") && ft5.toLowerCase().contains("lt(") &&
ft5.toLowerCase().contains("longitude"),
"Float Test5: Inner property equal produces range — got: " + ft5
);
// Float Test6: Inner property not equal (produces OR(GTE, LT) inverse range)
String ft6 = buildPredicate("address.longitude!=6.5");
test(ft6 != null, "Float Test6: Inner property not equal parses");
test(
ft6.toLowerCase().contains("or(") &&
ft6.toLowerCase().contains("gte(") && ft6.toLowerCase().contains("lt(") &&
ft6.toLowerCase().contains("longitude"),
"Float Test6: Inner property not equal produces OR range — got: " + ft6
);
// Float Test7: Inner property greater than
String ft7 = buildPredicate("address.longitude > 6.5");
test(ft7 != null, "Float Test7: Inner property greater than parses");
test(
ft7.toLowerCase().contains("gt(") &&
ft7.toLowerCase().contains("longitude"),
"Float Test7: Inner property greater than — got: " + ft7
);
// Float Test8: Inner property greater than or equal
String ft8 = buildPredicate("address.longitude>=6.5");
test(ft8 != null, "Float Test8: Inner property greater than or equal parses");
test(
ft8.toLowerCase().contains("gte(") &&
ft8.toLowerCase().contains("longitude"),
"Float Test8: Inner property greater than or equal — got: " + ft8
);
// Float Test10: Inner property less than or equal
String ft10 = buildPredicate("address.longitude <= 6.5");
test(ft10 != null, "Float Test10: Inner property less than or equal parses");
test(
ft10.toLowerCase().contains("lte(") &&
ft10.toLowerCase().contains("longitude"),
"Float Test10: Inner property less than or equal — got: " + ft10
);
// Float Test11: Inner property is within range
String ft11 = buildPredicate("address.latitude IN RANGE (6.5, 8.5)");
test(ft11 != null, "Float Test11: Inner property IN RANGE parses");
test(
ft11.toLowerCase().contains("gte(") && ft11.toLowerCase().contains("lt(") &&
ft11.toLowerCase().contains("latitude"),
"Float Test11: Inner property IN RANGE produces GTE+LT — got: " + ft11
);
// Float Test12: Inner property is not within range
String ft12 = buildPredicate("address.latitude NOT IN RANGE (6.5, 8.5)");
test(ft12 != null, "Float Test12: Inner property NOT IN RANGE parses");
test(
ft12.toLowerCase().contains("or(") &&
ft12.toLowerCase().contains("gte(") && ft12.toLowerCase().contains("lt(") &&
ft12.toLowerCase().contains("latitude"),
"Float Test12: Inner property NOT IN RANGE produces OR — got: " + ft12
);
// Float Test13: Small float IN RANGE preserves decimal precision
String ft13 = buildPredicate("address.latitude IN RANGE (-0.0001, 0.0001)");
test(ft13 != null, "Float Test13: Small float IN RANGE (-0.0001, 0.0001) parses");
test(
ft13.contains("-1.000001E-4") || ft13.contains("-0.0001000001") || ft13.contains("-1.0001E-4"),
"Float Test13: Small float IN RANGE lower bound preserves magnitude — got: " + ft13
);
test(
! ft13.contains("0.1") || ft13.contains("0.0001"),
"Float Test13: Small float IN RANGE does NOT corrupt to 0.1 — got: " + ft13
);
// Float Test14: Small float equality preserves decimal precision
String ft14 = buildPredicate("address.longitude = 0.001");
test(ft14 != null, "Float Test14: Small float = 0.001 parses");
test(
! ft14.contains("0.1,") && ! ft14.contains(", 0.1"),
"Float Test14: Small float = 0.001 does NOT corrupt to 0.1 — got: " + ft14
);
// Float Test15: Small float NOT IN RANGE preserves precision
String ft15 = buildPredicate("address.latitude NOT IN RANGE (-0.0001, 0.0001)");
test(ft15 != null, "Float Test15: Small float NOT IN RANGE parses");
test(
ft15.toLowerCase().contains("or(") &&
ft15.toLowerCase().contains("gte(") && ft15.toLowerCase().contains("lt("),
"Float Test15: Small float NOT IN RANGE produces OR(GTE,LT) — got: " + ft15
);
`
},
// ───────── Date property tests (matches JS Date Test11-Test18) ─────────
{
name: 'testDateProperties',
javaCode: `
// Date Test11: Date equality produces AND(GTE start, LT end)
String dt11 = buildPredicate("created=2025-01-01");
test(dt11 != null, "Date Test11: Date equality parses");
test(
dt11.toLowerCase().contains("gte(foam.core.auth.user.created,") &&
dt11.toLowerCase().contains("lt(foam.core.auth.user.created,"),
"Date Test11: Date equality produces range (GTE + LT) — got: " + dt11
);
// Date Test12: Date equality with spaces
String dt12 = buildPredicate("created = 2025-05-31");
test(dt12 != null, "Date Test12: Date equality with spaces parses");
test(
dt12.toLowerCase().contains("gte(foam.core.auth.user.created,") &&
dt12.toLowerCase().contains("lt(foam.core.auth.user.created,"),
"Date Test12: Date equality with spaces produces range — got: " + dt12
);
// Date Test13: Relative date > TODAY-7
String dt13 = buildPredicate("birthday > TODAY-7");
test(dt13 != null, "Date Test13: Relative date > TODAY-7 parses");
test(
dt13.toLowerCase().contains("gt(foam.core.auth.user.birthday,"),
"Date Test13: Relative date produces GT — got: " + dt13
);
// Date Test14: Relative date <= TODAY+30
String dt14 = buildPredicate("birthday <= TODAY+30");
test(dt14 != null, "Date Test14: Relative date <= TODAY+30 parses");
test(
dt14.toLowerCase().contains("lte(foam.core.auth.user.birthday,"),
"Date Test14: Relative date produces LTE — got: " + dt14
);
// Date Test15: Date IN RANGE
String dt15 = buildPredicate("birthday IN RANGE (2025-03-31, 2025-04-30)");
test(dt15 != null, "Date Test15: Date IN RANGE parses");
test(
dt15.toLowerCase().contains("gte(foam.core.auth.user.birthday,") &&
dt15.toLowerCase().contains("lt(foam.core.auth.user.birthday,"),
"Date Test15: Date IN RANGE produces GTE+LT — got: " + dt15
);
// Date Test16: Date NOT IN RANGE
String dt16 = buildPredicate("birthday NOT IN RANGE (2025-03-31, 2025-04-30)");
test(dt16 != null, "Date Test16: Date NOT IN RANGE parses");
test(
dt16.toLowerCase().contains("gte(foam.core.auth.user.birthday,") &&
dt16.toLowerCase().contains("lt(foam.core.auth.user.birthday,"),
"Date Test16: Date NOT IN RANGE produces OR(GTE,LT) — got: " + dt16
);
// Date Test17: Date IS EMPTY
assertQueryContains(
"lastLogin IS EMPTY",
"not(",
"Date Test17: Date is empty wraps in Not"
);
// Date Test18: Date IS NOT EMPTY
{
String dt18 = buildPredicate("lastLogin IS NOT EMPTY");
test(dt18 != null, "Date Test18: Date is not empty parses");
test(
! dt18.toLowerCase().contains("not("),
"Date Test18: Date is not empty is not negated — got: " + dt18
);
}
`
},
// ───────── Invalid date format tests (matches JS Date Test19-Test21) ─────────
// NOTE: The JS test uses symbol-level parsing (parseString('2025.01.15', 'date'))
// which rejects dots/commas. The Java parser's full query parsing is more lenient:
// 'created=2025.01.15' consumes '2025' as a valid year-only date.
// These tests verify the Java parser handles these gracefully (no crash).
{
name: 'testInvalidDateFormats',
javaCode: `
// Date Test19: Date with dots - Java parser consumes year portion
// JS symbol test rejects this, but Java full query gracefully parses year-only
{
String dt19 = buildPredicate("created=2025.01.15");
test(dt19 != null, "Date Test19: Date with dots (2025.01.15) does not crash");
}
// Date Test20: Date with commas - same behavior
{
String dt20 = buildPredicate("created=2025,01,15");
test(dt20 != null, "Date Test20: Date with commas (2025,01,15) does not crash");
}
// Date Test21: Short date with dots - same behavior
{
String dt21 = buildPredicate("created=25.01.15");
test(dt21 != null, "Date Test21: Short date with dots (25.01.15) does not crash");
}
`
},
// ───────── Combined date tests (matches JS Date Test22-Test23) ─────────
{
name: 'testCombinedDateProperties',
javaCode: `
// Date Test22: Date AND query
String dt22 = buildPredicate("birthday IN RANGE (2025-03-31, 2025-04-30) AND lastLogin IS EMPTY");
test(dt22 != null, "Date Test22: Date AND query parses");
test(
dt22.toLowerCase().contains("gte(foam.core.auth.user.birthday,") &&
dt22.toLowerCase().contains("not("),
"Date Test22: Date AND combines range + IS EMPTY — got: " + dt22
);
// Date Test23: Date OR query
String dt23 = buildPredicate("birthday NOT IN RANGE (2025-03-31, 2025-04-30) OR lastLogin IS NOT EMPTY");
test(dt23 != null, "Date Test23: Date OR query parses");
test(
dt23.toLowerCase().contains("or("),
"Date Test23: Date OR includes OR — got: " + dt23
);
`
},
// ───────── Number property tests (matches JS Number Test7-Test14) ─────────
{
name: 'testNumberProperties',
javaCode: `
// Number Test7: Equal
assertQuery(
"id = 6",
"Eq(foam.core.auth.User.id, 6)",
"Number Test7: The id equal to the value"
);
// Number Test8: Not equal
assertQuery(
"id!=6",
"Neq(foam.core.auth.User.id, 6)",
"Number Test8: The id not equal to the value"
);
// Number Test9: Greater than
assertQuery(
"id>6",
"Gt(foam.core.auth.User.id, 6)",
"Number Test9: The id greater than the value"
);
// Number Test10: Greater than or equal
assertQuery(
"id>=6",
"Gte(foam.core.auth.User.id, 6)",
"Number Test10: The id greater than or equal to the value"
);
// Number Test11: Less than
assertQuery(
"id<6",
"Lt(foam.core.auth.User.id, 6)",
"Number Test11: The id less than the value"
);
// Number Test12: Less than or equal
assertQuery(
"id<=6",
"Lte(foam.core.auth.User.id, 6)",
"Number Test12: The id less than or equal to the value"
);
// Number Test13: IN list
assertQuery(
"id IN (6,7,8)",
"In(foam.core.auth.User.id, len: 3,6,7,8)",
"Number Test13: The id exactly matches any of the listed values"
);
// Number Test14: NOT IN list
assertQuery(
"id NOT IN (6,7,8)",
"Not(In(foam.core.auth.User.id, len: 3,6,7,8))",
"Number Test14: The id does not exactly match any of the listed values"
);
`
},
// ───────── Number combined tests (matches JS Number Test15-Test17) ─────────
{
name: 'testNumberCombinedProperties',
javaCode: `
// Number Test15: Contradictory query (id=16 AND id<9)
// JS partialEval simplifies to False; Java And.partialEval() preserves the expression
{
String nt15 = buildPredicate("id=16 AND id<9");
test(nt15 != null, "Number Test15: Contradictory AND query parses");
test(
nt15.toLowerCase().contains("eq(") && nt15.toLowerCase().contains("lt("),
"Number Test15: Contradictory AND contains both Eq and Lt — got: " + nt15
);
}
// Number Test16: Greater than AND less than
assertQuery(
"id>9 AND id<16",
"And(Gt(foam.core.auth.User.id, 9), Lt(foam.core.auth.User.id, 16))",
"Number Test16: The id greater than a value and less than another value"
);
// Number Test17: Equal OR less than
assertQuery(
"id=18 OR id<9",
"Or(Eq(foam.core.auth.User.id, 18), Lt(foam.core.auth.User.id, 9))",
"Number Test17: The id equal to the value or less than another value"
);
`
},
// ───────── Enum property tests (matches JS Enum Test1-Test4) ─────────
{
name: 'testEnumProperties',
javaCode: `
// Enum Test1: Equal
assertQuery(
"lifecycleState= ACTIVE",
"Eq(foam.core.auth.User.lifecycleState, ACTIVE)",
"Enum Test1: The status equal to the value"
);
// Enum Test2: Not equal
assertQuery(
"lifecycleState!=ACTIVE",
"Neq(foam.core.auth.User.lifecycleState, ACTIVE)",
"Enum Test2: The status not equal to the value"
);
// Enum Test3: IN list
assertQuery(
"lifecycleState IN (ACTIVE,REJECTED)",
"In(foam.core.auth.User.lifecycleState, len: 2,ACTIVE,REJECTED)",
"Enum Test3: The status exactly matches any of the listed values"
);
// Enum Test4: NOT IN list
assertQuery(
"lifecycleState NOT IN ( ACTIVE, REJECTED )",
"Not(In(foam.core.auth.User.lifecycleState, len: 2,ACTIVE,REJECTED))",
"Enum Test4: The status does not exactly match any of the listed values"
);
`
},
// ───────── Boolean property tests (matches JS Boolean Test1-Test2) ─────────
{
name: 'testBooleanProperties',
javaCode: `
// Boolean Test1: IS TRUE
assertQuery(
"loginEnabled IS TRUE",
"Eq(foam.core.auth.User.loginEnabled, true)",
"Boolean Test1: The enabled is true"
);
// Boolean Test2: IS FALSE
assertQuery(
"loginEnabled IS FALSE",
"Eq(foam.core.auth.User.loginEnabled, false)",
"Boolean Test2: The enabled is false"
);
`
},
// ───────── Parentheses tests (matches JS Parentheses Test1-Test5) ─────────
{
name: 'testParentheses',
javaCode: `
// Parentheses Test1: Simple parentheses
assertQuery(
"( id = 6 )",
"Eq(foam.core.auth.User.id, 6)",
"Parentheses Test1: The id equal to the value with parentheses"
);
// Parentheses Test2: Contradictory query in parentheses (id=17 AND id<9)
// JS partialEval simplifies to False; Java And.partialEval() preserves the expression
{
String pt2 = buildPredicate("(id=17 AND id<9)");
test(pt2 != null, "Parentheses Test2: Contradictory AND in parens parses");
test(
pt2.toLowerCase().contains("eq(") && pt2.toLowerCase().contains("lt("),
"Parentheses Test2: Contradictory AND in parens contains Eq and Lt — got: " + pt2
);
}
// Parentheses Test3: AND in parentheses
assertQuery(
"(id>9 AND id<17)",
"And(Gt(foam.core.auth.User.id, 9), Lt(foam.core.auth.User.id, 17))",
"Parentheses Test3: The id greater than a value and less than another value with parentheses"
);
// Parentheses Test4: OR in parentheses
assertQuery(
"(id=18 OR id<10)",
"Or(Eq(foam.core.auth.User.id, 18), Lt(foam.core.auth.User.id, 10))",
"Parentheses Test4: The id equal to the value or less than another value with parentheses"
);
// Parentheses Test5: NOT with AND
assertQuery(
"NOT id=17 AND loginEnabled IS TRUE",
"And(Neq(foam.core.auth.User.id, 17), Eq(foam.core.auth.User.loginEnabled, true))",
"Parentheses Test5: Negate the id equal to the value and login enabled is true"
);
`
}
]
});