@balena/abstract-sql-compiler 10.2.4-build-tests-ts-beb2503119ff2e479e1da25051d33a021129a9d7-1 → 10.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.versionbot/CHANGELOG.yml +1 -9
- package/CHANGELOG.md +0 -1
- package/package.json +2 -2
- package/test/odata/expand.js +78 -79
- package/test/odata/filterby.js +36 -36
- package/test/odata/orderby.js +8 -8
- package/test/odata/paging.js +3 -3
- package/test/odata/resource_parsing.js +38 -40
- package/test/odata/select.js +9 -9
- package/test/odata/stress.js +5 -6
- package/test/odata/test.js +138 -0
- package/test/odata/test.ts +0 -292
@@ -22,7 +22,7 @@ const teamFieldsStr = teamFields.join(', ');
|
|
22
22
|
test('/pilot', (result, sqlEquals) => {
|
23
23
|
it('should select from pilot', () => {
|
24
24
|
sqlEquals(
|
25
|
-
result,
|
25
|
+
result.query,
|
26
26
|
`\
|
27
27
|
SELECT ${pilotFieldsStr}
|
28
28
|
FROM "pilot"`,
|
@@ -33,7 +33,7 @@ FROM "pilot"`,
|
|
33
33
|
test('/pilot(1)', 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
34
34
|
it('should select from pilot with id', () => {
|
35
35
|
sqlEquals(
|
36
|
-
result,
|
36
|
+
result.query,
|
37
37
|
`\
|
38
38
|
SELECT ${pilotFieldsStr}
|
39
39
|
FROM "pilot"
|
@@ -45,7 +45,7 @@ WHERE ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
|
45
45
|
test("/pilot('TextKey')", 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
46
46
|
it('should select from pilot with id', () => {
|
47
47
|
sqlEquals(
|
48
|
-
result,
|
48
|
+
result.query,
|
49
49
|
`\
|
50
50
|
SELECT ${pilotFieldsStr}
|
51
51
|
FROM "pilot"
|
@@ -57,7 +57,7 @@ WHERE ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
|
57
57
|
test('/pilot(1)/licence', 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
58
58
|
it('should select from the licence of pilot with id', () => {
|
59
59
|
sqlEquals(
|
60
|
-
result,
|
60
|
+
result.query,
|
61
61
|
`\
|
62
62
|
SELECT ${aliasLicenceFieldsStr}
|
63
63
|
FROM "pilot",
|
@@ -71,7 +71,7 @@ AND ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
|
71
71
|
test('/licence(1)/is_of__pilot', 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
72
72
|
it('should select from the pilots of licence with id', () => {
|
73
73
|
sqlEquals(
|
74
|
-
result,
|
74
|
+
result.query,
|
75
75
|
`\
|
76
76
|
SELECT ${aliasPilotLicenceFieldsStr}
|
77
77
|
FROM "licence",
|
@@ -89,7 +89,7 @@ test(
|
|
89
89
|
(result, sqlEquals) => {
|
90
90
|
it('should select from the plane of pilot with id', () => {
|
91
91
|
sqlEquals(
|
92
|
-
result,
|
92
|
+
result.query,
|
93
93
|
`\
|
94
94
|
SELECT ${aliasPlaneFieldsStr}
|
95
95
|
FROM "pilot",
|
@@ -110,7 +110,7 @@ test(
|
|
110
110
|
(result, sqlEquals) => {
|
111
111
|
it('should select from the pilots of plane with id', () => {
|
112
112
|
sqlEquals(
|
113
|
-
result,
|
113
|
+
result.query,
|
114
114
|
`\
|
115
115
|
SELECT ${aliasPilotFields}
|
116
116
|
FROM "plane",
|
@@ -127,7 +127,7 @@ AND ("plane"."id") IS NOT NULL AND ("plane"."id") = (?)`,
|
|
127
127
|
test('/pilot(1)', 'DELETE', [['Bind', 0]], (result, sqlEquals) => {
|
128
128
|
it('should delete the pilot with id 1', () => {
|
129
129
|
sqlEquals(
|
130
|
-
result,
|
130
|
+
result.query,
|
131
131
|
`\
|
132
132
|
DELETE FROM "pilot"
|
133
133
|
WHERE ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
@@ -195,7 +195,7 @@ test(
|
|
195
195
|
(result, sqlEquals) => {
|
196
196
|
it('should insert/update the pilot with id 1', () => {
|
197
197
|
sqlEquals(
|
198
|
-
result,
|
198
|
+
result.query,
|
199
199
|
`\
|
200
200
|
INSERT INTO "pilot" ("name")
|
201
201
|
VALUES (?)`,
|
@@ -206,7 +206,7 @@ VALUES (?)`,
|
|
206
206
|
test('/pilot', 'POST', (result, sqlEquals) => {
|
207
207
|
it('should insert a pilot with default values', () => {
|
208
208
|
sqlEquals(
|
209
|
-
result,
|
209
|
+
result.query,
|
210
210
|
`\
|
211
211
|
INSERT INTO "pilot" DEFAULT VALUES`,
|
212
212
|
);
|
@@ -214,15 +214,14 @@ INSERT INTO "pilot" DEFAULT VALUES`,
|
|
214
214
|
});
|
215
215
|
|
216
216
|
(function () {
|
217
|
-
|
218
|
-
const bindings = /** @type {const} */ ([
|
217
|
+
const bindings = [
|
219
218
|
['Bind', ['pilot', 'is_experienced']],
|
220
219
|
['Bind', 0],
|
221
|
-
]
|
220
|
+
];
|
222
221
|
const testFunc = (result, sqlEquals) => {
|
223
222
|
it('should update the pilot with id 1', () => {
|
224
223
|
sqlEquals(
|
225
|
-
result,
|
224
|
+
result.query,
|
226
225
|
`\
|
227
226
|
UPDATE "pilot"
|
228
227
|
SET "is experienced" = ?
|
@@ -241,7 +240,7 @@ test(
|
|
241
240
|
(result, sqlEquals) => {
|
242
241
|
it('should delete the pilot with id 1', () => {
|
243
242
|
sqlEquals(
|
244
|
-
result,
|
243
|
+
result.query,
|
245
244
|
`\
|
246
245
|
DELETE FROM "pilot-can fly-plane"
|
247
246
|
WHERE ("pilot-can fly-plane"."id") IS NOT NULL AND ("pilot-can fly-plane"."id") = (?)`,
|
@@ -306,7 +305,7 @@ test(
|
|
306
305
|
(result, sqlEquals) => {
|
307
306
|
it('should insert/update the pilot-can fly-plane with id 1', () => {
|
308
307
|
sqlEquals(
|
309
|
-
result,
|
308
|
+
result.query,
|
310
309
|
`\
|
311
310
|
INSERT INTO "pilot-can fly-plane" ("pilot", "can fly-plane")
|
312
311
|
VALUES (?, ?)`,
|
@@ -317,7 +316,7 @@ VALUES (?, ?)`,
|
|
317
316
|
test('/pilot__can_fly__plane', 'POST', (result, sqlEquals) => {
|
318
317
|
it('should insert a "pilot-can fly-plane" with default values', () => {
|
319
318
|
sqlEquals(
|
320
|
-
result,
|
319
|
+
result.query,
|
321
320
|
`\
|
322
321
|
INSERT INTO "pilot-can fly-plane" DEFAULT VALUES`,
|
323
322
|
);
|
@@ -325,15 +324,14 @@ INSERT INTO "pilot-can fly-plane" DEFAULT VALUES`,
|
|
325
324
|
});
|
326
325
|
|
327
326
|
(function () {
|
328
|
-
|
329
|
-
const bindings = /** @type {const} */ ([
|
327
|
+
const bindings = [
|
330
328
|
['Bind', ['pilot-can fly-plane', 'pilot']],
|
331
329
|
['Bind', 0],
|
332
|
-
]
|
330
|
+
];
|
333
331
|
const testFunc = (result, sqlEquals) => {
|
334
332
|
it('should update the pilot with id 1', () => {
|
335
333
|
sqlEquals(
|
336
|
-
result,
|
334
|
+
result.query,
|
337
335
|
`\
|
338
336
|
UPDATE "pilot-can fly-plane"
|
339
337
|
SET "pilot" = ?
|
@@ -348,7 +346,7 @@ WHERE ("pilot-can fly-plane"."id") IS NOT NULL AND ("pilot-can fly-plane"."id")
|
|
348
346
|
test('/pilot(1)/$links/licence', 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
349
347
|
it('should select the list of licence ids, for generating the links', () => {
|
350
348
|
sqlEquals(
|
351
|
-
result,
|
349
|
+
result.query,
|
352
350
|
`\
|
353
351
|
SELECT "pilot"."licence" AS "licence"
|
354
352
|
FROM "pilot"
|
@@ -364,7 +362,7 @@ test(
|
|
364
362
|
(result, sqlEquals) => {
|
365
363
|
it('should select the list of plane ids, for generating the links', () => {
|
366
364
|
sqlEquals(
|
367
|
-
result,
|
365
|
+
result.query,
|
368
366
|
`\
|
369
367
|
SELECT "pilot.pilot-can fly-plane"."can fly-plane" AS "plane"
|
370
368
|
FROM "pilot",
|
@@ -376,18 +374,18 @@ AND ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
|
376
374
|
},
|
377
375
|
);
|
378
376
|
|
379
|
-
test.
|
377
|
+
test.skip('/pilot(1)/favourite_colour/red', () => {
|
380
378
|
it("should select the red component of the pilot's favourite colour");
|
381
379
|
});
|
382
380
|
|
383
|
-
test.
|
381
|
+
test.skip('/method(1)/child?foo=bar', () => {
|
384
382
|
it('should do something..');
|
385
383
|
});
|
386
384
|
|
387
385
|
test("/team('purple')", 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
388
386
|
it('should select the team with the "favourite colour" id of "purple"', () => {
|
389
387
|
sqlEquals(
|
390
|
-
result,
|
388
|
+
result.query,
|
391
389
|
`\
|
392
390
|
SELECT ${teamFieldsStr}
|
393
391
|
FROM "team"
|
@@ -404,7 +402,7 @@ test(
|
|
404
402
|
(result, sqlEquals) => {
|
405
403
|
it('should insert a team', () => {
|
406
404
|
sqlEquals(
|
407
|
-
result,
|
405
|
+
result.query,
|
408
406
|
`\
|
409
407
|
INSERT INTO "team" ("favourite colour")
|
410
408
|
VALUES (?)`,
|
@@ -413,7 +411,7 @@ VALUES (?)`,
|
|
413
411
|
},
|
414
412
|
);
|
415
413
|
|
416
|
-
test
|
414
|
+
test('/pilot/$count/$count', (result) => {
|
417
415
|
it('should fail because it is invalid', () => {
|
418
416
|
expect(result).to.be.instanceOf(ODataParser.SyntaxError);
|
419
417
|
});
|
@@ -422,7 +420,7 @@ test.fail('/pilot/$count/$count', (result) => {
|
|
422
420
|
test('/pilot/$count', (result, sqlEquals) => {
|
423
421
|
it('should select count(*) from pilot', () => {
|
424
422
|
sqlEquals(
|
425
|
-
result,
|
423
|
+
result.query,
|
426
424
|
`\
|
427
425
|
SELECT COUNT(*) AS "$count"
|
428
426
|
FROM "pilot"`,
|
@@ -430,13 +428,13 @@ FROM "pilot"`,
|
|
430
428
|
});
|
431
429
|
});
|
432
430
|
|
433
|
-
test
|
431
|
+
test('/pilot(5)/$count', (result) => {
|
434
432
|
it('should fail because it is invalid', () => {
|
435
433
|
expect(result).to.be.instanceOf(ODataParser.SyntaxError);
|
436
434
|
});
|
437
435
|
});
|
438
436
|
|
439
|
-
test
|
437
|
+
test('/pilot?$filter=id eq 5/$count', (result) => {
|
440
438
|
it('should fail because it is invalid', () => {
|
441
439
|
expect(result).to.be.instanceOf(ODataParser.SyntaxError);
|
442
440
|
});
|
@@ -449,7 +447,7 @@ test(
|
|
449
447
|
(result, sqlEquals) => {
|
450
448
|
it('should select count(*) from pilot where pilot/id > 5 ', () => {
|
451
449
|
sqlEquals(
|
452
|
-
result,
|
450
|
+
result.query,
|
453
451
|
`\
|
454
452
|
SELECT COUNT(*) AS "$count"
|
455
453
|
FROM "pilot"
|
@@ -469,7 +467,7 @@ test(
|
|
469
467
|
(result, sqlEquals) => {
|
470
468
|
it('should select count(*) from pilot where id in (5,10)', () => {
|
471
469
|
sqlEquals(
|
472
|
-
result,
|
470
|
+
result.query,
|
473
471
|
`\
|
474
472
|
SELECT COUNT(*) AS "$count"
|
475
473
|
FROM "pilot"
|
@@ -487,7 +485,7 @@ test(
|
|
487
485
|
(result, sqlEquals) => {
|
488
486
|
it('should select count(*) from pilot where id in (5,10)', () => {
|
489
487
|
sqlEquals(
|
490
|
-
result,
|
488
|
+
result.query,
|
491
489
|
`\
|
492
490
|
SELECT COUNT(*) AS "$count"
|
493
491
|
FROM "pilot"
|
@@ -501,7 +499,7 @@ OR "pilot"."id" IS NULL)`,
|
|
501
499
|
test('/pilot(5)/licence/$count', 'GET', [['Bind', 0]], (result, sqlEquals) => {
|
502
500
|
it('should select count(*) the licence from pilot where pilot/id', () => {
|
503
501
|
sqlEquals(
|
504
|
-
result,
|
502
|
+
result.query,
|
505
503
|
`\
|
506
504
|
SELECT COUNT(*) AS "$count"
|
507
505
|
FROM "pilot",
|
@@ -515,7 +513,7 @@ AND ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
|
515
513
|
test('/pilot/$count?$orderby=id asc', (result, sqlEquals) => {
|
516
514
|
it('should select count(*) from pilot and ignore orderby', () => {
|
517
515
|
sqlEquals(
|
518
|
-
result,
|
516
|
+
result.query,
|
519
517
|
`\
|
520
518
|
SELECT COUNT(*) AS "$count"
|
521
519
|
FROM "pilot"`,
|
@@ -526,7 +524,7 @@ FROM "pilot"`,
|
|
526
524
|
test('/pilot/$count?$skip=5', (result, sqlEquals) => {
|
527
525
|
it('should select count(*) from pilot and ignore skip', () => {
|
528
526
|
sqlEquals(
|
529
|
-
result,
|
527
|
+
result.query,
|
530
528
|
`\
|
531
529
|
SELECT COUNT(*) AS "$count"
|
532
530
|
FROM "pilot"`,
|
@@ -537,7 +535,7 @@ FROM "pilot"`,
|
|
537
535
|
test('/pilot/$count?$top=5', (result, sqlEquals) => {
|
538
536
|
it('should select count(*) from pilot and ignore top', () => {
|
539
537
|
sqlEquals(
|
540
|
-
result,
|
538
|
+
result.query,
|
541
539
|
`\
|
542
540
|
SELECT COUNT(*) AS "$count"
|
543
541
|
FROM "pilot"`,
|
@@ -548,7 +546,7 @@ FROM "pilot"`,
|
|
548
546
|
test('/pilot/$count?$top=5&$skip=5', (result, sqlEquals) => {
|
549
547
|
it('should select count(*) from pilot and ignore top and skip', () => {
|
550
548
|
sqlEquals(
|
551
|
-
result,
|
549
|
+
result.query,
|
552
550
|
`\
|
553
551
|
SELECT COUNT(*) AS "$count"
|
554
552
|
FROM "pilot"`,
|
@@ -559,7 +557,7 @@ FROM "pilot"`,
|
|
559
557
|
test('/pilot/$count?$select=id', (result, sqlEquals) => {
|
560
558
|
it('should select count(*) from pilot and ignore select', () => {
|
561
559
|
sqlEquals(
|
562
|
-
result,
|
560
|
+
result.query,
|
563
561
|
`\
|
564
562
|
SELECT COUNT(*) AS "$count"
|
565
563
|
FROM "pilot"`,
|
package/test/odata/select.js
CHANGED
@@ -5,7 +5,7 @@ const pilotFieldsStr = pilotFields.join(', ');
|
|
5
5
|
test('/pilot?$select=name', (result, sqlEquals) => {
|
6
6
|
it('should select name from pilot', () => {
|
7
7
|
sqlEquals(
|
8
|
-
result,
|
8
|
+
result.query,
|
9
9
|
`\
|
10
10
|
SELECT "pilot"."name"
|
11
11
|
FROM "pilot"`,
|
@@ -16,7 +16,7 @@ FROM "pilot"`,
|
|
16
16
|
test('/pilot?$select=favourite_colour', (result, sqlEquals) => {
|
17
17
|
it('should select favourite_colour from pilot', () => {
|
18
18
|
sqlEquals(
|
19
|
-
result,
|
19
|
+
result.query,
|
20
20
|
`\
|
21
21
|
SELECT "pilot"."favourite colour" AS "favourite_colour"
|
22
22
|
FROM "pilot"`,
|
@@ -31,7 +31,7 @@ test(
|
|
31
31
|
(result, sqlEquals) => {
|
32
32
|
it('should select from pilot with id', () => {
|
33
33
|
sqlEquals(
|
34
|
-
result,
|
34
|
+
result.query,
|
35
35
|
`\
|
36
36
|
SELECT "pilot"."favourite colour" AS "favourite_colour"
|
37
37
|
FROM "pilot"
|
@@ -48,7 +48,7 @@ test(
|
|
48
48
|
(result, sqlEquals) => {
|
49
49
|
it('should select favourite colour from pilot "TextKey"', () => {
|
50
50
|
sqlEquals(
|
51
|
-
result,
|
51
|
+
result.query,
|
52
52
|
`\
|
53
53
|
SELECT "pilot"."favourite colour" AS "favourite_colour"
|
54
54
|
FROM "pilot"
|
@@ -61,7 +61,7 @@ WHERE ("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?)`,
|
|
61
61
|
test('/pilot?$select=trained__pilot/name', (result, sqlEquals) => {
|
62
62
|
it('should select name from pilot', () => {
|
63
63
|
sqlEquals(
|
64
|
-
result,
|
64
|
+
result.query,
|
65
65
|
`\
|
66
66
|
SELECT "pilot.trained-pilot"."name"
|
67
67
|
FROM "pilot"
|
@@ -73,7 +73,7 @@ LEFT JOIN "pilot" AS "pilot.trained-pilot" ON "pilot"."id" = "pilot.trained-pilo
|
|
73
73
|
test('/pilot?$select=trained__pilot/name,age', (result, sqlEquals) => {
|
74
74
|
it('should select name, age from pilot', () => {
|
75
75
|
sqlEquals(
|
76
|
-
result,
|
76
|
+
result.query,
|
77
77
|
`\
|
78
78
|
SELECT "pilot.trained-pilot"."name", "pilot"."age"
|
79
79
|
FROM "pilot"
|
@@ -85,7 +85,7 @@ LEFT JOIN "pilot" AS "pilot.trained-pilot" ON "pilot"."id" = "pilot.trained-pilo
|
|
85
85
|
test('/pilot?$select=*', (result, sqlEquals) => {
|
86
86
|
it('should select * from pilot', () => {
|
87
87
|
sqlEquals(
|
88
|
-
result,
|
88
|
+
result.query,
|
89
89
|
`\
|
90
90
|
SELECT ${pilotFieldsStr}
|
91
91
|
FROM "pilot"`,
|
@@ -96,7 +96,7 @@ FROM "pilot"`,
|
|
96
96
|
test('/pilot?$select=licence/id', (result, sqlEquals) => {
|
97
97
|
it('should select licence/id for pilots', () => {
|
98
98
|
sqlEquals(
|
99
|
-
result,
|
99
|
+
result.query,
|
100
100
|
`\
|
101
101
|
SELECT "pilot.licence"."id"
|
102
102
|
FROM "pilot"
|
@@ -108,7 +108,7 @@ LEFT JOIN "licence" AS "pilot.licence" ON "pilot"."licence" = "pilot.licence"."i
|
|
108
108
|
test('/pilot?$select=can_fly__plane/plane/id', (result, sqlEquals) => {
|
109
109
|
it('should select can_fly__plane/plane/id for pilots', () => {
|
110
110
|
sqlEquals(
|
111
|
-
result,
|
111
|
+
result.query,
|
112
112
|
`\
|
113
113
|
SELECT "pilot.pilot-can fly-plane.plane"."id"
|
114
114
|
FROM "pilot"
|
package/test/odata/stress.js
CHANGED
@@ -14,8 +14,7 @@ const filterBindsNandString = _.map(
|
|
14
14
|
() => 'NOT(("pilot"."id") IS NOT NULL AND ("pilot"."id") = (?))',
|
15
15
|
).join('\nAND ');
|
16
16
|
|
17
|
-
|
18
|
-
const filterBinds = filterIDs.map((_n, i) => /** @type {const} */ (['Bind', i]));
|
17
|
+
const filterBinds = _.map(filterIDs, (_n, i) => ['Bind', i]);
|
19
18
|
|
20
19
|
let filterString = `id in (${filterIDs.join(', ')})`;
|
21
20
|
test(
|
@@ -25,7 +24,7 @@ test(
|
|
25
24
|
(result, sqlEquals) => {
|
26
25
|
it('should select from pilot with a long IN clause', () => {
|
27
26
|
sqlEquals(
|
28
|
-
result,
|
27
|
+
result.query,
|
29
28
|
`\
|
30
29
|
SELECT ${pilotFieldsStr}
|
31
30
|
FROM "pilot"
|
@@ -45,7 +44,7 @@ test(
|
|
45
44
|
(result, sqlEquals) => {
|
46
45
|
it('should select from pilot with a long NOT IN clause', () => {
|
47
46
|
sqlEquals(
|
48
|
-
result,
|
47
|
+
result.query,
|
49
48
|
`\
|
50
49
|
SELECT ${pilotFieldsStr}
|
51
50
|
FROM "pilot"
|
@@ -65,7 +64,7 @@ test(
|
|
65
64
|
(result, sqlEquals) => {
|
66
65
|
it('should select from pilot with a long IN clause', () => {
|
67
66
|
sqlEquals(
|
68
|
-
result,
|
67
|
+
result.query,
|
69
68
|
`\
|
70
69
|
SELECT ${pilotFieldsStr}
|
71
70
|
FROM "pilot"
|
@@ -83,7 +82,7 @@ test(
|
|
83
82
|
(result, sqlEquals) => {
|
84
83
|
it('should select from pilot with a long NOT IN clause', () => {
|
85
84
|
sqlEquals(
|
86
|
-
result,
|
85
|
+
result.query,
|
87
86
|
`\
|
88
87
|
SELECT ${pilotFieldsStr}
|
89
88
|
FROM "pilot"
|
@@ -0,0 +1,138 @@
|
|
1
|
+
import * as fs from 'node:fs';
|
2
|
+
import * as ODataParser from '@balena/odata-parser';
|
3
|
+
import { OData2AbstractSQL } from '@balena/odata-to-abstract-sql';
|
4
|
+
const sbvrModel = fs.readFileSync(require.resolve('../model.sbvr'), 'utf8');
|
5
|
+
|
6
|
+
import * as AbstractSQLCompiler from '../..';
|
7
|
+
|
8
|
+
import { expect } from 'chai';
|
9
|
+
import _ from 'lodash';
|
10
|
+
|
11
|
+
const generateClientModel = function (input) {
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
13
|
+
const sbvrTypes = require('@balena/sbvr-types').default;
|
14
|
+
const typeVocab = fs.readFileSync(
|
15
|
+
require.resolve('@balena/sbvr-types/Type.sbvr'),
|
16
|
+
'utf8',
|
17
|
+
);
|
18
|
+
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
20
|
+
const SBVRParser = require('@balena/sbvr-parser').SBVRParser.createInstance();
|
21
|
+
SBVRParser.enableReusingMemoizations(SBVRParser._sideEffectingRules);
|
22
|
+
SBVRParser.AddCustomAttribute('Database ID Field:');
|
23
|
+
SBVRParser.AddCustomAttribute('Database Table Name:');
|
24
|
+
SBVRParser.AddBuiltInVocab(typeVocab);
|
25
|
+
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
27
|
+
const LF2AbstractSQL = require('@balena/lf-to-abstract-sql');
|
28
|
+
const LF2AbstractSQLTranslator = LF2AbstractSQL.createTranslator(sbvrTypes);
|
29
|
+
|
30
|
+
const lf = SBVRParser.matchAll(input, 'Process');
|
31
|
+
const abstractSql = LF2AbstractSQLTranslator(lf, 'Process');
|
32
|
+
return abstractSql;
|
33
|
+
};
|
34
|
+
|
35
|
+
export const clientModel = generateClientModel(sbvrModel);
|
36
|
+
const odata2AbstractSQL = new OData2AbstractSQL(clientModel);
|
37
|
+
|
38
|
+
const bindingsTest = function (actualBindings, expectedBindings) {
|
39
|
+
if (expectedBindings == null) {
|
40
|
+
expectedBindings = false;
|
41
|
+
}
|
42
|
+
if (expectedBindings === false) {
|
43
|
+
it('should not have any bindings', () => {
|
44
|
+
expect(actualBindings).to.be.empty;
|
45
|
+
});
|
46
|
+
} else {
|
47
|
+
it('should have matching bindings', () => {
|
48
|
+
expect(actualBindings).to.deep.equal(expectedBindings);
|
49
|
+
});
|
50
|
+
}
|
51
|
+
};
|
52
|
+
|
53
|
+
const equals = (actual, expected) => {
|
54
|
+
expect(actual).to.equal(expected);
|
55
|
+
};
|
56
|
+
const sqlEquals = {
|
57
|
+
websql: equals,
|
58
|
+
mysql: equals,
|
59
|
+
postgres(actual, expected) {
|
60
|
+
let num = 1;
|
61
|
+
while (_.includes(expected, '?')) {
|
62
|
+
expected = expected.replace('?', '$' + num);
|
63
|
+
num++;
|
64
|
+
}
|
65
|
+
equals(actual, expected);
|
66
|
+
},
|
67
|
+
};
|
68
|
+
|
69
|
+
const runExpectation = function (describe, engine, input, ...args) {
|
70
|
+
/** @type {import('@balena/odata-parser').SupportedMethod} */
|
71
|
+
let method = 'GET';
|
72
|
+
let expectedBindings = false;
|
73
|
+
let body = {};
|
74
|
+
let expectation;
|
75
|
+
switch (args.length) {
|
76
|
+
case 1:
|
77
|
+
[expectation] = args;
|
78
|
+
break;
|
79
|
+
case 2:
|
80
|
+
[method, expectation] = args;
|
81
|
+
break;
|
82
|
+
case 3:
|
83
|
+
[method, expectedBindings, expectation] = args;
|
84
|
+
break;
|
85
|
+
case 4:
|
86
|
+
[method, expectedBindings, body, expectation] = args;
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
|
90
|
+
describe(
|
91
|
+
'Parsing ' + method + ' ' + _.truncate(input, { length: 100 }),
|
92
|
+
function () {
|
93
|
+
let result;
|
94
|
+
try {
|
95
|
+
input = ODataParser.parse(input);
|
96
|
+
const { tree, extraBodyVars } = odata2AbstractSQL.match(
|
97
|
+
input.tree,
|
98
|
+
method,
|
99
|
+
_.keys(body),
|
100
|
+
0,
|
101
|
+
);
|
102
|
+
_.assign(body, extraBodyVars);
|
103
|
+
result = AbstractSQLCompiler[engine].compileRule(tree);
|
104
|
+
} catch (e) {
|
105
|
+
expectation(e);
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
if (Array.isArray(result)) {
|
109
|
+
for (let i = 0; i < result.length; i++) {
|
110
|
+
const actualResult = result[i];
|
111
|
+
if (expectedBindings[0][0] === 'Bind') {
|
112
|
+
bindingsTest(actualResult.bindings, expectedBindings);
|
113
|
+
} else {
|
114
|
+
bindingsTest(actualResult.bindings, expectedBindings[i]);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
} else {
|
118
|
+
bindingsTest(result.bindings, expectedBindings);
|
119
|
+
}
|
120
|
+
expectation(result, sqlEquals[engine]);
|
121
|
+
},
|
122
|
+
);
|
123
|
+
};
|
124
|
+
|
125
|
+
const bindRunExpectation = function (engine) {
|
126
|
+
const bound = runExpectation.bind(null, describe, engine);
|
127
|
+
bound.skip = runExpectation.bind(null, describe.skip, engine);
|
128
|
+
// eslint-disable-next-line no-only-tests/no-only-tests -- this is a false positive
|
129
|
+
bound.only = runExpectation.bind(null, describe.only, engine);
|
130
|
+
return bound;
|
131
|
+
};
|
132
|
+
|
133
|
+
const testFn = bindRunExpectation('postgres');
|
134
|
+
testFn.postgres = bindRunExpectation('postgres');
|
135
|
+
testFn.mysql = bindRunExpectation('mysql');
|
136
|
+
testFn.websql = bindRunExpectation('websql');
|
137
|
+
|
138
|
+
export default testFn;
|