19
19
from io import StringIO
20
20
from unittest import TestCase
21
21
22
+ from _pytest .python_api import raises
23
+
22
24
from py2neo .cypher import Record
23
25
from py2neo .data import Subgraph , Walkable , Node , Relationship , Path , walk
24
26
from py2neo .integration import Table
@@ -405,8 +407,9 @@ def test_property_keys(self):
405
407
assert self .subgraph .keys () == {"name" , "age" , "since" }
406
408
407
409
def test_empty_subgraph (self ):
408
- with self .assertRaises (ValueError ):
409
- Subgraph ()
410
+ s = Subgraph ()
411
+ assert len (s .nodes ) == 0
412
+ assert len (s .relationships ) == 0
410
413
411
414
412
415
class WalkableTestCase (TestCase ):
@@ -869,40 +872,119 @@ def test_can_concatenate_node_and_none(self):
869
872
870
873
class UnionTestCase (TestCase ):
871
874
872
- def test_graph_union (self ):
875
+ def test_node_union (self ):
876
+ s = alice | bob
877
+ assert len (s .nodes ) == 2
878
+ assert len (s .relationships ) == 0
879
+
880
+ def test_node_union_in_place (self ):
881
+ n = Node ()
882
+ with raises (TypeError ):
883
+ n |= alice
884
+
885
+ def test_subgraph_union (self ):
873
886
graph_1 = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
874
887
graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
875
888
graph = graph_1 | graph_2
876
889
assert len (graph .nodes ) == 4
877
890
assert len (graph .relationships ) == 5
878
891
assert graph .nodes == (alice | bob | carol | dave ).nodes
879
892
893
+ def test_subgraph_union_in_place (self ):
894
+ graph = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
895
+ graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
896
+ graph |= graph_2
897
+ assert len (graph .nodes ) == 4
898
+ assert len (graph .relationships ) == 5
899
+ assert graph .nodes == (alice | bob | carol | dave ).nodes
900
+
880
901
881
902
class IntersectionTestCase (TestCase ):
882
903
883
- def test_graph_intersection (self ):
904
+ def test_node_intersection_same (self ):
905
+ s = alice & alice
906
+ assert len (s .nodes ) == 1
907
+ assert len (s .relationships ) == 0
908
+
909
+ def test_node_intersection_different (self ):
910
+ s = alice & bob
911
+ assert len (s .nodes ) == 0
912
+ assert len (s .relationships ) == 0
913
+
914
+ def test_node_intersection_in_place (self ):
915
+ n = Node ()
916
+ with raises (TypeError ):
917
+ n &= alice
918
+
919
+ def test_subgraph_intersection (self ):
884
920
graph_1 = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
885
921
graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
886
922
graph = graph_1 & graph_2
887
923
assert len (graph .nodes ) == 2
888
924
assert len (graph .relationships ) == 1
889
925
assert graph .nodes == (bob | carol ).nodes
890
926
927
+ def test_subgraph_intersection_in_place (self ):
928
+ graph = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
929
+ graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
930
+ graph &= graph_2
931
+ assert len (graph .nodes ) == 2
932
+ assert len (graph .relationships ) == 1
933
+ assert graph .nodes == (bob | carol ).nodes
934
+
891
935
892
936
class DifferenceTestCase (TestCase ):
893
937
894
- def test_graph_difference (self ):
938
+ def test_node_difference_same (self ):
939
+ s = alice - alice
940
+ assert len (s .nodes ) == 0
941
+ assert len (s .relationships ) == 0
942
+
943
+ def test_node_difference_different (self ):
944
+ s = alice - bob
945
+ assert len (s .nodes ) == 1
946
+ assert len (s .relationships ) == 0
947
+
948
+ def test_node_difference_in_place (self ):
949
+ n = Node ()
950
+ with raises (TypeError ):
951
+ n -= alice
952
+
953
+ def test_subgraph_difference (self ):
895
954
graph_1 = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
896
955
graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
897
956
graph = graph_1 - graph_2
898
957
assert len (graph .nodes ) == 3
899
958
assert len (graph .relationships ) == 2
900
959
assert graph .nodes == (alice | bob | carol ).nodes
901
960
961
+ def test_subgraph_difference_in_place (self ):
962
+ graph = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
963
+ graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
964
+ graph -= graph_2
965
+ assert len (graph .nodes ) == 3
966
+ assert len (graph .relationships ) == 2
967
+ assert graph .nodes == (alice | bob | carol ).nodes
968
+
902
969
903
970
class SymmetricDifferenceTestCase (TestCase ):
904
971
905
- def test_graph_symmetric_difference (self ):
972
+ def test_node_symmetric_difference_same (self ):
973
+ s = alice ^ alice
974
+ assert len (s .nodes ) == 0
975
+ assert len (s .relationships ) == 0
976
+
977
+ def test_node_symmetric_difference_different (self ):
978
+ s = alice ^ bob
979
+ assert len (s .nodes ) == 2
980
+ assert len (s .relationships ) == 0
981
+
982
+ def test_node_symmetric_difference_in_place (self ):
983
+ n = Node ()
984
+ with raises (TypeError ):
985
+ n ^= alice
986
+
987
+ def test_subgraph_symmetric_difference (self ):
906
988
graph_1 = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
907
989
graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
908
990
graph = graph_1 ^ graph_2
@@ -912,6 +994,16 @@ def test_graph_symmetric_difference(self):
912
994
assert graph .relationships == frozenset (alice_knows_bob | alice_likes_carol |
913
995
carol_married_to_dave | dave_works_for_dave )
914
996
997
+ def test_subgraph_symmetric_difference_in_place (self ):
998
+ graph = (alice_knows_bob | alice_likes_carol | carol_dislikes_bob )
999
+ graph_2 = (carol_dislikes_bob | carol_married_to_dave | dave_works_for_dave )
1000
+ graph ^= graph_2
1001
+ assert len (graph .nodes ) == 4
1002
+ assert len (graph .relationships ) == 4
1003
+ assert graph .nodes == (alice | bob | carol | dave ).nodes
1004
+ assert graph .relationships == frozenset (alice_knows_bob | alice_likes_carol |
1005
+ carol_married_to_dave | dave_works_for_dave )
1006
+
915
1007
916
1008
def test_record_repr ():
917
1009
person = Record (["name" , "age" ], ["Alice" , 33 ])
0 commit comments