-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NotificationAcceptanceTest.java
1074 lines (919 loc) · 45.9 KB
/
NotificationAcceptanceTest.java
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
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.javacompat;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.neo4j.graphdb.InputPosition;
import org.neo4j.graphdb.Notification;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.SeverityLevel;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.impl.notification.NotificationCode;
import org.neo4j.graphdb.impl.notification.NotificationDetail;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.procedure.Procedure;
import org.neo4j.test.rule.ImpermanentDatabaseRule;
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.graphdb.impl.notification.NotificationCode.CREATE_UNIQUE_UNAVAILABLE_FALLBACK;
import static org.neo4j.graphdb.impl.notification.NotificationCode.EAGER_LOAD_CSV;
import static org.neo4j.graphdb.impl.notification.NotificationCode.INDEX_HINT_UNFULFILLABLE;
import static org.neo4j.graphdb.impl.notification.NotificationCode.LENGTH_ON_NON_PATH;
import static org.neo4j.graphdb.impl.notification.NotificationCode.RULE_PLANNER_UNAVAILABLE_FALLBACK;
import static org.neo4j.graphdb.impl.notification.NotificationCode.RUNTIME_UNSUPPORTED;
import static org.neo4j.graphdb.impl.notification.NotificationCode.UNBOUNDED_SHORTEST_PATH;
import static org.neo4j.graphdb.impl.notification.NotificationDetail.Factory.index;
public class NotificationAcceptanceTest
{
@Rule
public final ImpermanentDatabaseRule rule = new ImpermanentDatabaseRule();
@Test
public void shouldNotifyWhenUsingCypher3_1ForTheRulePlannerWhenCypherVersionIsTheDefault() throws Exception
{
// when
Result result = db().execute( "CYPHER planner=rule RETURN 1" );
InputPosition position = new InputPosition( 20, 1, 21 );
// then
assertThat( result.getNotifications(), Matchers.contains( RULE_PLANNER_UNAVAILABLE_FALLBACK.notification( position ) ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( "CYPHER 3.1" ) );
assertThat( arguments.get( "planner" ), equalTo( "RULE" ) );
result.close();
}
@Test
public void shouldNotifyWhenUsingCypher3_1ForTheRulePlannerWhenCypherVersionIs3_3() throws Exception
{
// when
Result result = db().execute( "CYPHER 3.3 planner=rule RETURN 1" );
InputPosition position = new InputPosition( 24, 1, 25 );
// then
assertThat( result.getNotifications(), Matchers.contains( RULE_PLANNER_UNAVAILABLE_FALLBACK.notification( position ) ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( "CYPHER 3.1" ) );
assertThat( arguments.get( "planner" ), equalTo( "RULE" ) );
result.close();
}
@Test
public void shouldNotifyWhenUsingCypher3_1ForTheRulePlannerWhenCypherVersionIs3_2() throws Exception
{
// when
Result result = db().execute( "CYPHER 3.2 planner=rule RETURN 1" );
InputPosition position = new InputPosition( 24, 1, 25 );
// then
assertThat( result.getNotifications(), Matchers.contains( RULE_PLANNER_UNAVAILABLE_FALLBACK.notification( position ) ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( "CYPHER 3.1" ) );
assertThat( arguments.get( "planner" ), equalTo( "RULE" ) );
result.close();
}
@Test
public void shouldNotNotifyWhenUsingTheRulePlannerWhenCypherVersionIsNot3_2() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 2.3" ).forEach( version ->
{
// when
Result result = db().execute( version + " planner=rule RETURN 1" );
// then
assertThat( Iterables.asList( result.getNotifications() ), empty() );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( version ) );
assertThat( arguments.get( "planner" ), equalTo( "RULE" ) );
result.close();
} );
}
@Test
public void shouldWarnWhenRequestingCompiledRuntimeOnUnsupportedQuery() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotifyInStream( version, "EXPLAIN CYPHER runtime=compiled MATCH (a)-->(b), (c)-->(d) RETURN count(*)", InputPosition.empty,
RUNTIME_UNSUPPORTED ) );
}
@Test
public void shouldWarnWhenRequestingSlottedRuntimeOnUnsupportedQuery() throws Exception
{
Stream.of( "CYPHER 3.3" ).forEach(
version -> shouldNotifyInStream( version, "explain cypher runtime=slotted merge (a)-[:X]->(b)", InputPosition.empty, RUNTIME_UNSUPPORTED ) );
}
@Test
public void shouldNotifyWhenUsingCreateUniqueWhenCypherVersionIsDefault() throws Exception
{
// when
Result result = db().execute( "MATCH (b) WITH b LIMIT 1 CREATE UNIQUE (b)-[:REL]->()" );
InputPosition position = new InputPosition( 25, 1, 26 );
// then
assertThat( result.getNotifications(),
Matchers.contains( CREATE_UNIQUE_UNAVAILABLE_FALLBACK.notification( position ) ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( "CYPHER 3.1" ) );
result.close();
}
@Test
public void shouldNotifyWhenUsingCreateUniqueWhenCypherVersionIs3_3() throws Exception
{
// when
Result result = db().execute( "CYPHER 3.3 MATCH (b) WITH b LIMIT 1 CREATE UNIQUE (b)-[:REL]->()" );
InputPosition position = new InputPosition( 36, 1, 37 );
// then
assertThat( result.getNotifications(), Matchers.contains( CREATE_UNIQUE_UNAVAILABLE_FALLBACK.notification( position ) ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( "CYPHER 3.1" ) );
result.close();
}
@Test
public void shouldNotifyWhenUsingCreateUniqueWhenCypherVersionIs3_2() throws Exception
{
// when
Result result = db().execute( "CYPHER 3.2 MATCH (b) WITH b LIMIT 1 CREATE UNIQUE (b)-[:REL]->()" );
InputPosition position = new InputPosition( 36, 1, 37 );
// then
assertThat( result.getNotifications(), Matchers.contains( CREATE_UNIQUE_UNAVAILABLE_FALLBACK.notification( position ) ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( "CYPHER 3.1" ) );
result.close();
}
@Test
public void shouldNotNotifyWhenUsingCreateUniqueWhenCypherVersionIsNot3_2() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 2.3" ).forEach(
version -> shouldNotNotifyInStream( version, " MATCH (b) WITH b LIMIT 1 CREATE UNIQUE (b)-[:REL]->()" ) );
}
@Test
public void shouldWarnWhenUsingLengthOnNonPath() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
// pattern
shouldNotifyInStream( version, "explain match (a) where a.name='Alice' return length((a)-->()-->())", new InputPosition( 63, 1, 64 ),
LENGTH_ON_NON_PATH );
// collection
shouldNotifyInStream( version, " explain return length([1, 2, 3])", new InputPosition( 33, 1, 34 ), LENGTH_ON_NON_PATH );
// string
shouldNotifyInStream( version, " explain return length('a string')", new InputPosition( 33, 1, 34 ), LENGTH_ON_NON_PATH );
} );
}
@Test
public void shouldNotNotifyWhenUsingLengthOnPath() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, " explain match p=(a)-[*]->(b) return length(p)" ) );
}
@Test
public void shouldNotNotifyWhenUsingSizeOnCollection() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, "explain return size([1, 2, 3])" ) );
}
@Test
public void shouldNotNotifyWhenUsingSizeOnString() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, " explain return size('a string')" ) );
}
@Test
public void shouldNotNotifyForCostUnsupportedUpdateQueryIfPlannerNotExplicitlyRequested() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, " EXPLAIN MATCH (n:Movie) SET n.title = 'The Movie'" ) );
}
@Test
public void shouldNotNotifyForCostSupportedUpdateQuery() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
shouldNotNotifyInStream( version, "EXPLAIN CYPHER planner=cost MATCH (n:Movie) SET n:Seen" );
shouldNotNotifyInStream( version, "EXPLAIN CYPHER planner=idp MATCH (n:Movie) SET n:Seen" );
shouldNotNotifyInStream( version, "EXPLAIN CYPHER planner=dp MATCH (n:Movie) SET n:Seen" );
} );
}
@Test
public void shouldNotNotifyUsingJoinHintWithCost() throws Exception
{
List<String> queries = Arrays.asList( "CYPHER planner=cost EXPLAIN MATCH (a)-->(b) USING JOIN ON b RETURN a, b",
"CYPHER planner=cost EXPLAIN MATCH (a)-->(x)<--(b) USING JOIN ON x RETURN a, b" );
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
for ( String query : queries )
{
assertNotifications( version + query, containsNoItem( joinHintUnsuportedWarning ) );
}
} );
}
@Test
public void shouldWarnOnPotentiallyCachedQueries() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
assertNotifications( version + "explain match (a)-->(b), (c)-->(d) return *", containsItem( cartesianProductWarning ) );
// no warning without explain
shouldNotNotifyInStream( version, "match (a)-->(b), (c)-->(d) return *" );
} );
}
@Test
public void shouldWarnOnceWhenSingleIndexHintCannotBeFulfilled() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotifyInStreamWithDetail( version, " EXPLAIN MATCH (n:Person) USING INDEX n:Person(name) WHERE n.name = 'John' RETURN n",
InputPosition.empty, INDEX_HINT_UNFULFILLABLE, index( "Person", "name" ) ) );
}
@Test
public void shouldWarnOnEachUnfulfillableIndexHint() throws Exception
{
String query = " EXPLAIN MATCH (n:Person), (m:Party), (k:Animal) " + "USING INDEX n:Person(name) " + "USING INDEX m:Party(city) " +
"USING INDEX k:Animal(species) " + "WHERE n.name = 'John' AND m.city = 'Reykjavik' AND k.species = 'Sloth' " + "RETURN n";
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
shouldNotifyInStreamWithDetail( version, query, InputPosition.empty, INDEX_HINT_UNFULFILLABLE, index( "Person", "name" ) );
shouldNotifyInStreamWithDetail( version, query, InputPosition.empty, INDEX_HINT_UNFULFILLABLE, index( "Party", "city" ) );
shouldNotifyInStreamWithDetail( version, query, InputPosition.empty, INDEX_HINT_UNFULFILLABLE, index( "Animal", "species" ) );
} );
}
@Test
public void shouldNotNotifyOnLiteralMaps() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> shouldNotNotifyInStream( version, " explain return { id: 42 } " ) );
}
@Test
public void shouldNotNotifyOnNonExistingLabelUsingLoadCSV() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
// create node
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row CREATE (n:Category)" );
// merge node
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row MERGE (n:Category)" );
// set label to node
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row CREATE (n) SET n:Category" );
} );
}
@Test
public void shouldNotNotifyOnNonExistingRelTypeUsingLoadCSV() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
// create rel
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row CREATE ()-[:T]->()" );
// merge rel
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row MERGE ()-[:T]->()" );
} );
}
@Test
public void shouldNotNotifyOnNonExistingPropKeyIdUsingLoadCSV() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
// create node
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row CREATE (n) SET n.p = 'a'" );
// merge node
shouldNotNotifyInStream( version, " EXPLAIN LOAD CSV WITH HEADERS FROM 'file:///fake.csv' AS row MERGE (n) ON CREATE SET n.p = 'a'" );
} );
}
@Test
public void shouldNotNotifyOnEagerBeforeLoadCSVDelete() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> shouldNotNotifyInStream( version,
"EXPLAIN MATCH (n) DELETE n WITH * LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MERGE () RETURN line" ) );
}
@Test
public void shouldNotNotifyOnEagerBeforeLoadCSVCreate() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
assertNotifications( version +"EXPLAIN MATCH (a), (b) CREATE (c) WITH c LOAD CSV FROM 'file:///ignore/ignore.csv' AS line RETURN *",
containsNoItem( eagerOperatorWarning ) ) );
}
@Test
public void shouldWarnOnEagerAfterLoadCSV() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> shouldNotifyInStream( version,
"EXPLAIN MATCH (n) LOAD CSV FROM 'file:///ignore/ignore.csv' AS line WITH * DELETE n MERGE () RETURN line", InputPosition.empty,
EAGER_LOAD_CSV ) );
}
@Test
public void shouldNotNotifyOnLoadCSVWithoutEager() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MATCH (:A) CREATE (:B) RETURN line" ) );
}
@Test
public void shouldNotNotifyOnEagerWithoutLoadCSV() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> assertNotifications( version + "EXPLAIN MATCH (a), (b) CREATE (c) RETURN *", containsNoItem( eagerOperatorWarning ) ) );
}
@Test
public void shouldWarnOnLargeLabelScansWithLoadCVSMatch() throws Exception
{
for ( int i = 0; i < 11; i++ )
{
try ( Transaction tx = db().beginTx() )
{
db().createNode().addLabel( label( "A" ) );
tx.success();
}
}
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> assertNotifications( version + "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MATCH (a:A) RETURN *",
containsNoItem( largeLabelCSVWarning ) ) );
}
@Test
public void shouldWarnOnLargeLabelScansWithLoadCVSMerge() throws Exception
{
for ( int i = 0; i < 11; i++ )
{
try ( Transaction tx = db().beginTx() )
{
db().createNode().addLabel( label( "A" ) );
tx.success();
}
}
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> assertNotifications( version + "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MERGE (a:A) RETURN *",
containsNoItem( largeLabelCSVWarning ) ) );
}
@Test
public void shouldNotWarnOnSmallLabelScansWithLoadCVS() throws Exception
{
try ( Transaction tx = db().beginTx() )
{
db().createNode().addLabel( label( "A" ) );
tx.success();
}
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
shouldNotNotifyInStream( version, "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MATCH (a:A) RETURN *" );
shouldNotNotifyInStream( version, "EXPLAIN LOAD CSV FROM 'file:///ignore/ignore.csv' AS line MERGE (a:A) RETURN *" );
} );
}
@Test
public void shouldWarnOnDeprecatedToInt() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
assertNotifications( version + " EXPLAIN RETURN toInt('1') AS one", containsItem( deprecatedFeatureWarning ) ) );
}
@Test
public void shouldWarnOnDeprecatedUpper() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
assertNotifications( version + " EXPLAIN RETURN upper('foo') AS one", containsItem( deprecatedFeatureWarning ) ) );
}
@Test
public void shouldWarnOnDeprecatedLower() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
assertNotifications( version + " EXPLAIN RETURN lower('BAR') AS one", containsItem( deprecatedFeatureWarning ) ) );
}
@Test
public void shouldWarnOnDeprecatedRels() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
assertNotifications( version + " EXPLAIN MATCH p = ()-->() RETURN rels(p) AS r", containsItem( deprecatedFeatureWarning ) ) );
}
@Test
public void shouldWarnOnDeprecatedProcedureCalls() throws Exception
{
db().getDependencyResolver().provideDependency( Procedures.class ).get().registerProcedure( TestProcedures.class );
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
assertNotifications( version + "explain CALL oldProc()", containsItem( deprecatedProcedureWarning ) );
assertNotifications( version + "explain CALL oldProc() RETURN 1", containsItem( deprecatedProcedureWarning ) );
} );
}
@Test
public void shouldWarnOnDeprecatedProcedureResultField() throws Exception
{
db().getDependencyResolver().provideDependency( Procedures.class ).get().registerProcedure( TestProcedures.class );
Stream.of( "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> assertNotifications( version + "explain CALL changedProc() YIELD oldField RETURN oldField",
containsItem( deprecatedProcedureReturnFieldWarning ) ) );
}
@Test
public void shouldWarnOnUnboundedShortestPath() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotifyInStream( version, "EXPLAIN MATCH p = shortestPath((n)-[*]->(m)) RETURN m", new InputPosition( 44, 1, 45 ),
UNBOUNDED_SHORTEST_PATH ) );
}
@Test
public void shouldNotNotifyOnDynamicPropertyLookupWithNoLabels() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "Call db.awaitIndexes()" );
shouldNotNotifyInStream( version, "EXPLAIN MATCH (n) WHERE n['key-' + n.name] = 'value' RETURN n" );
} );
}
@Test
public void shouldWarnOnDynamicPropertyLookupWithBothStaticAndDynamicProperties() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "Call db.awaitIndexes()" );
assertNotifications( version + "EXPLAIN MATCH (n:Person) WHERE n.name = 'Tobias' AND n['key-' + n.name] = 'value' RETURN n",
containsItem( dynamicPropertyWarning ));
} );
}
@Test
public void shouldNotNotifyOnDynamicPropertyLookupWithLabelHavingNoIndex() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "Call db.awaitIndexes()" );
try ( Transaction tx = db().beginTx() )
{
db().createNode().addLabel( label( "Foo" ) );
tx.success();
}
shouldNotNotifyInStream( version, "EXPLAIN MATCH (n:Foo) WHERE n['key-' + n.name] = 'value' RETURN n" );
} );
}
@Test
public void shouldWarnOnUnfulfillableIndexSeekUsingDynamicProperty() throws Exception
{
List<String> queries = new ArrayList<>();
// dynamic property lookup with single label
queries.add( "EXPLAIN MATCH (n:Person) WHERE n['key-' + n.name] = 'value' RETURN n" );
// dynamic property lookup with explicit label check
queries.add( "EXPLAIN MATCH (n) WHERE n['key-' + n.name] = 'value' AND (n:Person) RETURN n" );
// dynamic property lookup with range seek
queries.add( "EXPLAIN MATCH (n:Person) WHERE n['key-' + n.name] > 10 RETURN n" );
// dynamic property lookup with range seek (reverse)
queries.add( "EXPLAIN MATCH (n:Person) WHERE 10 > n['key-' + n.name] RETURN n" );
// dynamic property lookup with a single label and property existence check with exists
queries.add( "EXPLAIN MATCH (n:Person) WHERE exists(n['na' + 'me']) RETURN n" );
// dynamic property lookup with a single label and starts with
queries.add( "EXPLAIN MATCH (n:Person) WHERE n['key-' + n.name] STARTS WITH 'Foo' RETURN n" );
// dynamic property lookup with a single label and regex
queries.add( "EXPLAIN MATCH (n:Person) WHERE n['key-' + n.name] =~ 'Foo*' RETURN n" );
// dynamic property lookup with a single label and IN
queries.add( "EXPLAIN MATCH (n:Person) WHERE n['key-' + n.name] IN ['Foo', 'Bar'] RETURN n" );
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
for ( String query : queries )
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "Call db.awaitIndexes()" );
assertNotifications( version + query, containsItem( dynamicPropertyWarning ) );
}
} );
}
@Test
public void shouldNotNotifyOnDynamicPropertyLookupWithSingleLabelAndNegativePredicate() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "Call db.awaitIndexes()" );
shouldNotNotifyInStream( version, "EXPLAIN MATCH (n:Person) WHERE n['key-' + n.name] <> 'value' RETURN n" );
} );
}
@Test
public void shouldWarnOnUnfulfillableIndexSeekUsingDynamicPropertyAndMultipleLabels() throws Exception
{
Stream.of( "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "Call db.awaitIndexes()" );
assertNotifications( version + "EXPLAIN MATCH (n:Person:Foo) WHERE n['key-' + n.name] = 'value' RETURN n",
containsItem( dynamicPropertyWarning ) );
} );
}
@Test
public void shouldWarnOnUnfulfillableIndexSeekUsingDynamicPropertyAndMultipleIndexedLabels() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
db().execute( "CREATE INDEX ON :Person(name)" );
db().execute( "CREATE INDEX ON :Jedi(weapon)" );
db().execute( "Call db.awaitIndexes()" );
assertNotifications( version + "EXPLAIN MATCH (n:Person:Jedi) WHERE n['key-' + n.name] = 'value' RETURN n",
containsItem( dynamicPropertyWarning ) );
} );
}
@Test
public void shouldWarnOnFutureAmbiguousRelTypeSeparator() throws Exception
{
List<String> deprecatedQueries = Arrays.asList( "explain MATCH (a)-[:A|:B|:C {foo:'bar'}]-(b) RETURN a,b", "explain MATCH (a)-[x:A|:B|:C]-() RETURN a",
"explain MATCH (a)-[:A|:B|:C*]-() RETURN a" );
List<String> nonDeprecatedQueries =
Arrays.asList( "explain MATCH (a)-[:A|B|C {foo:'bar'}]-(b) RETURN a,b", "explain MATCH (a)-[:A|:B|:C]-(b) RETURN a,b",
"explain MATCH (a)-[:A|B|C]-(b) RETURN a,b" );
for ( String query : deprecatedQueries )
{
assertNotifications( "CYPHER 3.3 " + query, containsItem( deprecatedSeparatorWarning ) );
}
for ( String query : nonDeprecatedQueries )
{
assertNotifications( "CYPHER 3.3 " + query, containsNoItem( deprecatedSeparatorWarning ) );
}
}
@Test
public void shouldWarnOnBindingVariableLengthRelationship() throws Exception
{
assertNotifications( "CYPHER 3.3 explain MATCH ()-[rs*]-() RETURN rs", containsItem( depracatedBindingWarning ) );
assertNotifications( "CYPHER 3.3 explain MATCH p = ()-[*]-() RETURN relationships(p) AS rs", containsNoItem( depracatedBindingWarning ) );
}
@Test
public void shouldWarnOnCartesianProduct() throws Exception
{
Stream.of( "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version ->
{
assertNotifications( version + "explain match (a)-->(b), (c)-->(d) return *", containsItem( cartesianProductWarning ) );
assertNotifications( version + "explain cypher runtime=compiled match (a)-->(b), (c)-->(d) return *", containsItem( cartesianProductWarning ) );
assertNotifications( version + "explain cypher runtime=interpreted match (a)-->(b), (c)-->(d) return *", containsItem( cartesianProductWarning ) );
} );
}
@Test
public void shouldNotNotifyOnCartesianProductWithoutExplain() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, " match (a)-->(b), (c)-->(d) return *" ) );
}
@Test
public void shouldWarnOnMissingLabel() throws Exception
{
assertNotifications( "EXPLAIN MATCH (a:NO_SUCH_THING) RETURN a", containsItem( unknownLabelWarning ) );
}
@Test
public void shouldWarnOnMisspelledLabel() throws Exception {
try ( Transaction tx = db().beginTx() )
{
db().createNode().addLabel( label( "Person" ) );
tx.success();
}
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> {
assertNotifications(version +"EXPLAIN MATCH (n:Preson) RETURN *", containsItem( unknownLabelWarning ) );
shouldNotNotifyInStream( version, "EXPLAIN MATCH (n:Person) RETURN *" );
});
}
@Test
public void shouldWarnOnMissingLabelWithCommentInBeginningWithOlderCypherVersions() throws Exception
{
assertNotifications( "CYPHER 2.3 EXPLAIN//TESTING \nMATCH (n:X) return n Limit 1", containsItem( unknownLabelWarning ) );
assertNotifications( "CYPHER 3.1 EXPLAIN//TESTING \nMATCH (n:X) return n Limit 1", containsItem( unknownLabelWarning ) );
}
@Test
public void shouldWarnOnMissingLabelWithCommentInBeginning() throws Exception
{
assertNotifications( "EXPLAIN//TESTING \nMATCH (n:X) return n Limit 1", containsItem( unknownLabelWarning ) );
}
@Test
public void shouldWarnOnMissingLabelWithCommentInBeginningTwoLines() throws Exception
{
assertNotifications( "//TESTING \n //TESTING \n EXPLAIN MATCH (n)\n MATCH (b:X) return n,b Limit 1", containsItem( unknownLabelWarning ) );
}
@Test
public void shouldWarnOnMissingLabelWithCommentInBeginningOnOneLine() throws Exception
{
assertNotifications( "explain /* Testing */ MATCH (n:X) RETURN n", containsItem( unknownLabelWarning ) );
}
@Test
public void shouldWarnOnMissingLabelWithCommentInMiddel() throws Exception
{
assertNotifications( "EXPLAIN\nMATCH (n)\n//TESTING \nMATCH (n:X)\nreturn n Limit 1", containsItem( unknownLabelWarning ) );
}
@Test
public void shouldNotNotifyForMissingLabelOnUpdate() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, " EXPLAIN CREATE (n:Person)" ) );
}
@Test
public void shouldWarnOnMissingRelationshipType() throws Exception
{
assertNotifications( "EXPLAIN MATCH ()-[a:NO_SUCH_THING]->() RETURN a", containsItem( unknownRelationshipWarning ) );
}
@Test
public void shouldWarnOnMisspelledRelationship() throws Exception {
try ( Transaction tx = db().beginTx() )
{
db().createNode().addLabel( label( "Person" ) );
tx.success();
}
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> {
db().execute( "CREATE (n)-[r:R]->(m)");
assertNotifications(version + "EXPLAIN MATCH ()-[r:r]->() RETURN *", containsItem( unknownRelationshipWarning ) );
shouldNotNotifyInStream( version, "EXPLAIN MATCH ()-[r:R]->() RETURN *" );
});
}
@Test
public void shouldWarnOnMissingRelationshipTypeWithComment() throws Exception
{
assertNotifications( "EXPLAIN /*Comment*/ MATCH ()-[a:NO_SUCH_THING]->() RETURN a", containsItem( unknownRelationshipWarning ) );
}
@Test
public void shouldWarnOnMissingProperty() throws Exception
{
assertNotifications( "EXPLAIN MATCH (a {NO_SUCH_THING: 1337}) RETURN a", containsItem( unknownPropertyKeyWarning ) );
}
@Test
public void shouldWarnOnMisspelledProperty() throws Exception {
db().execute("CREATE (n {prop : 42})");
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach( version -> {
db().execute( "CREATE (n)-[r:R]->(m)");
assertNotifications(version + "EXPLAIN MATCH (n) WHERE n.propp = 43 RETURN n", containsItem( unknownPropertyKeyWarning ) );
shouldNotNotifyInStream( version, "EXPLAIN MATCH (n) WHERE n.prop = 43 RETURN n" );
});
}
@Test
public void shouldWarnOnMissingPropertyWithComment() throws Exception
{
assertNotifications( "EXPLAIN /*Comment*/ MATCH (a {NO_SUCH_THING: 1337}) RETURN a", containsItem( unknownPropertyKeyWarning ) );
}
@Test
public void shouldNotNotifyForMissingPropertiesOnUpdate() throws Exception
{
Stream.of( "CYPHER 2.3", "CYPHER 3.1", "CYPHER 3.2", "CYPHER 3.3" ).forEach(
version -> shouldNotNotifyInStream( version, " EXPLAIN CREATE (n {prop: 42})" ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForAllNodeScan()
{
assertNotifications( "EXPLAIN START n=node(*) RETURN n", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForNodeById()
{
assertNotifications( "EXPLAIN START n=node(1337) RETURN n", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForNodeByIds()
{
assertNotifications( "EXPLAIN START n=node(42,1337) RETURN n", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForNodeIndexSeek()
{
try ( Transaction ignore = db().beginTx() )
{
db().index().forNodes( "index" );
}
assertNotifications( "EXPLAIN START n=node:index(key = 'value') RETURN n", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForNodeIndexSearch()
{
try ( Transaction ignore = db().beginTx() )
{
db().index().forNodes( "index" );
}
assertNotifications( "EXPLAIN START n=node:index('key:value*') RETURN n", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForAllRelScan()
{
assertNotifications( "EXPLAIN START r=relationship(*) RETURN r", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForRelById()
{
assertNotifications( "EXPLAIN START r=relationship(1337) RETURN r", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForRelByIds()
{
assertNotifications( "EXPLAIN START r=relationship(42,1337) RETURN r", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForRelIndexSeek()
{
try ( Transaction ignore = db().beginTx() )
{
db().index().forRelationships( "index" );
}
assertNotifications( "EXPLAIN START r=relationship:index(key = 'value') RETURN r", containsItem( deprecatedStartWarning ) );
}
@Test
public void shouldWarnThatStartIsDeprecatedForRelIndexSearch()
{
try ( Transaction ignore = db().beginTx() )
{
db().index().forRelationships( "index" );
}
assertNotifications( "EXPLAIN START r=relationship:index('key:value*') RETURN r", containsItem( deprecatedStartWarning ) );
}
@Test
public void version2_3ShouldWarnAboutBareNodes() throws Exception {
Result res = db().execute("EXPLAIN CYPHER 2.3 MATCH n RETURN n");
assert res.getNotifications().iterator().hasNext();
}
private void assertNotifications( String query, Matcher<Iterable<Notification>> matchesExpectation )
{
try ( Result result = db().execute( query ) )
{
assertThat( result.getNotifications(), matchesExpectation );
}
}
private Matcher<Notification> notification(
String code,
Matcher<String> description,
Matcher<InputPosition> position,
SeverityLevel severity )
{
return new TypeSafeMatcher<Notification>()
{
@Override
protected boolean matchesSafely( Notification item )
{
return code.equals( item.getCode() ) &&
description.matches( item.getDescription() ) &&
position.matches( item.getPosition() ) &&
severity.equals( item.getSeverity() );
}
@Override
public void describeTo( Description target )
{
target.appendText( "Notification{code=" ).appendValue( code )
.appendText( ", description=[" ).appendDescriptionOf( description )
.appendText( "], position=[" ).appendDescriptionOf( position )
.appendText( "], severity=" ).appendValue( severity )
.appendText( "}" );
}
};
}
private GraphDatabaseAPI db()
{
return rule.getGraphDatabaseAPI();
}
private <T> Matcher<Iterable<T>> containsItem( Matcher<T> itemMatcher )
{
return new TypeSafeMatcher<Iterable<T>>()
{
@Override
protected boolean matchesSafely( Iterable<T> items )
{
for ( T item : items )
{
if ( itemMatcher.matches( item ) )
{
return true;
}
}
return false;
}
@Override
public void describeTo( Description description )
{
description.appendText( "an iterable containing " ).appendDescriptionOf( itemMatcher );
}
};
}
private <T> Matcher<Iterable<T>> containsNoItem( Matcher<T> itemMatcher )
{
return new TypeSafeMatcher<Iterable<T>>()
{
@Override
protected boolean matchesSafely( Iterable<T> items )
{
for ( T item : items )
{
if ( itemMatcher.matches( item ) )
{
return false;
}
}
return true;
}
@Override
public void describeTo( Description description )
{
description.appendText( "an iterable not containing " ).appendDescriptionOf( itemMatcher );
}
};
}
private void shouldNotifyInStream( String version, String query, InputPosition pos, NotificationCode code )
{
//when
Result result = db().execute( version + query );
//then
NotificationCode.Notification notification = code.notification( pos );
assertThat( Iterables.asList( result.getNotifications() ), Matchers.hasItems( notification ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( version ) );
result.close();
}
private void shouldNotifyInStreamWithDetail( String version, String query, InputPosition pos, NotificationCode code, NotificationDetail detail )
{
//when
Result result = db().execute( version + query );
//then
NotificationCode.Notification notification = code.notification( pos, detail );
assertThat( Iterables.asList( result.getNotifications() ), Matchers.hasItems( notification ) );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( version ) );
result.close();
}
private void shouldNotNotifyInStream( String version, String query )
{
// when
Result result = db().execute( version + query );
// then
assertThat( Iterables.asList( result.getNotifications() ), empty() );
Map<String,Object> arguments = result.getExecutionPlanDescription().getArguments();
assertThat( arguments.get( "version" ), equalTo( version ) );
result.close();
}
public static class ChangedResults
{
@Deprecated
public final String oldField = "deprecated";
public final String newField = "use this";
}
public static class TestProcedures
{
@Procedure( "newProc" )
public void newProc()
{
}
@Deprecated