-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlinebreaks.test.ts
988 lines (838 loc) · 37.6 KB
/
linebreaks.test.ts
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
import { formatQuery } from '../../formatting/formatting';
import { MAX_COL } from '../../formatting/formattingHelpers';
import { verifyFormatting } from './testutil';
describe('tests for line breaks', () => {
const q0 = `
match (n)
where n.age > 10 and n.born > 10 and n.prop > 15 and n.otherprop > 20 and n.thirdprop > 50
return n`;
const q1 = `MATCH (p:Person)
WHERE p.name STARTS WITH 'A' OR p.name STARTS WITH 'B' OR p.name STARTS WITH 'C' OR p.age > 30 OR p.salary > 50000 OR p.experience > 10 OR p.position = 'Manager'
RETURN p`;
const q2 = `MATCH (e:Employee)
RETURN
CASE
WHEN e.salary > 100000 THEN 'High'
WHEN e.salary > 50000 THEN 'Medium'
WHEN e.salary > 30000 THEN
CASE
WHEN e.experience > 5 THEN 'Mid-Level'
ELSE 'Low'
END
ELSE 'Entry-Level'
END AS SalaryCategory`;
const q3 = `MATCH (o:Order)-[:CONTAINS]->(p:Product)
WITH o, p, COUNT(p) AS productCount, SUM(p.price) AS totalValue, AVG(p.discount) AS avgDiscount, MIN(p.price) AS minPrice, MAX(p.price) AS maxPrice
WHERE totalValue > 1000 AND productCount > 5
RETURN o, totalValue, avgDiscount`;
const q4 = `MATCH (c:Customer)-[:PURCHASED]->(o:Order)-[:CONTAINS]->(p:Product)
RETURN c.name, COLLECT({orderId: o.id, items: COLLECT({product: p.name, price: p.price, discount: p.discount})}) AS orderSummary, c.someOtherPrettyLongProperty AS otherLongProperty`;
const q5 = `MATCH (a:Author)-[:WROTE]->(b:Book)-[:TRANSLATED_TO]->(t:Translation)-[:PUBLISHED_BY]->(p:Publisher)-[:LOCATED_IN]->(c:Country)
WHERE b.genre = 'Sci-Fi' AND p.name STARTS WITH 'P' AND c.region = 'Europe'
RETURN a.name, b.title, t.language, p.name, c.name`;
const q6 = `MATCH (c:Customer)
CALL {
WITH c
MATCH (c)-[:PURCHASED]->(o:Order)-[:CONTAINS]->(p:Product)
RETURN COUNT(o) AS totalOrders, SUM(p.price) AS totalSpent, AVG(p.price) AS avgPrice, MAX(p.price) AS mostExpensiveItem
}
RETURN c.name, totalOrders, totalSpent, avgPrice, mostExpensiveItem`;
const q7 = `MATCH (c:Company)-[:EMPLOYS]->(e:Employee)
UNWIND e.projects AS project
UNWIND project.tasks AS task
RETURN c.name, e.name, task.name, COUNT(task.subtasks) AS totalSubtasks, SUM(task.hoursSpent) AS totalHours, AVG(task.complexity) AS avgComplexity`;
const q8 = `MATCH (p:Product)
WHERE p.category IN ['Electronics', 'Furniture', 'Clothing', 'Toys', 'Books', 'Appliances', 'Jewelry', 'Automotive', 'Beauty', 'Garden']
RETURN p`;
const q9 = `MERGE (a:Author {name: 'J.K. Rowling'})
ON CREATE SET a.birthYear = 1965, a.nationality = 'British', a.booksWritten = 7, a.netWorth = 1000000000, a.genre = 'Fantasy'
MERGE (b:Book {title: 'Harry Potter and the Sorcerers Stone'})
ON CREATE SET b.publishedYear = 1997, b.sales = 120000000, b.rating = 4.8, b.genre = 'Fantasy'
MERGE (a)-[:WROTE]->(b)
RETURN a, b`;
const q10 = `MATCH (p:Person)
WHERE p.name = 'Alberta' OR p.name = 'Berta' OR p.name = 'C' OR p.age > 30 OR p.salary > 50000 OR p.experience > 10 OR p.position = 'Manager'
RETURN p`;
// Greg query
const q11 = `MATCH (s:Schema)
// Find a schema which has at least 2 tables and at least PK and one FK
WHERE (s)-->(:Table)-->(:Column)-[:FK_COLUMN]-()
AND
(s)-->(:Table)-->(:Column)-[:PK_COLUMN]-()
AND
count { (s)-->() } > 1
// WITH collect(s) as schemas
// MATCH (s)|
WITH s
MATCH (s)-[:CONTAINS_TABLE]->(t:Table)-[:HAS_COLUMN]->(c:Column)
OPTIONAL MATCH (c)<-[:PK_COLUMN]-(pk:PrimaryKey)
OPTIONAL MATCH (c)<-[:FK_COLUMN]-(fk:ForeignKey)
WITH s,
t.name as tableName,
collect({name: c.name,
pk: CASE (not pk is null and $printKeyInfo) WHEN True THEN "(PK)" ELSE "" END,
fk: CASE (not fk is null and $printKeyInfo) WHEN True THEN "(FK)" ELSE "" END
}) as columns
WITH s, tableName, [x in columns | x.name + x.fk + x.pk] as columns
WITH s, "Table " + tableName + " has columns:" + apoc.text.join(columns,'') as tableDescriptions
WITH s, apoc.text.join(collect(tableDescriptions),'------------------------') as schemaDescription
SET s.schemaDescription=schemaDescription`;
// subqueries example
const q12 = `UNWIND range(1,100) as _
CALL {
MATCH (source:object) WHERE source.id= $id1
MATCH (target:object) WHERE target.id= $id2
MATCH path = (source)-[*1..10]->(target)
WITH path, reduce(weight = 0, r IN relationships(path) | weight + r.weight) as Weight
ORDER BY Weight LIMIT 3
RETURN length(path) as l, Weight
}
RETURN count(*)`;
//allTokenTypes example
const q13 = `MATCH (variable:Label)-[:REL_TYPE]->()
WHERE variable.property = "String" OR namespaced.function() = false // comment
OR $parameter > 2
RETURN variable;`;
const q14 = `MATCH (p:Product) WHERE p.price > 1000 AND p.stock > 50 AND p.category IN ['Electronics','Home Appliances','Garden Tools','Sports Equipment','Automotive Parts','Fashion Accessories','Books','Toys','Jewelry','Musical Instruments','Art Supplies','Office Supplies'] AND (CASE WHEN p.discount IS NULL THEN 0 ELSE p.discount END) > 0.15 AND (p.sold - (CASE WHEN p.reserved IS NULL THEN 0 ELSE p.reserved END)) > 20 AND (p.rating * (CASE WHEN p.reviews IS NULL THEN 1 ELSE p.reviews END)) > 3000 RETURN p`;
const q15 = `MATCH (o:Order)-[:CONTAINS]->(p:Product) WITH o, COUNT(p) AS itemCount, SUM(p.price * (1 - p.discount)) AS totalRevenue, AVG(p.rating) AS avgRating, MIN(p.price) AS minPrice, MAX(p.price) AS maxPrice, COLLECT({name: p.name, category: p.category, price: p.price, discount: p.discount, rating: p.rating, stock: p.stock, supplier: p.supplier, warranty: p.warranty, features: p.features}) AS productDetails WHERE totalRevenue > 10000 AND itemCount > 5 AND avgRating > 3.5 AND minPrice < 50 AND maxPrice < 1000 RETURN o, itemCount, totalRevenue, avgRating, minPrice, maxPrice, productDetails`;
const q16 = `MATCH (p:Product) WHERE p.sku IN ['SKU0001','SKU0002','SKU0003','SKU0004','SKU0005','SKU0006','SKU0007','SKU0008','SKU0009','SKU0010','SKU0011','SKU0012','SKU0013','SKU0014','SKU0015','SKU0016','SKU0017','SKU0018','SKU0019','SKU0020','SKU0021','SKU0022','SKU0023','SKU0024','SKU0025'] AND (p.price > 20 OR p.rating >= 4.0) AND (CASE WHEN p.discount IS NOT NULL THEN p.discount ELSE 0 END) < 0.25 RETURN p`;
const q17 = `MATCH (c:Customer)-[:HAS_INTEREST]->(i:Interest) UNWIND i.tags AS tag UNWIND ['Sports','Music','Travel','Technology','Fashion','Cooking','Gaming','Fitness','Art','Science','History','Literature','Movies','Theater','Photography','Nature','Automotive','Business','Health','Education'] AS popularTag WITH c, i, tag, popularTag, CASE WHEN tag = popularTag THEN 1 ELSE 0 END AS tagMatchScore, SIZE(i.tags) AS tagCount, (CASE WHEN SIZE(i.tags)=0 THEN 0 ELSE tagMatchScore * 100.0 / SIZE(i.tags) END) AS matchPercentage WHERE matchPercentage > 50 AND i.confidence > 0.7 RETURN c, i, tag, popularTag, tagMatchScore, matchPercentage`;
const q18 = `MATCH (m:Movie) RETURN m.title, m.releaseYear, CASE WHEN m.rating >= 9.0 THEN 'Masterpiece' WHEN m.rating >= 8.0 THEN 'Excellent' WHEN m.rating >= 7.0 THEN 'Great' WHEN m.rating >= 6.0 THEN 'Good' WHEN m.rating >= 5.0 THEN 'Average' WHEN m.rating >= 4.0 THEN 'Below Average' WHEN m.rating >= 3.0 THEN 'Poor' WHEN m.rating >= 2.0 THEN 'Very Poor' ELSE 'Unwatchable' END AS review, CASE WHEN m.genres CONTAINS 'Drama' AND m.genres CONTAINS 'Historical' THEN 'Epic' WHEN m.genres CONTAINS 'Comedy' AND m.genres CONTAINS 'Romance' THEN 'Charming' ELSE 'Mixed' END AS styleCategory`;
const q19 = `MATCH (p:Person)-[:KNOWS]->(friend:Person) OPTIONAL MATCH (friend)-[:WORKS_AT]->(c:Company) OPTIONAL MATCH (friend)-[:LIVES_IN]->(city:City) WITH p, friend, c, city, CASE WHEN c.name IS NULL THEN 'Unemployed' ELSE c.industry END AS jobIndustry, CASE WHEN city.population > 1000000 THEN 'Metropolitan' WHEN city.population > 500000 THEN 'Urban' ELSE 'Small Town' END AS citySize WHERE (p.age > 30 OR friend.age > 30) AND (jobIndustry IN ['Technology','Finance','Healthcare','Education','Entertainment']) RETURN p.name, friend.name, jobIndustry, city.name, citySize`;
const q20 = `MATCH (s:Session) CALL { WITH s MATCH (s)-[:HAS_EVENT]->(e:Event) WHERE (e.timestamp >= datetime('2025-01-01T00:00:00Z') AND e.timestamp <= datetime('2025-12-31T23:59:59Z')) AND e.type IN ['Click','View','Purchase','Signup','Logout','Login','Share','Comment','Like','Dislike','Subscribe','Unsubscribe'] WITH e, CASE WHEN e.value > 1000 THEN 'High' WHEN e.value > 500 THEN 'Medium' ELSE 'Low' END AS eventValue RETURN COLLECT({eventId: e.id, type: e.type, value: e.value, category: eventValue, extra: e.extraData}) AS events } WITH s, SIZE(events) AS eventCount WHERE eventCount > 5 RETURN s, eventCount, events`;
const q21 = `MATCH path = (m1:loooooooongrelationtypename {code: "mFG66X9v"})-[
r:verylongrelationtypename]->(m2:anotherverylongrelationtypename)
RETURN path`;
const q22 = `MATCH (e:Employee) RETURN e.name, CASE WHEN e.salary > 150000 AND e.experience > 10 THEN 'Senior ' + (CASE WHEN e.department = 'Engineering' THEN 'Engineer' WHEN e.department = 'Sales' THEN 'Sales Leader' ELSE 'Manager' END) WHEN e.salary > 100000 AND e.experience > 7 THEN 'Experienced ' + (CASE WHEN e.department = 'Engineering' THEN 'Developer' WHEN e.department = 'HR' THEN 'HR Specialist' ELSE 'Associate' END) WHEN e.salary > 75000 AND e.experience > 5 THEN 'Mid-Level ' + (CASE WHEN e.department = 'Engineering' THEN 'Engineer' ELSE 'Professional' END) ELSE 'Junior ' + (CASE WHEN e.department = 'Engineering' THEN 'Engineer' ELSE 'Staff' END) END AS jobTitle`;
const q23 = `MATCH (u:User) CALL { WITH u MATCH (u)-[:PURCHASED]->(o:Order)-[:CONTAINS]->(p:Product) WHERE p.price > 100 AND p.category IN ['Electronics','Computers','Smartphones','Accessories','Gaming','Wearables','Home Automation','Networking','Audio','Video','Software','Peripherals'] WITH u, o, p, p.price * (1 - COALESCE(p.discount,0)) AS netPrice, CASE WHEN p.rating > 4.5 THEN 'Excellent' WHEN p.rating > 3.5 THEN 'Good' WHEN p.rating > 2.5 THEN 'Average' ELSE 'Poor' END AS qualityRating RETURN o, COLLECT({product: p.name, netPrice: netPrice, qualityRating: qualityRating, features: p.features, warranty: p.warranty, stock: p.stock, supplier: p.supplier}) AS orderProducts } WITH u, COUNT(o) AS totalOrders, SUM([x IN COLLECT(o) | x.total]) AS totalSpent WHERE totalOrders > 3 RETURN u, totalOrders, totalSpent`;
const q24 = `CALL apoc.periodic.iterate ("eZ0sadadawdawdsdsdsdq", "1p7sdsdsasdwasddsdEsdsd", {baisdsdadadze: "v0Asdsdsdadadadsdsdp", paladadadel: "UsdssdsdsddUg"})`;
const queries = [
q0,
q1,
q2,
q3,
q4,
q5,
q6,
q7,
q8,
q9,
q10,
q11,
q12,
q13,
q14,
q15,
q16,
q17,
q18,
q19,
q20,
q21,
q22,
q23,
q24,
];
test('keeps all queries within the max column width', () => {
queries.forEach((query) => {
const formatted = formatQuery(query);
const lines = formatted.split('\n');
lines.forEach((line) => {
expect(line.length).toBeLessThanOrEqual(MAX_COL);
});
});
});
test('does not split in the middle of a relation', () => {
const expected = `
MATCH path = (m1:loooooooongrelationtypename {code: "mFG66X9v"})-
[r:verylongrelationtypename]->(m2:anotherverylongrelationtypename)
RETURN path`.trimStart();
verifyFormatting(q21, expected);
});
test('does not split the $ and the parameter name', () => {
const query =
'RETURN $paraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaam';
const expected =
'RETURN $paraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaam';
verifyFormatting(query, expected);
});
test('aligns split node pattern', () => {
const query = `MERGE (veeeeeerylongnodenameeeeeeeee:ZjFYQFrVDTVsA
{name: $veeeeeeeeerylongparaaaaaaaaaaaaaaam})`;
const expected = `
MERGE (veeeeeerylongnodenameeeeeeeee:ZjFYQFrVDTVsA
{name: $veeeeeeeeerylongparaaaaaaaaaaaaaaam})`.trimStart();
verifyFormatting(query, expected);
});
test('aligns nested parentheses well', () => {
const query = `MATCH (n)
WHERE ((($param1 IS NOT NULL AND this1.title = $param1) AND this1:WaFQynNy) AND (this1:WaFQynNy OR this1:hyztnnwg OR this1:QpLckJcy))`;
const expected = `MATCH (n)
WHERE ((($param1 IS NOT NULL AND this1.title = $param1) AND this1:WaFQynNy) AND
(this1:WaFQynNy OR this1:hyztnnwg OR this1:QpLckJcy))`;
verifyFormatting(query, expected);
});
test('aligns large maps one further than the opening brace', () => {
const query = `RETURN {looooooooooooooooooooooongkey:value, loooooooooooooooooooongkeeeyyyyyyyy:value2, looooooooooooooongkeeey:value3}`;
const expected = `
RETURN {looooooooooooooooooooooongkey: value,
loooooooooooooooooooongkeeeyyyyyyyy: value2,
looooooooooooooongkeeey: value3}`.trimStart();
verifyFormatting(query, expected);
});
test('long list should not break after the opening brace leaving it alone', () => {
const query = `MATCH (p:Product)
WHERE p.article_number IN [
"OCj0AswA", "dFRbj1s3", "oMbdvgm7", "L4Vey8xn", "GNgeDIkA", "pU4RE0lM",
"M6XNVJsO", "NcdW0tuB", "Pf6RIuP4", "6tKStKwl", "HfvahDu5", "gJoq3HnU",
"g7LjxbGD"]
RETURN p`;
const expected = `MATCH (p:Product)
WHERE p.article_number IN
["OCj0AswA", "dFRbj1s3", "oMbdvgm7", "L4Vey8xn", "GNgeDIkA", "pU4RE0lM",
"M6XNVJsO", "NcdW0tuB", "Pf6RIuP4", "6tKStKwl", "HfvahDu5", "gJoq3HnU",
"g7LjxbGD"]
RETURN p`;
verifyFormatting(query, expected);
});
test('should prefer breaking pattern list on commas', () => {
const query = `EXPLAIN
MATCH (eq:loooooongtype {keeeey: "sAGhmzsL"})-[]-(m:tyyyyype), (m)-[l1]-
(eqa:EquipoEmpresa)
WHERE eqa.prop <> "Aq0kC1bX"
RETURN eq`;
const expected = `EXPLAIN
MATCH (eq:loooooongtype {keeeey: "sAGhmzsL"})-[]-(m:tyyyyype),
(m)-[l1]-(eqa:EquipoEmpresa)
WHERE eqa.prop <> "Aq0kC1bX"
RETURN eq`;
verifyFormatting(query, expected);
});
test('should prefer to put ORDER BY etc together', () => {
const query = `MATCH (p:Person)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(Kevin:Person {name: "HEZDAAhT"})
WHERE p.name <> "nnwAPHJg"
RETURN p.name AS Name, p.born AS BirthYear, m.title AS MovieTitle
ORDER BY Name ASC
LIMIT "ZTWWLgIq"`;
const expected = `MATCH (p:Person)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-
(Kevin:Person {name: "HEZDAAhT"})
WHERE p.name <> "nnwAPHJg"
RETURN p.name AS Name, p.born AS BirthYear, m.title AS MovieTitle
ORDER BY Name ASC
LIMIT "ZTWWLgIq"`;
verifyFormatting(query, expected);
});
test('paths should be aligned after the =', () => {
const query = `MATCH p1 = (i:tyyyype {keeeeeeeey: "1QwLfE5M"})--
(il:nodetyyyype {type: "58vomdG0"})
RETURN i, apoc.map.removeKeys(il, ["TT6hUzUE"]) AS props`;
const expected = `
MATCH p1 = (i:tyyyype {keeeeeeeey: "1QwLfE5M"})--
(il:nodetyyyype {type: "58vomdG0"})
RETURN i, apoc.map.removeKeys(il, ["TT6hUzUE"]) AS props`.trimStart();
verifyFormatting(query, expected);
});
test('should fit this whole node on one line', () => {
const query = `MATCH (i:tyyyyyyyype {createdAt: datetime("V3bzb8bX"), description: "UM706WRV"})
RETURN i`;
const expected = `MATCH (i:tyyyyyyyype {createdAt: datetime("V3bzb8bX"), description: "UM706WRV"})
RETURN i`;
verifyFormatting(query, expected);
});
test('should not have weird alignment for multiple node creation', () => {
const query = `
CREATE (:actor {name: "jEmtGrSI"}),
(:actor {name: "HqFUar0i"}),
(:actor {name: "ZAvjBFt6"}),
(:actor {name: "7hbDfMOa"}),
(:actor {name: "AXhPvCyh"})`;
const expected = `
CREATE (:actor {name: "jEmtGrSI"}), (:actor {name: "HqFUar0i"}),
(:actor {name: "ZAvjBFt6"}), (:actor {name: "7hbDfMOa"}),
(:actor {name: "AXhPvCyh"})`.trimStart();
verifyFormatting(query, expected);
});
test('should align lists by the first element, not the bracket', () => {
const query = `MATCH (p:Product)
WHERE p.price > 1000 AND p.stock > 50 AND
p.category IN ['Electronics', 'Home Appliances', 'Garden Tools',
'Sports Equipment', 'Automotive Parts',
'Fashion Accessories', 'Books', 'Toys', 'Jewelry',
'Musical Instruments', 'Art Supplies', 'Office Supplies']
RETURN p`;
const expected = `MATCH (p:Product)
WHERE p.price > 1000 AND p.stock > 50 AND
p.category IN
['Electronics', 'Home Appliances', 'Garden Tools', 'Sports Equipment',
'Automotive Parts', 'Fashion Accessories', 'Books', 'Toys', 'Jewelry',
'Musical Instruments', 'Art Supplies', 'Office Supplies']
RETURN p`;
verifyFormatting(query, expected);
});
test('should not align long create statements weirdly', () => {
const query = `CREATE
(a:Location {name: "DXe5KhL3"}),
(b:Location {name: "v2BpdkOj"}),
(c:Location {name: "Fi5CMJ9Y"}),
(d:Location {name: "S31K3X1o"}),
(a)-[:ROUTE_TO {distance: "zjisNPKv", duration: "ivAC2TGF"}]->(b),
(b)-[:ROUTE_TO {distance: "Irogkqf1", duration: "QsCt67v1"}]->(c),
(c)-[:ROUTE_TO {distance: "Y53yoQwn", duration: "X41tnMDd"}]->(d);`;
const expected = `CREATE (a:Location {name: "DXe5KhL3"}), (b:Location {name: "v2BpdkOj"}),
(c:Location {name: "Fi5CMJ9Y"}), (d:Location {name: "S31K3X1o"}),
(a)-[:ROUTE_TO {distance: "zjisNPKv", duration: "ivAC2TGF"}]->(b),
(b)-[:ROUTE_TO {distance: "Irogkqf1", duration: "QsCt67v1"}]->(c),
(c)-[:ROUTE_TO {distance: "Y53yoQwn", duration: "X41tnMDd"}]->(d);`;
verifyFormatting(query, expected);
});
test('should align arguments of function invocation after opening bracket', () => {
const query = `RETURN collect(create_this1 { datetime: apoc.date.convertFormat(toString(create_this1.datetime), "OZQvXyoU", "EhpkDy8g") }) AS data`;
const expected = `
RETURN collect(create_this1
{datetime:
apoc.date.convertFormat(toString(create_this1.datetime),
"OZQvXyoU", "EhpkDy8g")}) AS data`.trimStart();
verifyFormatting(query, expected);
});
test('should not forget about alignment for unwind clause', () => {
const query = `UNWIND [{_id:"MiltPFxk", properties:{name:"5nIou0gC", id:"ha44MrBy", value:"6o5lzHd6"}}, {_id:"2uMA2cW8", properties:{name:"WOsBC4Ks", id:"bP526OzE", value:"WhYP4dxd"}}] AS row RETURN row`;
const expected = `
UNWIND [{_id: "MiltPFxk",
properties: {name: "5nIou0gC", id: "ha44MrBy", value: "6o5lzHd6"}},
{_id: "2uMA2cW8",
properties: {name: "WOsBC4Ks", id: "bP526OzE", value: "WhYP4dxd"}}]
AS row
RETURN row`.trimStart();
verifyFormatting(query, expected);
});
test('should not want to split in the middle of AS here', () => {
const query = `EXPLAIN
MATCH (p:Person)-[:HAS_ACCOUNT]->(s:Platform)
WHERE s.deactivated = "k1fU0uk0" AND
NOT (toLower(s.name) CONTAINS "ki9c1rU8") AND p.networkDbId IS NOT NULL
WITH p, COLLECT({platfId: s.platfId, name: s.name, numMsgs: s.deactivated}) AS
platfs, COUNT(s) AS numplatf
WHERE numplatf >= "gkLi0qvW"
RETURN DISTINCT p.networkDbId, p.name, platfs`;
const expected = `EXPLAIN
MATCH (p:Person)-[:HAS_ACCOUNT]->(s:Platform)
WHERE s.deactivated = "k1fU0uk0" AND
NOT (toLower(s.name) CONTAINS "ki9c1rU8") AND p.networkDbId IS NOT NULL
WITH p, COLLECT({platfId: s.platfId, name: s.name, numMsgs: s.deactivated})
AS platfs, COUNT(s) AS numplatf
WHERE numplatf >= "gkLi0qvW"
RETURN DISTINCT p.networkDbId, p.name, platfs`;
verifyFormatting(query, expected);
});
test('no splits within an arrow', () => {
const query = `MERGE (naame)-[:tyyyyyyyyyype {keeeeeeeey: "dFTkCNlb", keey: "rmmCQGIb"}]->(naaaaame);`;
const expected = `
MERGE (naame)-[:tyyyyyyyyyype {keeeeeeeey: "dFTkCNlb", keey: "rmmCQGIb"}]->
(naaaaame);`.trimStart();
verifyFormatting(query, expected);
});
test('function arguments should align', () => {
const query = `CALL apoc.periodic.iterate("eZQB0P0q", "1p7EFkyE", {batchSize: "v0Ap5F8F", parallel: "UUc75lVg"}) YIELD batches, total, timeTaken, committedOperations, failedOperations`;
const expected = `
CALL apoc.periodic.iterate("eZQB0P0q", "1p7EFkyE",
{batchSize: "v0Ap5F8F", parallel: "UUc75lVg"})
YIELD batches, total, timeTaken, committedOperations, failedOperations`.trimStart();
verifyFormatting(query, expected);
});
test('does not split weird parenthesized expressions in an odd way', () => {
const query = `MATCH (p:Product)--(o:Order)
WHERE (p.priiiiiiiiiiiiiiiiiiice + o.siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiize)
RETURN p;`;
const expected = `MATCH (p:Product)--(o:Order)
WHERE (p.priiiiiiiiiiiiiiiiiiice +
o.siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiize)
RETURN p;`;
verifyFormatting(query, expected);
});
test('complicated QPP', () => {
const query = `
MATCH (dmk:Station {name: 'Denmark Hill'})<-[:CALLS_AT]-(l1a:CallingPoint)-[:NEXT]->+
(l1b)-[:CALLS_AT]->(x:Station)<-[:CALLS_AT]-(l2a:CallingPoint)-[:NEXT]->*
(l2b)-[:CALLS_AT]->(gtw:Station {name: 'Gatwick Airport'})
RETURN dmk`;
const expected = `
MATCH (dmk:Station {name: 'Denmark Hill'})<-[:CALLS_AT]-(l1a:CallingPoint)-
[:NEXT]->+(l1b)-[:CALLS_AT]->(x:Station)<-[:CALLS_AT]-(l2a:CallingPoint)-
[:NEXT]->*(l2b)-[:CALLS_AT]->(gtw:Station {name: 'Gatwick Airport'})
RETURN dmk`.trim();
verifyFormatting(query, expected);
});
test('should not break after DISTINCT that follows RETURN', () => {
const query = `MATCH (abcde:wxyz)-[]->(fgh:wxyz)-[]->(ijk:wxyz)-[]->(lm:wxyz)
WHERE abcde.zxcvbnml = "XyZpQ8Rt"
RETURN DISTINCT
abcde.qwertyuiopa, abcde.zxcvbnmasdfgh, abcde.zxcvbnml, fgh.qwertyuiopa,
fgh.zxcvbnmasdfgh, fgh.zxcvbnml, ijk.qwertyuiopa, ijk.zxcvbnmasdfgh,
ijk.zxcvbnml, lm.qwertyuiopa, lm.zxcvbnmasdfgh, lm.zxcvbnml, lm.lkjhgfdswert
ORDER BY lm.lkjhgfdswert ASC`;
const expected = `MATCH (abcde:wxyz)-[]->(fgh:wxyz)-[]->(ijk:wxyz)-[]->(lm:wxyz)
WHERE abcde.zxcvbnml = "XyZpQ8Rt"
RETURN DISTINCT abcde.qwertyuiopa, abcde.zxcvbnmasdfgh, abcde.zxcvbnml,
fgh.qwertyuiopa, fgh.zxcvbnmasdfgh, fgh.zxcvbnml,
ijk.qwertyuiopa, ijk.zxcvbnmasdfgh, ijk.zxcvbnml,
lm.qwertyuiopa, lm.zxcvbnmasdfgh, lm.zxcvbnml, lm.lkjhgfdswert
ORDER BY lm.lkjhgfdswert ASC`;
verifyFormatting(query, expected);
});
});
describe('tests for respcecting user line breaks', () => {
test('multiple clauses', () => {
const query = `MATCH (move:Item) WHERE move.name = $move
OPTIONAL MATCH (insertBefore:Item) WHERE insertBefore.name = $insertBefore
WITH move, insertBefore
WHERE (insertBefore IS NULL OR move <> insertBefore) AND
(NOT EXISTS {(move)-[:NEXT]->(insertBefore)}) AND
(insertBefore IS NOT NULL OR EXISTS{(move)-[:NEXT]->()})
OPTIONAL MATCH (beforeMove:Item)-[relBeforeMove:NEXT]->(move)
OPTIONAL MATCH (move)-[relAfterMove:NEXT]->(afterMove:Item)
DELETE relBeforeMove
DELETE relAfterMove
WITH move, insertBefore, beforeMove, afterMove
OPTIONAL MATCH (insertAfter:Item)-[oldRel:NEXT]->(insertBefore)
DELETE oldRel
WITH move, insertBefore, insertAfter, beforeMove, afterMove
CALL(beforeMove, afterMove) {
WITH *
WHERE beforeMove IS NOT NULL AND afterMove IS NOT NULL
CREATE (beforeMove)-[:NEXT]->(afterMove)
}
CALL(move, insertBefore, insertAfter) {
WITH *
WHERE insertBefore IS NULL // insertAfter will always be NULL in this case
MATCH (lastElement:Item)
WHERE NOT (lastElement)-[:NEXT]->() AND lastElement <> move
CREATE (lastElement)-[:NEXT]->(move)
}
CALL(move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NULL AND insertBefore IS NOT NULL
CREATE (move)-[:NEXT]->(insertBefore)
}
CALL(move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NOT NULL AND insertBefore IS NOT NULL
CREATE (insertAfter)-[:NEXT]->(move)
CREATE (move)-[:NEXT]->(insertBefore)
}`;
const expected = `MATCH (move:Item)
WHERE move.name = $move
OPTIONAL MATCH (insertBefore:Item)
WHERE insertBefore.name = $insertBefore
WITH move, insertBefore
WHERE (insertBefore IS NULL OR move <> insertBefore) AND
(NOT EXISTS { (move)-[:NEXT]->(insertBefore) }) AND
(insertBefore IS NOT NULL OR EXISTS { (move)-[:NEXT]->() })
OPTIONAL MATCH (beforeMove:Item)-[relBeforeMove:NEXT]->(move)
OPTIONAL MATCH (move)-[relAfterMove:NEXT]->(afterMove:Item)
DELETE relBeforeMove
DELETE relAfterMove
WITH move, insertBefore, beforeMove, afterMove
OPTIONAL MATCH (insertAfter:Item)-[oldRel:NEXT]->(insertBefore)
DELETE oldRel
WITH move, insertBefore, insertAfter, beforeMove, afterMove
CALL (beforeMove, afterMove) {
WITH *
WHERE beforeMove IS NOT NULL AND afterMove IS NOT NULL
CREATE (beforeMove)-[:NEXT]->(afterMove)
}
CALL (move, insertBefore, insertAfter) {
WITH *
WHERE insertBefore IS NULL // insertAfter will always be NULL in this case
MATCH (lastElement:Item)
WHERE NOT (lastElement)-[:NEXT]->() AND lastElement <> move
CREATE (lastElement)-[:NEXT]->(move)
}
CALL (move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NULL AND insertBefore IS NOT NULL
CREATE (move)-[:NEXT]->(insertBefore)
}
CALL (move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NOT NULL AND insertBefore IS NOT NULL
CREATE (insertAfter)-[:NEXT]->(move)
CREATE (move)-[:NEXT]->(insertBefore)
}`;
verifyFormatting(query, expected);
});
test('multiple clauses with comments', () => {
const query = `
// Find the cell to move and the cell to insert it before (which may be null to insert at the end of the list)
MATCH (move:Item) WHERE move.name = $move
OPTIONAL MATCH (insertBefore:Item) WHERE insertBefore.name = $insertBefore
// If we ask to have it placed at the same position, don't do anything
WITH move, insertBefore
WHERE (insertBefore IS NULL OR move <> insertBefore) AND
(NOT EXISTS {(move)-[:NEXT]->(insertBefore)}) AND
(insertBefore IS NOT NULL OR EXISTS{(move)-[:NEXT]->()})
// Find the items before and after the item to move (if they exist)
OPTIONAL MATCH (beforeMove:Item)-[relBeforeMove:NEXT]->(move)
OPTIONAL MATCH (move)-[relAfterMove:NEXT]->(afterMove:Item)
// Disconnect the item to move
DELETE relBeforeMove
DELETE relAfterMove
// Now locate the item to insert it after (if any)
WITH move, insertBefore, beforeMove, afterMove
OPTIONAL MATCH (insertAfter:Item)-[oldRel:NEXT]->(insertBefore)
// Delete the old link to make place for the moved item
DELETE oldRel
// Now patch up the link where the item was removed (unless in start or end)
WITH move, insertBefore, insertAfter, beforeMove, afterMove
CALL(beforeMove, afterMove) {
WITH *
WHERE beforeMove IS NOT NULL AND afterMove IS NOT NULL
CREATE (beforeMove)-[:NEXT]->(afterMove)
}
// Now we need to insert the moved item, but how we do that depends on where it goes
CALL(move, insertBefore, insertAfter) {
WITH *
WHERE insertBefore IS NULL // insertAfter will always be NULL in this case
MATCH (lastElement:Item)
WHERE NOT (lastElement)-[:NEXT]->() AND lastElement <> move
CREATE (lastElement)-[:NEXT]->(move)
}
CALL(move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NULL AND insertBefore IS NOT NULL
CREATE (move)-[:NEXT]->(insertBefore)
}
CALL(move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NOT NULL AND insertBefore IS NOT NULL
CREATE (insertAfter)-[:NEXT]->(move)
CREATE (move)-[:NEXT]->(insertBefore)
}`;
const expected = `// Find the cell to move and the cell to insert it before (which may be null to insert at the end of the list)
MATCH (move:Item)
WHERE move.name = $move
OPTIONAL MATCH (insertBefore:Item)
WHERE insertBefore.name = $insertBefore
// If we ask to have it placed at the same position, don't do anything
WITH move, insertBefore
WHERE (insertBefore IS NULL OR move <> insertBefore) AND
(NOT EXISTS { (move)-[:NEXT]->(insertBefore) }) AND
(insertBefore IS NOT NULL OR EXISTS { (move)-[:NEXT]->() })
// Find the items before and after the item to move (if they exist)
OPTIONAL MATCH (beforeMove:Item)-[relBeforeMove:NEXT]->(move)
OPTIONAL MATCH (move)-[relAfterMove:NEXT]->(afterMove:Item)
// Disconnect the item to move
DELETE relBeforeMove
DELETE relAfterMove
// Now locate the item to insert it after (if any)
WITH move, insertBefore, beforeMove, afterMove
OPTIONAL MATCH (insertAfter:Item)-[oldRel:NEXT]->(insertBefore)
// Delete the old link to make place for the moved item
DELETE oldRel
// Now patch up the link where the item was removed (unless in start or end)
WITH move, insertBefore, insertAfter, beforeMove, afterMove
CALL (beforeMove, afterMove) {
WITH *
WHERE beforeMove IS NOT NULL AND afterMove IS NOT NULL
CREATE (beforeMove)-[:NEXT]->(afterMove)
}
// Now we need to insert the moved item, but how we do that depends on where it goes
CALL (move, insertBefore, insertAfter) {
WITH *
WHERE insertBefore IS NULL // insertAfter will always be NULL in this case
MATCH (lastElement:Item)
WHERE NOT (lastElement)-[:NEXT]->() AND lastElement <> move
CREATE (lastElement)-[:NEXT]->(move)
}
CALL (move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NULL AND insertBefore IS NOT NULL
CREATE (move)-[:NEXT]->(insertBefore)
}
CALL (move, insertBefore, insertAfter) {
WITH *
WHERE insertAfter IS NOT NULL AND insertBefore IS NOT NULL
CREATE (insertAfter)-[:NEXT]->(move)
CREATE (move)-[:NEXT]->(insertBefore)
}`;
verifyFormatting(query, expected);
});
test('simplest possible match return example', () => {
const query = `
MATCH (n)
RETURN n`;
const expected = `
MATCH (n)
RETURN n`.trimStart();
verifyFormatting(query, expected);
});
test('simplest possible match return example with more than one break', () => {
const query = `
MATCH (n)
RETURN n`;
const expected = `
MATCH (n)
RETURN n`.trimStart();
verifyFormatting(query, expected);
});
test('this comment should leave a newline before it', () => {
const query = `
MATCH (n)
// Comment
RETURN n`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});
test('another long example with clauses and comments', () => {
const query = `MERGE (qwerty:Abcdef {name: "ABCDEFGH"})
// Xyzzab qwe POIUYTREWQ poiuy rty uio MNBVCXZ
MERGE (A1B2C3D4E5:Qwert {name: "IJKLMNOP"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "abcdefgh"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "ijklmnop"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "qrstuvwx"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "yzABCDEF"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "GHIJKLMN"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "opqrstuv"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "wxyz0123"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "456789ab"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "cdefghij"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "klmnopqr"})
MERGE (A1B2C3D4E5)-[:QAZWSXEDCR]->(:Zxcvbn {name: "stuvwxyz"})
MERGE (qwerty)-[:PLMKOIJNBHUY]->(A1B2C3D4E5)
// Abcdef ghi ZxcvbnmQwertyz uiopa sdx fgh jklzxcv
MERGE (F6G7H8J9K0L1M2:Qwert {name: "ZXCVBNML"})
MERGE (F6G7H8J9K0L1M2)-[:QAZWSXEDCR]->(:Zxcvbn {name: "asdfghjk"})
MERGE (F6G7H8J9K0L1M2)-[:QAZWSXEDCR]->(:Zxcvbn {name: "poiuytre"})
MERGE (qwerty)-[:PLMKOIJNBHUY]->(F6G7H8J9K0L1M2)
MERGE (ZyXwVuTsr:Qwert {name: "lkjhgfds"})
MERGE (ZyXwVuTsr)-[:QAZWSXEDCR]->(:Zxcvbn {name: "mnbvcxza"})
MERGE (ZyXwVuTsr)-[:QAZWSXEDCR]->(:Zxcvbn {name: "qwertyui"})
MERGE (qwerty)-[:PLMKOIJNBHUY]->(ZyXwVuTsr)
// Fghijk lmn QWERTYUIOPASDFGHJ KJIHG QAZ WSX EDCRFVT
MERGE (QWERTYUIOPASDFGHJ:Qwert {name: "1234abcd"})
MERGE (QWERTYUIOPASDFGHJ)-[:QAZWSXEDCR]->(:Zxcvbn {name: "efghijkl"})
MERGE (QWERTYUIOPASDFGHJ)-[:QAZWSXEDCR]->(:Zxcvbn {name: "mnopqrst"})
MERGE (QWERTYUIOPASDFGHJ)-[:QAZWSXEDCR]->(:Zxcvbn {name: "uvwxYZ12"})
MERGE (QWERTYUIOPASDFGHJ)-[:QAZWSXEDCR]->(:Zxcvbn {name: "34567890"})
MERGE (qwerty)-[:PLMKOIJNBHUY]->(QWERTYUIOPASDFGHJ)
// Lmnopq rst UVWXYZABCDEFG NOPQR STU VWX YZABCDF
MERGE (LMNOPQRSTUVWX:Qwert {name: "zxvbnmlk"})
MERGE (LMNOPQRSTUVWX)-[:QAZWSXEDCR]->(:Zxcvbn {name: "opaslkdj"})
MERGE (LMNOPQRSTUVWX)-[:QAZWSXEDCR]->(:Zxcvbn {name: "qwerty12"})
MERGE (qwerty)-[:PLMKOIJNBHUY]->(LMNOPQRSTUVWX)
// uvwxyz efg lmno pqrstuvwxyzab
MERGE (pqr45:Qwer {name: "asdf1234", type: "zxcv5678"})
MERGE (LMNOPQRSTUVWX)-[:ZXCVB]->(pqr45)-[:ZXCVB]->(A1B2C3D4E5)
MERGE (qwerty)-[:ASDFGHJKL]->(pqr45)
MERGE (stu78:Qwer {name: "poiuy987", type: "lkjh6543"})
MERGE (A1B2C3D4E5)-[:ZXCVB]->(stu78)-[:ZXCVB]->(F6G7H8J9K0L1M2)
MERGE (qwerty)-[:ASDFGHJKL]->(stu78)
MERGE (vwx90:Qwer {name: "mnbv3210", type: "zazxswed"})
MERGE (A1B2C3D4E5)-[:ZXCVB]->(vwx90)-[:ZXCVB]->(ZyXwVuTsr)
MERGE (qwerty)-[:ASDFGHJKL]->(vwx90)`;
const expected = query;
verifyFormatting(query, expected);
});
test('double newline before block comment', () => {
const query = `
MATCH (a)
/* block comment */
RETURN a`;
const expected = `
MATCH (a)
/* block comment */
RETURN a`.trimStart();
verifyFormatting(query, expected);
});
test('inline block comment with internal newlines', () => {
const query = `
MATCH (a)
RETURN a; /* comment line 1
comment line 2 */
MATCH (b)
RETURN b;`;
const expected = `
MATCH (a)
RETURN a; /* comment line 1
comment line 2 */
MATCH (b)
RETURN b;`.trimStart();
verifyFormatting(query, expected);
});
test('multiline block comment with line before it', () => {
const query = `
MATCH (a)
RETURN a;
/* comment line 1
comment line 3 */
MATCH (b)
RETURN b;`;
const expected = `
MATCH (a)
RETURN a;
/* comment line 1
comment line 3 */
MATCH (b)
RETURN b;`.trimStart();
verifyFormatting(query, expected);
});
test('mixed comments with explicit newline', () => {
const query = `
MATCH (a)
// single line comment
/* block comment */
RETURN a`.trimStart();
const expected = `
MATCH (a)
// single line comment
/* block comment */
RETURN a`.trimStart();
verifyFormatting(query, expected);
});
test('should remove the first but not the second newline, and keep indentation', () => {
const query = `
MERGE (a:Person {name: "Alice"})
ON CREATE SET a.created = timestamp()
ON MATCH SET a.lastSeen = timestamp()
RETURN a
`.trimStart();
const expected = `
MERGE (a:Person {name: "Alice"})
ON CREATE SET a.created = timestamp()
ON MATCH SET a.lastSeen = timestamp()
RETURN a`.trimStart();
verifyFormatting(query, expected);
});
test('this query should not lose idempotency because of double break and concatenate', () => {
const query = `MATCH (a:AbCdEf {XyZ012345: "ABCDEFGH"})
MATCH (a)-[b:ZxCvBnMq]->(d:GhIjKlM)<-[e1:ZxCvBnMq]-(zYxWvUtSrqP:AbCdEf)
WHERE (
// abcdefghijklmnopqrstuvwxy
(b.AbCdEfGhIjKlMn <= "1A2b3C4d")
// Qwertyuiopasdfghjklzxcvbnm1234567890AB
OR (
e1.OpQrStUvW >= "Z9y8X7w6"
AND b.AbCdEfGhIjKlMn in ["aBcDeFgH","IjKlMnOp"]
)
// QazwsxedcrfvtgbyhnujmikolpASDFGHJKLQWERTYUIOPZXCVBNM1234567abcdefghij
OR (
b.OpQrStUvW <= e1.OpQrStUvW
AND e1.OpQrStUvW >= "QrStUvWx"
AND b.AbCdEfGhIjKlMn in ["YzXwVuTs","LmNoPqRs"]
// AND b.LmNo_PqRsTuV = e1.LmNo_PqRsTuV
)
// 0123456789abcdefghijKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
OR (
b.OpQrStUvW = e1.OpQrStUvW
AND e1.OpQrStUvW >= "StUvWxYz"
AND b.AbCdEfGhIjKlMn > "QwErTyUi"
// AND b.LmNo_PqRsTuV = e1.LmNo_PqRsTuV
)
)
RETURN a, b, d, e1, zYxWvUtSrqP`;
const expected = `MATCH (a:AbCdEf {XyZ012345: "ABCDEFGH"})
MATCH (a)-[b:ZxCvBnMq]->(d:GhIjKlM)<-[e1:ZxCvBnMq]-(zYxWvUtSrqP:AbCdEf)
WHERE (
// abcdefghijklmnopqrstuvwxy
(b.AbCdEfGhIjKlMn <= "1A2b3C4d")
// Qwertyuiopasdfghjklzxcvbnm1234567890AB
OR (e1.OpQrStUvW >= "Z9y8X7w6" AND
b.AbCdEfGhIjKlMn IN ["aBcDeFgH", "IjKlMnOp"])
// QazwsxedcrfvtgbyhnujmikolpASDFGHJKLQWERTYUIOPZXCVBNM1234567abcdefghij
OR (b.OpQrStUvW <= e1.OpQrStUvW AND e1.OpQrStUvW >= "QrStUvWx" AND
b.AbCdEfGhIjKlMn IN ["YzXwVuTs", "LmNoPqRs"])
// AND b.LmNo_PqRsTuV = e1.LmNo_PqRsTuV
// 0123456789abcdefghijKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
OR (b.OpQrStUvW = e1.OpQrStUvW AND e1.OpQrStUvW >= "StUvWxYz" AND
b.AbCdEfGhIjKlMn > "QwErTyUi"))
// AND b.LmNo_PqRsTuV = e1.LmNo_PqRsTuV
RETURN a, b, d, e1, zYxWvUtSrqP`;
verifyFormatting(query, expected);
});
test('should preserve newlines also after a comment', () => {
const query = `
MATCH (n)
// Comment, please preserve the line below
RETURN n`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});
test('should keep the logical delimiter the user has made intact', () => {
const query = `
MATCH (n)
// === THIS IS THE START OF THE RETURN STATEMENT ===
RETURN n`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});
test('should allow an explicit newline between MATCH and WHERE', () => {
const query = `
MATCH (n)
WHERE n.prop = "String"
RETURN n`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});
test('should allow an explicit newline before LIMIT', () => {
const query = `
MATCH (n)
RETURN n
LIMIT 10`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});
test('WHERE and LIMIT with comments in between', () => {
const query = `
MATCH (n)
// Comment before WHERE
WHERE n.prop = "String"
RETURN n
// Comment before LIMIT
LIMIT 10`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});
test('two statements', () => {
const query = `
MATCH (n)
RETURN n;
MATCH (n)
RETURN m;`;
const expected = query.trimStart();
verifyFormatting(query, expected);
});
test('multiple statements with comments inbetween', () => {
const query = `
MATCH (n)
RETURN n;
// This is a comment
MATCH (n)
RETURN n;
// This is another comment
MATCH (n)
RETURN n;`;
const expected = query.trimStart();
verifyFormatting(query, expected);
});
test('end of statement with multiple newlines', () => {
const query = `
MATCH (n)
RETURN m;
`;
const expected = `MATCH (n)
RETURN m;`;
verifyFormatting(query, expected);
});
test('too many newlines between statements should truncate to one', () => {
const query = `
MATCH (n)
RETURN n;
MATCH (n)
RETURN m;`;
const expected = `
MATCH (n)
RETURN n;
MATCH (n)
RETURN m;`.trimStart();
verifyFormatting(query, expected);
});
});