-
Notifications
You must be signed in to change notification settings - Fork 8
/
RedisGraphAPITest.cs
1017 lines (816 loc) · 41.2 KB
/
RedisGraphAPITest.cs
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
// .NET port of https://github.com/RedisGraph/JRedisGraph
using NRedisGraph.Tests.Utils;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
using static NRedisGraph.Header;
using static NRedisGraph.Statistics;
namespace NRedisGraph.Tests
{
public class RedisGraphAPITest : BaseTest
{
private ConnectionMultiplexer _muxr;
private RedisGraph _api;
public RedisGraphAPITest() : base()
{
}
protected override void BeforeTest()
{
_muxr = ConnectionMultiplexer.Connect(RedisConnectionString);
_muxr.GetDatabase().Execute("FLUSHDB");
_api = new RedisGraph(_muxr.GetDatabase(0));
}
protected override void AfterTest()
{
_api = null;
_muxr.Dispose();
_muxr = null;
}
[Fact]
public void TestCreateNode()
{
// Create a node
ResultSet resultSet = _api.Query("social", "CREATE ({name:'roi',age:32})");
Assert.Equal(1, resultSet.Statistics.NodesCreated);
Assert.Null(resultSet.Statistics.GetStringValue(Label.NodesDeleted));
Assert.Null(resultSet.Statistics.GetStringValue(Label.RelationshipsCreated));
Assert.Null(resultSet.Statistics.GetStringValue(Label.RelationshipsDeleted));
Assert.Equal(2, resultSet.Statistics.PropertiesSet);
Assert.NotNull(resultSet.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
Assert.Empty(resultSet);
}
[Fact]
public void TestCreateLabeledNode()
{
// Create a node with a label
ResultSet resultSet = _api.Query("social", "CREATE (:human{name:'danny',age:12})");
Assert.Empty(resultSet);
Assert.Equal("1", resultSet.Statistics.GetStringValue(Label.NodesCreated));
Assert.Equal("2", resultSet.Statistics.GetStringValue(Label.PropertiesSet));
Assert.NotNull(resultSet.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
}
[Fact]
public void TestCreateLabeledNodeFireAndForget()
{
// Create a node with a label
ResultSet resultSet = _api.Query("social", "CREATE (:human{name:'danny',age:12})");
Assert.Empty(resultSet);
Assert.Equal("1", resultSet.Statistics.GetStringValue(Label.NodesCreated));
Assert.Equal("2", resultSet.Statistics.GetStringValue(Label.PropertiesSet));
Assert.NotNull(resultSet.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
}
[Fact]
public void TestConnectNodes()
{
// Create both source and destination nodes
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'roi',age:32})"));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'amit',age:30})"));
// Connect source and destination nodes.
ResultSet resultSet = _api.Query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)");
Assert.Empty(resultSet);
Assert.Null(resultSet.Statistics.GetStringValue(Label.NodesCreated));
Assert.Null(resultSet.Statistics.GetStringValue(Label.PropertiesSet));
Assert.Equal(1, resultSet.Statistics.RelationshipsCreated);
Assert.Equal(0, resultSet.Statistics.RelationshipsDeleted);
Assert.NotNull(resultSet.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
}
[Fact]
public void TestDeleteNodes()
{
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'roi',age:32})"));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'amit',age:30})"));
ResultSet deleteResult = _api.Query("social", "MATCH (a:person) WHERE (a.name = 'roi') DELETE a");
Assert.Empty(deleteResult);
Assert.Null(deleteResult.Statistics.GetStringValue(Label.NodesCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.PropertiesSet));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.RelationshipsCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.RelationshipsDeleted));
Assert.Equal(1, deleteResult.Statistics.NodesDeleted);
Assert.NotNull(deleteResult.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'roi',age:32})"));
Assert.NotNull(_api.Query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)"));
deleteResult = _api.Query("social", "MATCH (a:person) WHERE (a.name = 'roi') DELETE a");
Assert.Empty(deleteResult);
Assert.Null(deleteResult.Statistics.GetStringValue(Label.NodesCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.PropertiesSet));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.NodesCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.RelationshipsCreated));
Assert.Equal(1, deleteResult.Statistics.RelationshipsDeleted);
Assert.Equal(1, deleteResult.Statistics.NodesDeleted);
Assert.NotNull(deleteResult.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
}
[Fact]
public void TestDeleteRelationship()
{
var graphName = $"social_{MethodBase.GetCurrentMethod()}";
Assert.NotNull(_api.Query(graphName, "CREATE (:person{name:'roi',age:32})"));
Assert.NotNull(_api.Query(graphName, "CREATE (:person{name:'amit',age:30})"));
Assert.NotNull(_api.Query(graphName, "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(a)"));
ResultSet deleteResult = _api.Query(graphName, "MATCH (a:person)-[e]->() WHERE (a.name = 'roi') DELETE e");
Assert.Empty(deleteResult);
Assert.Null(deleteResult.Statistics.GetStringValue(Label.NodesCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.PropertiesSet));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.NodesCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.RelationshipsCreated));
Assert.Null(deleteResult.Statistics.GetStringValue(Label.NodesDeleted));
Assert.Equal(1, deleteResult.Statistics.RelationshipsDeleted);
Assert.NotNull(deleteResult.Statistics.GetStringValue(Label.QueryInternalExecutionTime));
}
[Fact]
public void TestIndex()
{
// Create both source and destination nodes
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'roi',age:32})"));
ResultSet createIndexResult = _api.Query("social", "CREATE INDEX ON :person(age)");
Assert.Empty(createIndexResult);
Assert.Equal(1, createIndexResult.Statistics.IndicesCreated);
// since RediSearch as index, those action are allowed
ResultSet createNonExistingIndexResult = _api.Query("social", "CREATE INDEX ON :person(age1)");
Assert.Empty(createNonExistingIndexResult);
Assert.NotNull(createNonExistingIndexResult.Statistics.GetStringValue(Label.IndicesCreated));
Assert.Equal(1, createNonExistingIndexResult.Statistics.IndicesCreated);
ResultSet createExistingIndexResult = _api.GraphQuery("social", "CREATE INDEX ON :person(age)");
Assert.Empty(createExistingIndexResult);
Assert.Equal(0, createExistingIndexResult.Statistics.IndicesCreated);
ResultSet deleteExistingIndexResult = _api.GraphQuery("social", "DROP INDEX ON :person(age)");
Assert.Empty(deleteExistingIndexResult);
Assert.Equal(1, deleteExistingIndexResult.Statistics.IndicesDeleted);
}
[Fact]
public void TestHeader()
{
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'roi',age:32})"));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'amit',age:30})"));
Assert.NotNull(_api.Query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"));
ResultSet queryResult = _api.Query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age");
Assert.NotNull(queryResult.Header);
Header header = queryResult.Header;
Assert.Equal("Header{"
+ "schemaTypes=[COLUMN_SCALAR, COLUMN_SCALAR, COLUMN_SCALAR], "
+ "schemaNames=[a, r, a.age]}", header.ToString());
List<string> schemaNames = header.SchemaNames;
List<Header.ResultSetColumnTypes> schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Equal(3, schemaNames.Count);
Assert.Equal(3, schemaTypes.Count);
Assert.Equal("a", schemaNames[0]);
Assert.Equal("r", schemaNames[1]);
Assert.Equal("a.age", schemaNames[2]);
}
[Fact]
public void TestRecord()
{
string name = "roi";
long age = 32;
double doubleValue = 3.14;
bool boolValue = true;
string place = "TLV";
long since = 2000;
Property nameProperty = new Property("name", name);
Property ageProperty = new Property("age", age);
Property doubleProperty = new Property("doubleValue", doubleValue);
Property trueBooleanProperty = new Property("boolValue", true);
Property falseBooleanProperty = new Property("boolValue", false);
Property nullProperty = new Property("nullValue", null);
Property placeProperty = new Property("place", place);
Property sinceProperty = new Property("since", since);
Node expectedNode = new Node();
expectedNode.Id = 0;
expectedNode.AddLabel("person");
expectedNode.AddProperty(nameProperty);
expectedNode.AddProperty(ageProperty);
expectedNode.AddProperty(doubleProperty);
expectedNode.AddProperty(trueBooleanProperty);
Assert.Equal(
"Node{labels=[person], id=0, propertyMap={name=Property{name='name', value=roi}, age=Property{name='age', value=32}, doubleValue=Property{name='doubleValue', value=3.14}, boolValue=Property{name='boolValue', value=True}}}",
expectedNode.ToString());
Edge expectedEdge = new Edge();
expectedEdge.Id = 0;
expectedEdge.Source = 0;
expectedEdge.Destination = 1;
expectedEdge.RelationshipType = "knows";
expectedEdge.AddProperty(placeProperty);
expectedEdge.AddProperty(sinceProperty);
expectedEdge.AddProperty(doubleProperty);
expectedEdge.AddProperty(falseBooleanProperty);
Assert.Equal(
"Edge{relationshipType='knows', source=0, destination=1, id=0, propertyMap={place=Property{name='place', value=TLV}, since=Property{name='since', value=2000}, doubleValue=Property{name='doubleValue', value=3.14}, boolValue=Property{name='boolValue', value=False}}}",
expectedEdge.ToString());
var parms = new Dictionary<string, object>
{
{"name", name},
{"age", age},
{"boolValue", boolValue},
{"doubleValue", doubleValue}
};
Assert.NotNull(_api.Query("social", "CREATE (:person{name:$name,age:$age, doubleValue:$doubleValue, boolValue:$boolValue, nullValue:null})", parms));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'amit',age:30})"));
Assert.NotNull(_api.Query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows{place:'TLV', since:2000,doubleValue:3.14, boolValue:false, nullValue:null}]->(b)"));
ResultSet resultSet = _api.Query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.name, a.age, a.doubleValue, a.boolValue, a.nullValue, r.place, r.since, r.doubleValue, r.boolValue, r.nullValue");
Assert.NotNull(resultSet);
Assert.Equal(0, resultSet.Statistics.NodesCreated);
Assert.Equal(0, resultSet.Statistics.NodesDeleted);
Assert.Equal(0, resultSet.Statistics.LabelsAdded);
Assert.Equal(0, resultSet.Statistics.PropertiesSet);
Assert.Equal(0, resultSet.Statistics.RelationshipsCreated);
Assert.Equal(0, resultSet.Statistics.RelationshipsDeleted);
Assert.NotNull(resultSet.Statistics.QueryInternalExecutionTime);
Assert.Single(resultSet);
Record record = resultSet.First();
Node node = record.GetValue<Node>(0);
Assert.NotNull(node);
Assert.Equal(expectedNode, node);
node = record.GetValue<Node>("a");
Assert.Equal(expectedNode, node);
Edge edge = record.GetValue<Edge>(1);
Assert.NotNull(edge);
Assert.Equal(expectedEdge, edge);
edge = record.GetValue<Edge>("r");
Assert.Equal(expectedEdge, edge);
Assert.Equal(new[] {"a", "r", "a.name", "a.age", "a.doubleValue", "a.boolValue", "a.nullValue", "r.place", "r.since", "r.doubleValue", "r.boolValue", "r.nullValue"}, record.Keys);
Assert.Equal(new List<object> {expectedNode, expectedEdge, name, age, doubleValue, true, null, place, since, doubleValue, false, null}, record.Values);
Assert.Equal("roi", record.GetString(2));
Assert.Equal("32", record.GetString(3));
Assert.Equal(32, record.GetValue<long>(3));
Assert.Equal(32, record.GetValue<long>("a.age"));
Assert.Equal("roi", record.GetString("a.name"));
Assert.Equal("32", record.GetString("a.age"));
}
[Fact]
public void TinyTestMultiThread()
{
ResultSet resultSet = _api.Query("social", "CREATE ({name:'roi',age:32})");
_api.Query("social", "MATCH (a:person) RETURN a");
for (int i = 0; i < 10000; i++)
{
var resultSets = Enumerable.Range(0, 16).AsParallel().Select(x => _api.Query("social", "MATCH (a:person) RETURN a"));
}
}
[Fact]
public void TestMultiThread()
{
Assert.NotNull(_api.Query("social", "CREATE (:person {name:'roi', age:32})-[:knows]->(:person {name:'amit',age:30}) "));
List<ResultSet> resultSets = Enumerable.Range(0, 16).AsParallel().Select(x => _api.Query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r, a.age")).ToList();
Property nameProperty = new Property("name", "roi");
Property ageProperty = new Property("age", 32L);
Property lastNameProperty = new Property("lastName", "a");
Node expectedNode = new Node();
expectedNode.Id = 0;
expectedNode.AddLabel("person");
expectedNode.AddProperty(nameProperty);
expectedNode.AddProperty(ageProperty);
Edge expectedEdge = new Edge();
expectedEdge.Id = 0;
expectedEdge.Source = 0;
expectedEdge.Destination = 1;
expectedEdge.RelationshipType = "knows";
foreach (ResultSet resultSet in resultSets)
{
Assert.NotNull(resultSet.Header);
Header header = resultSet.Header;
List<String> schemaNames = header.SchemaNames;
List<Header.ResultSetColumnTypes> schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Equal(3, schemaNames.Count);
Assert.Equal(3, schemaTypes.Count);
Assert.Equal("a", schemaNames[0]);
Assert.Equal("r", schemaNames[1]);
Assert.Equal("a.age", schemaNames[2]);
Assert.Single(resultSet);
Record record = resultSet.First();
Assert.Equal(new[] {"a", "r", "a.age"}, record.Keys);
Assert.Equal(new List<object> {expectedNode, expectedEdge, 32L}, record.Values);
}
//test for update in local cache
expectedNode.RemoveProperty("name");
expectedNode.RemoveProperty("age");
expectedNode.AddProperty(lastNameProperty);
expectedNode.RemoveLabel("person");
expectedNode.AddLabel("worker");
expectedNode.Id = 2;
expectedEdge.RelationshipType = "worksWith";
expectedEdge.Source = 2;
expectedEdge.Destination = 3;
expectedEdge.Id = 1;
Assert.NotNull(_api.Query("social", "CREATE (:worker{lastName:'a'})"));
Assert.NotNull(_api.Query("social", "CREATE (:worker{lastName:'b'})"));
Assert.NotNull(_api.Query("social", "MATCH (a:worker), (b:worker) WHERE (a.lastName = 'a' AND b.lastName='b') CREATE (a)-[:worksWith]->(b)"));
resultSets = Enumerable.Range(0, 16).AsParallel().Select(x => _api.Query("social", "MATCH (a:worker)-[r:worksWith]->(b:worker) RETURN a,r")).ToList();
foreach (ResultSet resultSet in resultSets)
{
Assert.NotNull(resultSet.Header);
Header header = resultSet.Header;
List<String> schemaNames = header.SchemaNames;
List<Header.ResultSetColumnTypes> schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Equal(2, schemaNames.Count);
Assert.Equal(2, schemaTypes.Count);
Assert.Equal("a", schemaNames[0]);
Assert.Equal("r", schemaNames[1]);
Assert.Single(resultSet);
Record record = resultSet.First();
Assert.Equal(new[] {"a", "r"}, record.Keys);
Assert.Equal(new List<object> {expectedNode, expectedEdge}, record.Values);
}
}
[Fact]
public void TestAdditionToProcedures()
{
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'roi',age:32})"));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'amit',age:30})"));
Assert.NotNull(_api.Query("social", "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)"));
List<ResultSet> resultSets = Enumerable.Range(0, 16).AsParallel().Select(x => _api.Query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r")).ToList();
//expected objects init
Property nameProperty = new Property("name", "roi");
Property ageProperty = new Property("age", 32L);
Property lastNameProperty = new Property("lastName", "a");
Node expectedNode = new Node();
expectedNode.Id = 0;
expectedNode.AddLabel("person");
expectedNode.AddProperty(nameProperty);
expectedNode.AddProperty(ageProperty);
Edge expectedEdge = new Edge();
expectedEdge.Id = 0;
expectedEdge.Source = 0;
expectedEdge.Destination = 1;
expectedEdge.RelationshipType = "knows";
ResultSet resultSet = _api.Query("social", "MATCH (a:person)-[r:knows]->(b:person) RETURN a,r");
Assert.NotNull(resultSet.Header);
Header header = resultSet.Header;
List<String> schemaNames = header.SchemaNames;
List<Header.ResultSetColumnTypes> schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Equal(2, schemaNames.Count);
Assert.Equal(2, schemaTypes.Count);
Assert.Equal("a", schemaNames[0]);
Assert.Equal("r", schemaNames[1]);
Assert.Single(resultSet);
Record record = resultSet.First();
Assert.Equal(new[] {"a", "r"}, record.Keys);
Assert.Equal(new List<object> {expectedNode, expectedEdge}, record.Values);
//test for local cache updates
expectedNode.RemoveProperty("name");
expectedNode.RemoveProperty("age");
expectedNode.AddProperty(lastNameProperty);
expectedNode.RemoveLabel("person");
expectedNode.AddLabel("worker");
expectedNode.Id = 2;
expectedEdge.RelationshipType = "worksWith";
expectedEdge.Source = 2;
expectedEdge.Destination = 3;
expectedEdge.Id = 1;
Assert.NotNull(_api.Query("social", "CREATE (:worker{lastName:'a'})"));
Assert.NotNull(_api.Query("social", "CREATE (:worker{lastName:'b'})"));
Assert.NotNull(_api.Query("social", "MATCH (a:worker), (b:worker) WHERE (a.lastName = 'a' AND b.lastName='b') CREATE (a)-[:worksWith]->(b)"));
resultSet = _api.Query("social", "MATCH (a:worker)-[r:worksWith]->(b:worker) RETURN a,r");
Assert.NotNull(resultSet.Header);
header = resultSet.Header;
schemaNames = header.SchemaNames;
schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Equal(2, schemaNames.Count);
Assert.Equal(2, schemaTypes.Count);
Assert.Equal("a", schemaNames[0]);
Assert.Equal("r", schemaNames[1]);
Assert.Single(resultSet);
record = resultSet.First();
Assert.Equal(new[] {"a", "r"}, record.Keys);
Assert.Equal(new List<object> {expectedNode, expectedEdge}, record.Values);
}
[Fact]
public void TestEscapedQuery()
{
Assert.NotNull(_api.Query("social", "MATCH (n) where n.s1='S\"\\'' RETURN n"));
Assert.NotNull(_api.Query("social", "MATCH (n) where n.s1='S\"\\'' RETURN n"));
}
[Fact]
public void TestEscapedQueryAgain()
{
var params1 = new Dictionary<string, object>();
params1.Put("s1", "S\"'");
params1.Put("s2", "S'\"");
Assert.NotNull(_api.GraphQuery("social", "CREATE (:escaped{s1:$s1,s2:$s2})", params1));
var params2 = new Dictionary<string, object>();
params2.Put("s1", "S\"'");
params2.Put("s2", "S'\"");
Assert.NotNull(_api.GraphQuery("social", "MATCH (n) where n.s1=$s1 and n.s2=$s2 RETURN n", params2));
}
[Theory]
[MemberData(nameof(EscapedCypherParameters))]
public void TestEscapedCypherParameters(Dictionary<string, object> parameters)
{
Assert.NotNull(_api.Query("whatever", "CREATE (a:Test {SomeString: $SomeString})", parameters));
}
public static readonly object[][] EscapedCypherParameters = new[]
{
new object[] {new Dictionary<string, object> {{"SomeString", "dsf\"dsfdss"}}},
new object[] {new Dictionary<string, object> {{"SomeString", "dsfdsfdss\"#"}}},
};
[Fact]
public void TestMultiExec()
{
var transaction = _api.Multi();
// transaction.SetAsync("x", "1");
transaction.QueryAsync("social", "CREATE (:Person {name:'a'})");
transaction.QueryAsync("g", "CREATE (:Person {name:'a'})");
// transaction.IncrAsync("x");
// transaction.GetAsync("x");
transaction.QueryAsync("social", "MATCH (n:Person) RETURN n");
transaction.DeleteGraphAsync("g");
transaction.CallProcedureAsync("social", "db.labels");
var results = transaction.Exec();
// Skipping Redis SET command assetions...
// Redis Graph command
var resultSet = results[0];
Assert.Equal(1, resultSet.Statistics.NodesCreated);
Assert.Equal(1, resultSet.Statistics.PropertiesSet);
resultSet = results[1];
Assert.Equal(1, resultSet.Statistics.NodesCreated);
Assert.Equal(1, resultSet.Statistics.PropertiesSet);
// Skipping Redis INCR command assertions...
// Skipping Redis GET command assertions...
// Graph Query Result
resultSet = results[2];
Assert.NotNull(resultSet.Header);
var header = resultSet.Header;
var schemaNames = header.SchemaNames;
var schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Single(schemaNames);
Assert.Single(schemaTypes);
Assert.Equal("n", schemaNames[0]);
var nameProperty = new Property("name", "a");
var expectedNode = new Node();
expectedNode.Id = 0;
expectedNode.AddLabel("Person");
expectedNode.AddProperty(nameProperty);
// See that the result were pulled from the right graph.
Assert.Single(resultSet);
var record = resultSet.First();
Assert.Equal(new List<string> {"n"}, record.Keys);
Assert.Equal(expectedNode, record.GetValue<Node>("n"));
resultSet = results[4];
Assert.NotNull(resultSet.Header);
schemaNames = header.SchemaNames;
schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Single(schemaNames);
Assert.Single(schemaTypes);
Assert.Equal("n", schemaNames[0]);
Assert.Single(resultSet);
record = resultSet.First();
Assert.Equal(new List<string> {"label"}, record.Keys);
Assert.Equal("Person", record.GetValue<string>("label"));
}
/*
Since by default all commands executed by StackExchange.Redis travel through the same connection
we're going to skip the following "contexted" tests:
- testContextedAPI
- testWriteTransactionWatch
- testReadTransactionWatch
*/
[Fact]
public void TestArraySupport()
{
var expectedANode = new Node();
expectedANode.Id = 0;
expectedANode.AddLabel("person");
var aNameProperty = new Property("name", "a");
var aAgeProperty = new Property("age", 32L);
var aListProperty = new Property("array", new object[] {0L, 1L, 2L});
expectedANode.AddProperty(aNameProperty);
expectedANode.AddProperty(aAgeProperty);
expectedANode.AddProperty(aListProperty);
var expectedBNode = new Node();
expectedBNode.Id = 1;
expectedBNode.AddLabel("person");
var bNameProperty = new Property("name", "b");
var bAgeProperty = new Property("age", 30L);
var bListProperty = new Property("array", new object[] {3L, 4L, 5L});
expectedBNode.AddProperty(bNameProperty);
expectedBNode.AddProperty(bAgeProperty);
expectedBNode.AddProperty(bListProperty);
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'a',age:32,array:[0,1,2]})"));
Assert.NotNull(_api.Query("social", "CREATE (:person{name:'b',age:30,array:[3,4,5]})"));
// test array
var resultSet = _api.Query("social", "WITH [0,1,2] as x return x");
// check header
Assert.NotNull(resultSet.Header);
var header = resultSet.Header;
var schemaNames = header.SchemaNames;
var schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Single(schemaNames);
Assert.Single(schemaTypes);
Assert.Equal("x", schemaNames[0]);
// check record
Assert.Single(resultSet);
var record = resultSet.First();
Assert.Equal(new[] {"x"}, record.Keys);
var x = record.GetValue<object[]>("x");
Assert.Equal(new object[] {0L, 1L, 2L}, x);
// test collect
resultSet = _api.Query("social", "MATCH(n) return collect(n) as x");
Assert.NotNull(resultSet.Header);
header = resultSet.Header;
schemaNames = header.SchemaNames;
schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Single(schemaNames);
Assert.Single(schemaTypes);
Assert.Equal("x", schemaNames[0]);
// check record
Assert.Single(resultSet);
record = resultSet.First();
Assert.Equal(new[] {"x"}, record.Keys);
x = record.GetValue<object[]>("x");
Assert.Contains(expectedANode, x);
Assert.Contains(expectedBNode, x);
// test unwind
resultSet = _api.Query("social", "unwind([0,1,2]) as x return x");
Assert.NotNull(resultSet.Header);
header = resultSet.Header;
schemaNames = header.SchemaNames;
schemaTypes = header.SchemaTypes;
Assert.NotNull(schemaNames);
Assert.NotNull(schemaTypes);
Assert.Single(schemaNames);
Assert.Single(schemaTypes);
Assert.Equal("x", schemaNames[0]);
// check record
Assert.Equal(3, resultSet.Count);
for (var i = 0; i < 3; i++)
{
record = resultSet.ElementAt(i);
Assert.Equal(new[] {"x"}, record.Keys);
Assert.Equal(i, record.GetValue<long>("x"));
}
}
[Fact]
public void TestPath()
{
List<Node> nodes = new List<Node>(3);
for (int i = 0; i < 3; i++)
{
var node = new Node();
node.Id = i;
node.AddLabel("L1");
nodes.Add(node);
}
List<Edge> edges = new List<Edge>(2);
for (int i = 0; i < 2; i++)
{
var edge = new Edge();
edge.Id = i;
edge.RelationshipType = "R1";
edge.Source = i;
edge.Destination = i + 1;
edges.Add(edge);
}
var expectedPaths = new HashSet<Path>();
var path01 = new PathBuilder(2).Append(nodes[0]).Append(edges[0]).Append(nodes[1]).Build();
var path12 = new PathBuilder(2).Append(nodes[1]).Append(edges[1]).Append(nodes[2]).Build();
var path02 = new PathBuilder(3).Append(nodes[0]).Append(edges[0]).Append(nodes[1]).Append(edges[1]).Append(nodes[2]).Build();
expectedPaths.Add(path01);
expectedPaths.Add(path12);
expectedPaths.Add(path02);
_api.Query("social", "CREATE (:L1)-[:R1]->(:L1)-[:R1]->(:L1)");
var resultSet = _api.Query("social", "MATCH p = (:L1)-[:R1*]->(:L1) RETURN p");
Assert.Equal(expectedPaths.Count, resultSet.Count);
for (int i = 0; i < resultSet.Count; i++)
{
Path p = resultSet.ElementAt(i).GetValue<Path>("p");
Assert.Contains(p, expectedPaths);
expectedPaths.Remove(p);
}
}
[Theory]
[MemberData(nameof(TestParameterValues))]
public void TestParameters(object parameters)
{
var param = new Dictionary<string, object>();
object expected = parameters;
param.Put("param", expected);
ResultSet resultSet = _api.Query("social", "RETURN $param", param);
Assert.Single(resultSet);
Record r = resultSet.First();
object o = r.GetValue<object>(0);
Assert.Equal(expected, o);
}
[Fact]
public void TestParametersReadOnly()
{
var parameters = new object[]
{
1, 2.3, true, false, null, "str", 'a', "b", new List<int> {1, 2, 3},
new[] {1, 2, 3}
};
var expected_anwsers = new object[]
{
1L, 2.3, true, false, null, "str", "a", "b", new List<long> {1L, 2L, 3L},
new[] {1L, 2L, 3L}
};
var paramDict = new Dictionary<string, object>();
for (int i = 0; i < parameters.Length; i++)
{
var param = parameters[i];
paramDict.Put("param", param);
ResultSet resultSetRo = _api.GraphReadOnlyQuery("social", "RETURN $param", paramDict);
Assert.Single(resultSetRo);
var oRo = resultSetRo.First().GetValue<object>(0);
var expected = expected_anwsers[i];
Assert.Equal(expected, oRo);
}
}
[Fact]
public void TestNullGraphEntities()
{
// Create two nodes connected by a single outgoing edge.
Assert.NotNull(_api.GraphQuery("social", "CREATE (:L)-[:E]->(:L2)"));
// Test a query that produces 1 record with 3 null values.
ResultSet resultSet = _api.GraphQuery("social", "OPTIONAL MATCH (a:NONEXISTENT)-[e]->(b) RETURN a, e, b");
Assert.Single(resultSet);
Assert.Equal(new object[] {null, null, null}, resultSet.First().Values);
// Test a query that produces 2 records, with 2 null values in the second.
resultSet = _api.GraphQuery("social", "MATCH (a) OPTIONAL MATCH (a)-[e]->(b) RETURN a, e, b ORDER BY ID(a)");
Assert.Equal(2, resultSet.Count);
var record = resultSet.First();
Assert.Equal(3, record.Values.Count);
Assert.NotNull(record.Values[0]);
Assert.NotNull(record.Values[1]);
Assert.NotNull(record.Values[2]);
record = resultSet.Skip(1).Take(1).First();
Assert.Equal(3, record.Size);
Assert.NotNull(record.Values[0]);
Assert.Null(record.Values[1]);
Assert.Null(record.Values[2]);
// Test a query that produces 2 records, the first containing a path and the
// second containing a null value.
resultSet = _api.GraphQuery("social", "MATCH (a) OPTIONAL MATCH p = (a)-[e]->(b) RETURN p");
Assert.Equal(2, resultSet.Count);
record = resultSet.First();
Assert.Equal(1, record.Size);
Assert.NotNull(record.Values[0]);
record = resultSet.Skip(1).First();
Assert.Equal(1, record.Size);
Assert.Null(record.Values[0]);
}
[Fact]
public void Test64BitNumber()
{
long value = 1L << 40;
var parameters = new Dictionary<string, object>();
parameters.Put("val", value);
ResultSet resultSet = _api.GraphQuery("social", "CREATE (n {val:$val}) RETURN n.val", parameters);
Assert.Single(resultSet);
Assert.Equal(value, resultSet.First().GetValue<long>(0));
}
[Fact]
public void TestCachedExecution()
{
_api.GraphQuery("social", "CREATE (:N {val:1}), (:N {val:2})");
// First time should not be loaded from execution cache
var parameters = new Dictionary<string, object>();
parameters.Put("val", 1L);
var resultSet = _api.GraphQuery("social", "MATCH (n:N {val:$val}) RETURN n.val", parameters);
Assert.Single(resultSet);
Assert.Equal(parameters["val"], resultSet.First().Values[0]);
Assert.False(resultSet.Statistics.CachedExecution);
// Run in loop many times to make sure the query will be loaded
// from cache at least once
for (int i = 0; i < 64; i++)
{
resultSet = _api.GraphQuery("social", "MATCH (n:N {val:$val}) RETURN n.val", parameters);
}
Assert.Single(resultSet);
Assert.Equal(parameters["val"], resultSet.First().Values[0]);
Assert.True(resultSet.Statistics.CachedExecution);
}
// TODO: https://github.com/tombatron/NRedisGraph/issues/20
[Fact]
public void TestMapDataType()
{
var expected = new Dictionary<string, object>();
expected.Put("a", (long)1);
expected.Put("b", "str");
expected.Put("c", null);
var d = new List<long>();
d.Add(1);
d.Add(2);
d.Add(3);
expected.Put("d", d);
expected.Put("e", true);
var f = new Dictionary<string, object>();
f.Put("x", (long)1);
f.Put("y", (long)2);
expected.Put("f", f);
ResultSet res = _api.GraphQuery("social", "RETURN {a:1, b:'str', c:NULL, d:[1,2,3], e:True, f:{x:1, y:2}}");
Assert.Single(res);
// Record r = res.iterator().next();
var something = res.First().Values[0];
var actual = res.First().GetValue<Dictionary<string, object>>(0);
Assert.Equal(expected, actual);
}
// TODO: https://github.com/tombatron/NRedisGraph/issues/22
// [Fact]
// public void TestGeoPointLatLon() {
// var rs = _api.GraphQuery("social", "CREATE (:restaurant"
// + " {location: point({latitude:30.27822306, longitude:-97.75134723})})");
// Assert.Equal(1, rs.Statistics.NodesCreated);
// Assert.Equal(1, rs.Statistics.PropertiesSet);
//
// AssertTestGeoPoint();
// }
//
// [Fact]
// public void TestGeoPointLonLat() {
// var rs = _api.GraphQuery("social", "CREATE (:restaurant"
// + " {location: point({longitude:-97.75134723, latitude:30.27822306})})");
// Assert.Equal(1, rs.Statistics.NodesCreated);
// Assert.Equal(1, rs.Statistics.PropertiesSet);
//
// AssertTestGeoPoint();
// }
//
// private void AssertTestGeoPoint()
// {
// var results = _api.GraphQuery("social", "MATCH (restaurant) RETURN restaurant");
//
// Assert.Single(results);
//
// var record = results.First();
// Assert.Equal(1, record.Size);
// Assert.Equal(new[]{"restaurant"}, record.Keys);
//
// var node = record.Values[0] as Node;
// var property = node?.PropertyMap["location"] ?? null;
//
// Assert.Equal(new Point(30.27822306, -97.75134723), property.Value);
// }
// TODO: https://github.com/tombatron/NRedisGraph/issues/23
// [Fact]
// public void TimeoutArgument() {
// var rs = _api.GraphQuery("social", "UNWIND range(0,100) AS x WITH x AS x WHERE x = 100 RETURN x", 1L);
//
// Assert.Single(rs);
//
// var r = rs.First();
//
// Assert.Equal(100L, r.GetValue<long>(0));
// }
[Fact]
public void TestCachedExecutionReadOnly()
{
_api.GraphQuery("social", "CREATE (:N {val:1}), (:N {val:2})");
// First time should not be loaded from execution cache
var parameters = new Dictionary<string, object>();
parameters.Put("val", 1L);
var resultSet = _api.GraphReadOnlyQuery("social", "MATCH (n:N {val:$val}) RETURN n.val", parameters);
Assert.Single(resultSet);
Assert.Equal(parameters["val"], resultSet.First().Values[0]);
Assert.False(resultSet.Statistics.CachedExecution);
// Run in loop many times to make sure the query will be loaded
// from cache at least once
for (int i = 0; i < 64; i++)
{
resultSet = _api.GraphReadOnlyQuery("social", "MATCH (n:N {val:$val}) RETURN n.val", parameters);
}
Assert.Single(resultSet);
Assert.Equal(parameters["val"], resultSet.First().Values[0]);
Assert.True(resultSet.Statistics.CachedExecution);
}
[Fact]
public void TestSimpleReadOnly()
{
_api.GraphQuery("social", "CREATE (:person{name:'filipe',age:30})");
var rsRo = _api.GraphReadOnlyQuery("social", "MATCH (a:person) WHERE (a.name = 'filipe') RETURN a.age");
Assert.Single(rsRo);
Assert.Equal(30L, rsRo.First().GetValue<long>(0));