-
Notifications
You must be signed in to change notification settings - Fork 232
/
test_match_api.py
1109 lines (898 loc) · 36.4 KB
/
test_match_api.py
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
import re
from datetime import datetime
from test._async_compat import mark_async_test
from pytest import raises
from neomodel import (
INCOMING,
ArrayProperty,
AsyncRelationshipFrom,
AsyncRelationshipTo,
AsyncStructuredNode,
AsyncStructuredRel,
AsyncZeroOrOne,
DateTimeProperty,
IntegerProperty,
Q,
StringProperty,
UniqueIdProperty,
adb,
)
from neomodel._async_compat.util import AsyncUtil
from neomodel.async_.match import (
AsyncNodeSet,
AsyncQueryBuilder,
AsyncTraversal,
Collect,
Last,
NodeNameResolver,
Optional,
RawCypher,
RelationNameResolver,
)
from neomodel.exceptions import MultipleNodesReturned, RelationshipClassNotDefined
class SupplierRel(AsyncStructuredRel):
since = DateTimeProperty(default=datetime.now)
courier = StringProperty()
class Supplier(AsyncStructuredNode):
name = StringProperty()
delivery_cost = IntegerProperty()
coffees = AsyncRelationshipTo("Coffee", "COFFEE SUPPLIERS", model=SupplierRel)
class Species(AsyncStructuredNode):
name = StringProperty()
tags = ArrayProperty(StringProperty(), default=list)
coffees = AsyncRelationshipFrom(
"Coffee", "COFFEE SPECIES", model=AsyncStructuredRel
)
class Coffee(AsyncStructuredNode):
name = StringProperty(unique_index=True)
price = IntegerProperty()
suppliers = AsyncRelationshipFrom(Supplier, "COFFEE SUPPLIERS", model=SupplierRel)
species = AsyncRelationshipTo(Species, "COFFEE SPECIES", model=AsyncStructuredRel)
id_ = IntegerProperty()
class Extension(AsyncStructuredNode):
extension = AsyncRelationshipTo("Extension", "extension")
class CountryX(AsyncStructuredNode):
code = StringProperty(unique_index=True, required=True)
inhabitant = AsyncRelationshipFrom("PersonX", "IS_FROM")
class CityX(AsyncStructuredNode):
name = StringProperty(required=True)
country = AsyncRelationshipTo(CountryX, "FROM_COUNTRY")
class PersonX(AsyncStructuredNode):
uid = UniqueIdProperty()
name = StringProperty(unique_index=True)
age = IntegerProperty(index=True, default=0)
# traverse outgoing IS_FROM relations, inflate to Country objects
country = AsyncRelationshipTo(CountryX, "IS_FROM")
# traverse outgoing LIVES_IN relations, inflate to City objects
city = AsyncRelationshipTo(CityX, "LIVES_IN")
class SoftwareDependency(AsyncStructuredNode):
name = StringProperty(required=True)
version = StringProperty(required=True)
class HasCourseRel(AsyncStructuredRel):
level = StringProperty()
start_date = DateTimeProperty()
end_date = DateTimeProperty()
class Course(AsyncStructuredNode):
name = StringProperty()
class Building(AsyncStructuredNode):
name = StringProperty()
class Student(AsyncStructuredNode):
name = StringProperty()
parents = AsyncRelationshipTo("Student", "HAS_PARENT", model=AsyncStructuredRel)
children = AsyncRelationshipFrom("Student", "HAS_PARENT", model=AsyncStructuredRel)
lives_in = AsyncRelationshipTo(Building, "LIVES_IN", model=AsyncStructuredRel)
courses = AsyncRelationshipTo(Course, "HAS_COURSE", model=HasCourseRel)
preferred_course = AsyncRelationshipTo(
Course,
"HAS_PREFERRED_COURSE",
model=AsyncStructuredRel,
cardinality=AsyncZeroOrOne,
)
@mark_async_test
async def test_filter_exclude_via_labels():
await Coffee(name="Java", price=99).save()
node_set = AsyncNodeSet(Coffee)
qb = await AsyncQueryBuilder(node_set).build_ast()
results = [node async for node in qb._execute()]
assert "(coffee:Coffee)" in qb._ast.match
assert qb._ast.result_class
assert len(results) == 1
assert isinstance(results[0], Coffee)
assert results[0].name == "Java"
# with filter and exclude
await Coffee(name="Kenco", price=3).save()
node_set = node_set.filter(price__gt=2).exclude(price__gt=6, name="Java")
qb = await AsyncQueryBuilder(node_set).build_ast()
results = [node async for node in qb._execute()]
assert "(coffee:Coffee)" in qb._ast.match
assert "NOT" in qb._ast.where[0]
assert len(results) == 1
assert results[0].name == "Kenco"
@mark_async_test
async def test_simple_has_via_label():
nescafe = await Coffee(name="Nescafe", price=99).save()
tesco = await Supplier(name="Tesco", delivery_cost=2).save()
await nescafe.suppliers.connect(tesco)
ns = AsyncNodeSet(Coffee).has(suppliers=True)
qb = await AsyncQueryBuilder(ns).build_ast()
results = [node async for node in qb._execute()]
assert "COFFEE SUPPLIERS" in qb._ast.where[0]
assert len(results) == 1
assert results[0].name == "Nescafe"
await Coffee(name="nespresso", price=99).save()
ns = AsyncNodeSet(Coffee).has(suppliers=False)
qb = await AsyncQueryBuilder(ns).build_ast()
results = [node async for node in qb._execute()]
assert len(results) > 0
assert "NOT" in qb._ast.where[0]
@mark_async_test
async def test_get():
await Coffee(name="1", price=3).save()
assert await Coffee.nodes.get(name="1")
with raises(Coffee.DoesNotExist):
await Coffee.nodes.get(name="2")
await Coffee(name="2", price=3).save()
with raises(MultipleNodesReturned):
await Coffee.nodes.get(price=3)
@mark_async_test
async def test_simple_traverse_with_filter():
nescafe = await Coffee(name="Nescafe2", price=99).save()
tesco = await Supplier(name="Tesco", delivery_cost=2).save()
await nescafe.suppliers.connect(tesco)
qb = AsyncQueryBuilder(
AsyncNodeSet(source=nescafe).suppliers.match(since__lt=datetime.now())
)
_ast = await qb.build_ast()
results = [node async for node in qb._execute()]
assert qb._ast.lookup
assert qb._ast.match
assert qb._ast.return_clause.startswith("suppliers")
assert len(results) == 1
assert results[0].name == "Tesco"
@mark_async_test
async def test_double_traverse():
nescafe = await Coffee(name="Nescafe plus", price=99).save()
tesco = await Supplier(name="Asda", delivery_cost=2).save()
await nescafe.suppliers.connect(tesco)
await tesco.coffees.connect(await Coffee(name="Decafe", price=2).save())
ns = AsyncNodeSet(AsyncNodeSet(source=nescafe).suppliers.match()).coffees.match()
qb = await AsyncQueryBuilder(ns).build_ast()
results = [node async for node in qb._execute()]
assert len(results) == 2
names = [n.name for n in results]
assert "Decafe" in names
assert "Nescafe plus" in names
@mark_async_test
async def test_count():
await Coffee(name="Nescafe Gold", price=99).save()
ast = await AsyncQueryBuilder(AsyncNodeSet(source=Coffee)).build_ast()
count = await ast._count()
assert count > 0
await Coffee(name="Kawa", price=27).save()
node_set = AsyncNodeSet(source=Coffee)
node_set.skip = 1
node_set.limit = 1
ast = await AsyncQueryBuilder(node_set).build_ast()
count = await ast._count()
assert count == 1
@mark_async_test
async def test_len_and_iter_and_bool():
iterations = 0
await Coffee(name="Icelands finest").save()
for c in await Coffee.nodes:
iterations += 1
await c.delete()
assert iterations > 0
assert len(await Coffee.nodes) == 0
@mark_async_test
async def test_slice():
await Coffee(name="Icelands finest").save()
await Coffee(name="Britains finest").save()
await Coffee(name="Japans finest").save()
# Branching tests because async needs extra brackets
if AsyncUtil.is_async_code:
assert len(list((await Coffee.nodes)[1:])) == 2
assert len(list((await Coffee.nodes)[:1])) == 1
assert isinstance((await Coffee.nodes)[1], Coffee)
assert isinstance((await Coffee.nodes)[0], Coffee)
assert len(list((await Coffee.nodes)[1:2])) == 1
else:
assert len(list(Coffee.nodes[1:])) == 2
assert len(list(Coffee.nodes[:1])) == 1
assert isinstance(Coffee.nodes[1], Coffee)
assert isinstance(Coffee.nodes[0], Coffee)
assert len(list(Coffee.nodes[1:2])) == 1
@mark_async_test
async def test_issue_208():
# calls to match persist across queries.
b = await Coffee(name="basics").save()
l = await Supplier(name="lidl").save()
a = await Supplier(name="aldi").save()
await b.suppliers.connect(l, {"courier": "fedex"})
await b.suppliers.connect(a, {"courier": "dhl"})
assert len(await b.suppliers.match(courier="fedex"))
assert len(await b.suppliers.match(courier="dhl"))
@mark_async_test
async def test_issue_589():
node1 = await Extension().save()
node2 = await Extension().save()
assert node2 not in await node1.extension
await node1.extension.connect(node2)
assert node2 in await node1.extension
@mark_async_test
async def test_contains():
expensive = await Coffee(price=1000, name="Pricey").save()
asda = await Coffee(name="Asda", price=1).save()
assert expensive in await Coffee.nodes.filter(price__gt=999)
assert asda not in await Coffee.nodes.filter(price__gt=999)
# bad value raises
with raises(ValueError, match=r"Expecting StructuredNode instance"):
if AsyncUtil.is_async_code:
assert await Coffee.nodes.check_contains(2)
else:
assert 2 in Coffee.nodes
# unsaved
with raises(ValueError, match=r"Unsaved node"):
if AsyncUtil.is_async_code:
assert await Coffee.nodes.check_contains(Coffee())
else:
assert Coffee() in Coffee.nodes
@mark_async_test
async def test_order_by():
c1 = await Coffee(name="Icelands finest", price=5).save()
c2 = await Coffee(name="Britains finest", price=10).save()
c3 = await Coffee(name="Japans finest", price=35).save()
if AsyncUtil.is_async_code:
assert ((await Coffee.nodes.order_by("price"))[0]).price == 5
assert ((await Coffee.nodes.order_by("-price"))[0]).price == 35
else:
assert (Coffee.nodes.order_by("price")[0]).price == 5
assert (Coffee.nodes.order_by("-price")[0]).price == 35
ns = Coffee.nodes.order_by("-price")
qb = await AsyncQueryBuilder(ns).build_ast()
assert qb._ast.order_by
ns = ns.order_by(None)
qb = await AsyncQueryBuilder(ns).build_ast()
assert not qb._ast.order_by
ns = ns.order_by("?")
qb = await AsyncQueryBuilder(ns).build_ast()
assert qb._ast.with_clause == "coffee, rand() as r"
assert qb._ast.order_by == ["r"]
with raises(
ValueError,
match=r".*Neo4j internals like id or element_id are not allowed for use in this operation.",
):
await Coffee.nodes.order_by("id").all()
# Test order by on a relationship
l = await Supplier(name="lidl2").save()
await l.coffees.connect(c1)
await l.coffees.connect(c2)
await l.coffees.connect(c3)
ordered_n = [n for n in await l.coffees.order_by("name")]
assert ordered_n[0] == c2
assert ordered_n[1] == c1
assert ordered_n[2] == c3
@mark_async_test
async def test_order_by_rawcypher():
d1 = await SoftwareDependency(name="Package1", version="1.0.0").save()
d2 = await SoftwareDependency(name="Package2", version="1.4.0").save()
d3 = await SoftwareDependency(name="Package3", version="2.5.5").save()
assert (
await SoftwareDependency.nodes.order_by(
RawCypher("toInteger(split($n.version, '.')[0]) DESC"),
).all()
)[0] == d3
with raises(
ValueError, match=r"RawCypher: Do not include any action that has side effect"
):
SoftwareDependency.nodes.order_by(
RawCypher("DETACH DELETE $n"),
)
@mark_async_test
async def test_extra_filters():
c1 = await Coffee(name="Icelands finest", price=5, id_=1).save()
c2 = await Coffee(name="Britains finest", price=10, id_=2).save()
c3 = await Coffee(name="Japans finest", price=35, id_=3).save()
c4 = await Coffee(name="US extra-fine", price=None, id_=4).save()
coffees_5_10 = await Coffee.nodes.filter(price__in=[10, 5])
assert len(coffees_5_10) == 2, "unexpected number of results"
assert c1 in coffees_5_10, "doesnt contain 5 price coffee"
assert c2 in coffees_5_10, "doesnt contain 10 price coffee"
finest_coffees = await Coffee.nodes.filter(name__iendswith=" Finest")
assert len(finest_coffees) == 3, "unexpected number of results"
assert c1 in finest_coffees, "doesnt contain 1st finest coffee"
assert c2 in finest_coffees, "doesnt contain 2nd finest coffee"
assert c3 in finest_coffees, "doesnt contain 3rd finest coffee"
unpriced_coffees = await Coffee.nodes.filter(price__isnull=True)
assert len(unpriced_coffees) == 1, "unexpected number of results"
assert c4 in unpriced_coffees, "doesnt contain unpriced coffee"
coffees_with_id_gte_3 = await Coffee.nodes.filter(id___gte=3)
assert len(coffees_with_id_gte_3) == 2, "unexpected number of results"
assert c3 in coffees_with_id_gte_3
assert c4 in coffees_with_id_gte_3
with raises(
ValueError,
match=r".*Neo4j internals like id or element_id are not allowed for use in this operation.",
):
await Coffee.nodes.filter(elementId="4:xxx:111").all()
def test_traversal_definition_keys_are_valid():
muckefuck = Coffee(name="Mukkefuck", price=1)
with raises(ValueError):
AsyncTraversal(
muckefuck,
"a_name",
{
"node_class": Supplier,
"direction": INCOMING,
"relationship_type": "KNOWS",
"model": None,
},
)
AsyncTraversal(
muckefuck,
"a_name",
{
"node_class": Supplier,
"direction": INCOMING,
"relation_type": "KNOWS",
"model": None,
},
)
@mark_async_test
async def test_empty_filters():
"""Test this case:
```
SomeModel.nodes.filter().filter(Q(arg1=val1)).all()
SomeModel.nodes.exclude().exclude(Q(arg1=val1)).all()
SomeModel.nodes.filter().filter(arg1=val1).all()
```
In django_rest_framework filter uses such as lazy function and
``get_queryset`` function in ``GenericAPIView`` should returns
``NodeSet`` object.
"""
c1 = await Coffee(name="Super", price=5, id_=1).save()
c2 = await Coffee(name="Puper", price=10, id_=2).save()
empty_filter = Coffee.nodes.filter()
all_coffees = await empty_filter.all()
assert len(all_coffees) == 2, "unexpected number of results"
filter_empty_filter = empty_filter.filter(price=5)
assert len(await filter_empty_filter.all()) == 1, "unexpected number of results"
assert (
c1 in await filter_empty_filter.all()
), "doesnt contain c1 in ``filter_empty_filter``"
filter_q_empty_filter = empty_filter.filter(Q(price=5))
assert len(await filter_empty_filter.all()) == 1, "unexpected number of results"
assert (
c1 in await filter_empty_filter.all()
), "doesnt contain c1 in ``filter_empty_filter``"
@mark_async_test
async def test_q_filters():
c1 = await Coffee(name="Icelands finest", price=5, id_=1).save()
c2 = await Coffee(name="Britains finest", price=10, id_=2).save()
c3 = await Coffee(name="Japans finest", price=35, id_=3).save()
c4 = await Coffee(name="US extra-fine", price=None, id_=4).save()
c5 = await Coffee(name="Latte", price=35, id_=5).save()
c6 = await Coffee(name="Cappuccino", price=35, id_=6).save()
coffees_5_10 = await Coffee.nodes.filter(Q(price=10) | Q(price=5)).all()
assert len(coffees_5_10) == 2, "unexpected number of results"
assert c1 in coffees_5_10, "doesnt contain 5 price coffee"
assert c2 in coffees_5_10, "doesnt contain 10 price coffee"
coffees_5_6 = (
await Coffee.nodes.filter(Q(name="Latte") | Q(name="Cappuccino"))
.filter(price=35)
.all()
)
assert len(coffees_5_6) == 2, "unexpected number of results"
assert c5 in coffees_5_6, "doesnt contain 5 coffee"
assert c6 in coffees_5_6, "doesnt contain 6 coffee"
coffees_5_6 = (
await Coffee.nodes.filter(price=35)
.filter(Q(name="Latte") | Q(name="Cappuccino"))
.all()
)
assert len(coffees_5_6) == 2, "unexpected number of results"
assert c5 in coffees_5_6, "doesnt contain 5 coffee"
assert c6 in coffees_5_6, "doesnt contain 6 coffee"
finest_coffees = await Coffee.nodes.filter(name__iendswith=" Finest").all()
assert len(finest_coffees) == 3, "unexpected number of results"
assert c1 in finest_coffees, "doesnt contain 1st finest coffee"
assert c2 in finest_coffees, "doesnt contain 2nd finest coffee"
assert c3 in finest_coffees, "doesnt contain 3rd finest coffee"
unpriced_coffees = await Coffee.nodes.filter(Q(price__isnull=True)).all()
assert len(unpriced_coffees) == 1, "unexpected number of results"
assert c4 in unpriced_coffees, "doesnt contain unpriced coffee"
coffees_with_id_gte_3 = await Coffee.nodes.filter(Q(id___gte=3)).all()
assert len(coffees_with_id_gte_3) == 4, "unexpected number of results"
assert c3 in coffees_with_id_gte_3
assert c4 in coffees_with_id_gte_3
assert c5 in coffees_with_id_gte_3
assert c6 in coffees_with_id_gte_3
coffees_5_not_japans = await Coffee.nodes.filter(
Q(price__gt=5) & ~Q(name="Japans finest")
).all()
assert c3 not in coffees_5_not_japans
empty_Q_condition = await Coffee.nodes.filter(Q(price=5) | Q()).all()
assert (
len(empty_Q_condition) == 1
), "undefined Q leading to unexpected number of results"
assert c1 in empty_Q_condition
combined_coffees = await Coffee.nodes.filter(
Q(price=35), Q(name="Latte") | Q(name="Cappuccino")
).all()
assert len(combined_coffees) == 2
assert c5 in combined_coffees
assert c6 in combined_coffees
assert c3 not in combined_coffees
class QQ:
pass
with raises(TypeError):
wrong_Q = await Coffee.nodes.filter(Q(price=5) | QQ()).all()
def test_qbase():
test_print_out = str(Q(price=5) | Q(price=10))
test_repr = repr(Q(price=5) | Q(price=10))
assert test_print_out == "(OR: ('price', 5), ('price', 10))"
assert test_repr == "<Q: (OR: ('price', 5), ('price', 10))>"
assert ("price", 5) in (Q(price=5) | Q(price=10))
test_hash = set([Q(price_lt=30) | ~Q(price=5), Q(price_lt=30) | ~Q(price=5)])
assert len(test_hash) == 1
@mark_async_test
async def test_traversal_filter_left_hand_statement():
nescafe = await Coffee(name="Nescafe2", price=99).save()
nescafe_gold = await Coffee(name="Nescafe gold", price=11).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
biedronka = await Supplier(name="Biedronka", delivery_cost=5).save()
lidl = await Supplier(name="Lidl", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(biedronka)
await nescafe_gold.suppliers.connect(lidl)
lidl_supplier = (
await AsyncNodeSet(Coffee.nodes.filter(price=11).suppliers)
.filter(delivery_cost=3)
.all()
)
assert lidl in lidl_supplier
@mark_async_test
async def test_filter_with_traversal():
arabica = await Species(name="Arabica").save()
robusta = await Species(name="Robusta").save()
nescafe = await Coffee(name="Nescafe", price=11).save()
nescafe_gold = await Coffee(name="Nescafe Gold", price=99).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(tesco)
await nescafe.species.connect(arabica)
await nescafe_gold.species.connect(robusta)
results = await Coffee.nodes.filter(species__name="Arabica").all()
assert len(results) == 1
assert len(results[0]) == 3
assert results[0][0] == nescafe
results_multi_hop = await Supplier.nodes.filter(
coffees__species__name="Arabica"
).all()
assert len(results_multi_hop) == 1
assert results_multi_hop[0][0] == tesco
no_results = await Supplier.nodes.filter(coffees__species__name="Noffee").all()
assert no_results == []
@mark_async_test
async def test_relation_prop_filtering():
arabica = await Species(name="Arabica").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
supplier1 = await Supplier(name="Supplier 1", delivery_cost=3).save()
supplier2 = await Supplier(name="Supplier 2", delivery_cost=20).save()
await nescafe.suppliers.connect(supplier1, {"since": datetime(2020, 4, 1, 0, 0)})
await nescafe.suppliers.connect(supplier2, {"since": datetime(2010, 4, 1, 0, 0)})
await nescafe.species.connect(arabica)
results = await Supplier.nodes.filter(
**{"coffees__name": "Nescafe", "coffees|since__gt": datetime(2018, 4, 1, 0, 0)}
).all()
assert len(results) == 1
assert results[0][0] == supplier1
# Test it works with mixed argument syntaxes
results2 = await Supplier.nodes.filter(
name="Supplier 1",
coffees__name="Nescafe",
**{"coffees|since__gt": datetime(2018, 4, 1, 0, 0)},
).all()
assert len(results2) == 1
assert results2[0][0] == supplier1
@mark_async_test
async def test_relation_prop_ordering():
arabica = await Species(name="Arabica").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
supplier1 = await Supplier(name="Supplier 1", delivery_cost=3).save()
supplier2 = await Supplier(name="Supplier 2", delivery_cost=20).save()
await nescafe.suppliers.connect(supplier1, {"since": datetime(2020, 4, 1, 0, 0)})
await nescafe.suppliers.connect(supplier2, {"since": datetime(2010, 4, 1, 0, 0)})
await nescafe.species.connect(arabica)
results = (
await Supplier.nodes.fetch_relations("coffees").order_by("-coffees|since").all()
)
assert len(results) == 2
assert results[0][0] == supplier1
assert results[1][0] == supplier2
results = (
await Supplier.nodes.fetch_relations("coffees").order_by("coffees|since").all()
)
assert len(results) == 2
assert results[0][0] == supplier2
assert results[1][0] == supplier1
@mark_async_test
async def test_fetch_relations():
arabica = await Species(name="Arabica").save()
robusta = await Species(name="Robusta").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
nescafe_gold = await Coffee(name="Nescafe Gold", price=11).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(tesco)
await nescafe.species.connect(arabica)
result = (
await Supplier.nodes.filter(name="Tesco")
.fetch_relations("coffees__species")
.all()
)
assert len(result[0]) == 5
assert arabica in result[0]
assert robusta not in result[0]
assert tesco in result[0]
assert nescafe in result[0]
assert nescafe_gold not in result[0]
result = (
await Species.nodes.filter(name="Robusta")
.fetch_relations(Optional("coffees__suppliers"))
.all()
)
assert len(result) == 0
if AsyncUtil.is_async_code:
count = (
await Supplier.nodes.filter(name="Tesco")
.fetch_relations("coffees__species")
.get_len()
)
assert count == 1
assert (
await Supplier.nodes.fetch_relations("coffees__species")
.filter(name="Tesco")
.check_contains(tesco)
)
else:
count = len(
Supplier.nodes.filter(name="Tesco")
.fetch_relations("coffees__species")
.all()
)
assert count == 1
assert tesco in Supplier.nodes.fetch_relations("coffees__species").filter(
name="Tesco"
)
@mark_async_test
async def test_traverse_and_order_by():
arabica = await Species(name="Arabica").save()
robusta = await Species(name="Robusta").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
nescafe_gold = await Coffee(name="Nescafe Gold", price=110).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(tesco)
await nescafe.species.connect(arabica)
await nescafe_gold.species.connect(robusta)
results = (
await Species.nodes.fetch_relations("coffees").order_by("-coffees__price").all()
)
assert len(results) == 2
assert len(results[0]) == 3 # 2 nodes and 1 relation
assert results[0][0] == robusta
assert results[1][0] == arabica
@mark_async_test
async def test_annotate_and_collect():
arabica = await Species(name="Arabica").save()
robusta = await Species(name="Robusta").save()
nescafe = await Coffee(name="Nescafe 1002", price=99).save()
nescafe_gold = await Coffee(name="Nescafe 1003", price=11).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(tesco)
await nescafe.species.connect(arabica)
await nescafe_gold.species.connect(robusta)
await nescafe_gold.species.connect(arabica)
result = (
await Supplier.nodes.traverse_relations(species="coffees__species")
.annotate(Collect("species"))
.all()
)
assert len(result) == 1
assert len(result[0][1][0]) == 3 # 3 species must be there (with 2 duplicates)
result = (
await Supplier.nodes.traverse_relations(species="coffees__species")
.annotate(Collect("species", distinct=True))
.all()
)
assert len(result[0][1][0]) == 2 # 2 species must be there
result = (
await Supplier.nodes.traverse_relations(species="coffees__species")
.annotate(all_species=Collect("species", distinct=True))
.all()
)
assert len(result[0][1][0]) == 2 # 2 species must be there
result = (
await Supplier.nodes.traverse_relations("coffees__species")
.annotate(
all_species=Collect(NodeNameResolver("coffees__species"), distinct=True),
all_species_rels=Collect(
RelationNameResolver("coffees__species"), distinct=True
),
)
.all()
)
assert len(result[0][1][0]) == 2 # 2 species must be there
assert len(result[0][2][0]) == 3 # 3 species relations must be there
@mark_async_test
async def test_resolve_subgraph():
arabica = await Species(name="Arabica").save()
robusta = await Species(name="Robusta").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
nescafe_gold = await Coffee(name="Nescafe Gold", price=11).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(tesco)
await nescafe.species.connect(arabica)
await nescafe_gold.species.connect(robusta)
with raises(
RuntimeError,
match=re.escape(
"Nothing to resolve. Make sure to include relations in the result using fetch_relations() or filter()."
),
):
result = await Supplier.nodes.resolve_subgraph()
with raises(
NotImplementedError,
match=re.escape(
"You cannot use traverse_relations() with resolve_subgraph(), use fetch_relations() instead."
),
):
result = await Supplier.nodes.traverse_relations(
"coffees__species"
).resolve_subgraph()
result = await Supplier.nodes.fetch_relations("coffees__species").resolve_subgraph()
assert len(result) == 2
assert hasattr(result[0], "_relations")
assert "coffees" in result[0]._relations
coffees = result[0]._relations["coffees"]
assert hasattr(coffees, "_relations")
assert "species" in coffees._relations
assert hasattr(result[1], "_relations")
assert "coffees" in result[1]._relations
coffees = result[1]._relations["coffees"]
assert hasattr(coffees, "_relations")
assert "species" in coffees._relations
@mark_async_test
async def test_resolve_subgraph_optional():
arabica = await Species(name="Arabica").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
nescafe_gold = await Coffee(name="Nescafe Gold", price=11).save()
tesco = await Supplier(name="Tesco", delivery_cost=3).save()
await nescafe.suppliers.connect(tesco)
await nescafe_gold.suppliers.connect(tesco)
await nescafe.species.connect(arabica)
result = await Supplier.nodes.fetch_relations(
Optional("coffees__species")
).resolve_subgraph()
assert len(result) == 1
assert hasattr(result[0], "_relations")
assert "coffees" in result[0]._relations
coffees = result[0]._relations["coffees"]
assert hasattr(coffees, "_relations")
assert "species" in coffees._relations
assert coffees._relations["species"] == arabica
@mark_async_test
async def test_subquery():
arabica = await Species(name="Arabica").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
supplier1 = await Supplier(name="Supplier 1", delivery_cost=3).save()
supplier2 = await Supplier(name="Supplier 2", delivery_cost=20).save()
await nescafe.suppliers.connect(supplier1)
await nescafe.suppliers.connect(supplier2)
await nescafe.species.connect(arabica)
result = await Coffee.nodes.subquery(
Coffee.nodes.traverse_relations(suppliers="suppliers")
.intermediate_transform(
{"suppliers": "suppliers"}, ordering=["suppliers.delivery_cost"]
)
.annotate(supps=Last(Collect("suppliers"))),
["supps"],
)
result = await result.all()
assert len(result) == 1
assert len(result[0]) == 2
assert result[0][0] == supplier2
with raises(
RuntimeError,
match=re.escape("Variable 'unknown' is not returned by subquery."),
):
result = await Coffee.nodes.subquery(
Coffee.nodes.traverse_relations(suppliers="suppliers").annotate(
supps=Collect("suppliers")
),
["unknown"],
)
@mark_async_test
async def test_intermediate_transform():
arabica = await Species(name="Arabica").save()
nescafe = await Coffee(name="Nescafe", price=99).save()
supplier1 = await Supplier(name="Supplier 1", delivery_cost=3).save()
supplier2 = await Supplier(name="Supplier 2", delivery_cost=20).save()
await nescafe.suppliers.connect(supplier1, {"since": datetime(2020, 4, 1, 0, 0)})
await nescafe.suppliers.connect(supplier2, {"since": datetime(2010, 4, 1, 0, 0)})
await nescafe.species.connect(arabica)
result = (
await Coffee.nodes.fetch_relations("suppliers")
.intermediate_transform(
{
"coffee": "coffee",
"suppliers": NodeNameResolver("suppliers"),
"r": RelationNameResolver("suppliers"),
},
ordering=["-r.since"],
)
.annotate(oldest_supplier=Last(Collect("suppliers")))
.all()
)
assert len(result) == 1
assert result[0] == supplier2
with raises(
ValueError,
match=re.escape(
r"Wrong source type specified for variable 'test', should be a string or an instance of NodeNameResolver or RelationNameResolver"
),
):
Coffee.nodes.traverse_relations(suppliers="suppliers").intermediate_transform(
{
"test": Collect("suppliers"),
}
)
with raises(
ValueError,
match=re.escape(
r"You must provide one variable at least when calling intermediate_transform()"
),
):
Coffee.nodes.traverse_relations(suppliers="suppliers").intermediate_transform(
{}
)
@mark_async_test
async def test_mix_functions():
# Test with a mix of all advanced querying functions
eiffel_tower = await Building(name="Eiffel Tower").save()
empire_state_building = await Building(name="Empire State Building").save()
miranda = await Student(name="Miranda").save()
await miranda.lives_in.connect(empire_state_building)
jean_pierre = await Student(name="Jean-Pierre").save()
await jean_pierre.lives_in.connect(eiffel_tower)
mireille = await Student(name="Mireille").save()
mimoun_jr = await Student(name="Mimoun Jr").save()
mimoun = await Student(name="Mimoun").save()
await mireille.lives_in.connect(eiffel_tower)
await mimoun_jr.lives_in.connect(eiffel_tower)
await mimoun.lives_in.connect(eiffel_tower)
await mimoun.parents.connect(mireille)
await mimoun.children.connect(mimoun_jr)
math = await Course(name="Math").save()
dessin = await Course(name="Dessin").save()
await mimoun.courses.connect(
math,
{
"level": "1.2",
"start_date": datetime(2020, 6, 2),
"end_date": datetime(2020, 12, 31),
},
)
await mimoun.courses.connect(
math,
{
"level": "1.1",
"start_date": datetime(2020, 1, 1),
"end_date": datetime(2020, 6, 1),
},
)
await mimoun_jr.courses.connect(
math,
{
"level": "1.1",
"start_date": datetime(2020, 1, 1),
"end_date": datetime(2020, 6, 1),
},
)
await mimoun_jr.preferred_course.connect(dessin)