1818# See the License for the specific language governing permissions and
1919# limitations under the License.
2020
21+ from itertools import product
2122
22- from unittest import TestCase
23+ import pytest
2324
2425from neo4j .data import DataHydrator
2526from neo4j .graph import (
3940def test_can_create_node ():
4041 g = Graph ()
4142 gh = Graph .Hydrator (g )
42- alice = gh .hydrate_node (1 , {"Person" }, {"name" : "Alice" , "age" : 33 })
43+ alice = gh .hydrate_node (123 , {"Person" }, {"name" : "Alice" , "age" : 33 })
4344 assert isinstance (alice , Node )
45+ assert alice .id == 123
4446 assert alice .labels == {"Person" }
4547 assert set (alice .keys ()) == {"name" , "age" }
4648 assert set (alice .values ()) == {"Alice" , 33 }
4749 assert set (alice .items ()) == {("name" , "Alice" ), ("age" , 33 )}
50+ assert dict (alice ) == {"name" : "Alice" , "age" : 33 }
4851 assert alice .get ("name" ) == "Alice"
4952 assert alice .get ("age" ) == 33
53+ assert alice .get ("unknown" ) is None
54+ assert alice .get ("unknown" , None ) is None
55+ assert alice .get ("unknown" , False ) is False
56+ assert alice .get ("unknown" , "default" ) == "default"
5057 assert len (alice ) == 2
5158 assert alice ["name" ] == "Alice"
5259 assert alice ["age" ] == 33
@@ -55,10 +62,11 @@ def test_can_create_node():
5562 assert set (iter (alice )) == {"name" , "age" }
5663
5764
58- def test_null_properties ():
65+ def test_node_with_null_properties ():
5966 g = Graph ()
6067 gh = Graph .Hydrator (g )
61- stuff = gh .hydrate_node (1 , (), {"good" : ["puppies" , "kittens" ], "bad" : None })
68+ stuff = gh .hydrate_node (1 , (), {"good" : ["puppies" , "kittens" ],
69+ "bad" : None })
6270 assert isinstance (stuff , Node )
6371 assert set (stuff .keys ()) == {"good" }
6472 assert stuff .get ("good" ) == ["puppies" , "kittens" ]
@@ -70,13 +78,26 @@ def test_null_properties():
7078 assert "bad" not in stuff
7179
7280
73- def test_node_equality ():
74- g = Graph ()
75- node_1 = Node (g , 1234 )
76- node_2 = Node (g , 1234 )
77- node_3 = Node (g , 5678 )
78- assert node_1 == node_2
79- assert node_1 != node_3
81+ @pytest .mark .parametrize (("g1" , "id1" , "props1" , "g2" , "id2" , "props2" ), (
82+ (* n1 , * n2 )
83+ for n1 , n2 in product (
84+ (
85+ (g , id_ , props )
86+ for g in (0 , 1 )
87+ for id_ in (1 , 1234 )
88+ for props in (None , {}, {"a" : 1 })
89+ ),
90+ repeat = 2
91+ )
92+ ))
93+ def test_node_equality (g1 , id1 , props1 , g2 , id2 , props2 ):
94+ graphs = (Graph (), Graph ())
95+ node_1 = Node (graphs [g1 ], id1 , props1 )
96+ node_2 = Node (graphs [g2 ], id2 , props2 )
97+ if g1 == g2 and id1 == id2 :
98+ assert node_1 == node_2
99+ else :
100+ assert node_1 != node_2
80101 assert node_1 != "this is not a node"
81102
82103
@@ -109,6 +130,7 @@ def test_can_create_relationship():
109130 assert alice_knows_bob .start_node == alice
110131 assert alice_knows_bob .type == "KNOWS"
111132 assert alice_knows_bob .end_node == bob
133+ assert dict (alice_knows_bob ) == {"since" : 1999 }
112134 assert set (alice_knows_bob .keys ()) == {"since" }
113135 assert set (alice_knows_bob .values ()) == {1999 }
114136 assert set (alice_knows_bob .items ()) == {("since" , 1999 )}
@@ -133,32 +155,41 @@ def test_can_create_path():
133155 alice = gh .hydrate_node (1 , {"Person" }, {"name" : "Alice" , "age" : 33 })
134156 bob = gh .hydrate_node (2 , {"Person" }, {"name" : "Bob" , "age" : 44 })
135157 carol = gh .hydrate_node (3 , {"Person" }, {"name" : "Carol" , "age" : 55 })
136- alice_knows_bob = gh .hydrate_relationship (1 , alice .id , bob .id , "KNOWS" , {"since" : 1999 })
137- carol_dislikes_bob = gh .hydrate_relationship (2 , carol .id , bob .id , "DISLIKES" , {})
158+ alice_knows_bob = gh .hydrate_relationship (1 , alice .id , bob .id , "KNOWS" ,
159+ {"since" : 1999 })
160+ carol_dislikes_bob = gh .hydrate_relationship (2 , carol .id , bob .id ,
161+ "DISLIKES" , {})
138162 path = Path (alice , alice_knows_bob , carol_dislikes_bob )
139163 assert isinstance (path , Path )
140- assert path .start_node == alice
141- assert path .end_node == carol
164+ assert path .start_node is alice
165+ assert path .end_node is carol
142166 assert path .nodes == (alice , bob , carol )
143167 assert path .relationships == (alice_knows_bob , carol_dislikes_bob )
144168 assert list (path ) == [alice_knows_bob , carol_dislikes_bob ]
145169
146170
147- def test_can_hydrate_path ():
171+ @pytest .mark .parametrize ("cyclic" , (True , False ))
172+ def test_can_hydrate_path (cyclic ):
148173 g = Graph ()
149174 gh = Graph .Hydrator (g )
150175 alice = gh .hydrate_node (1 , {"Person" }, {"name" : "Alice" , "age" : 33 })
151176 bob = gh .hydrate_node (2 , {"Person" }, {"name" : "Bob" , "age" : 44 })
152- carol = gh .hydrate_node (3 , {"Person" }, {"name" : "Carol" , "age" : 55 })
177+ if cyclic :
178+ carol = alice
179+ else :
180+ carol = gh .hydrate_node (3 , {"Person" }, {"name" : "Carol" , "age" : 55 })
153181 r = [gh .hydrate_unbound_relationship (1 , "KNOWS" , {"since" : 1999 }),
154182 gh .hydrate_unbound_relationship (2 , "DISLIKES" , {})]
155183 path = gh .hydrate_path ([alice , bob , carol ], r , [1 , 1 , - 2 , 2 ])
156- assert path .start_node == alice
157- assert path .end_node == carol
184+ assert path .start_node is alice
185+ assert path .end_node is carol
158186 assert path .nodes == (alice , bob , carol )
159- expected_alice_knows_bob = gh .hydrate_relationship (1 , alice .id , bob .id , "KNOWS" , {"since" : 1999 })
160- expected_carol_dislikes_bob = gh .hydrate_relationship (2 , carol .id , bob .id , "DISLIKES" , {})
161- assert path .relationships == (expected_alice_knows_bob , expected_carol_dislikes_bob )
187+ expected_alice_knows_bob = gh .hydrate_relationship (1 , alice .id , bob .id ,
188+ "KNOWS" , {"since" : 1999 })
189+ expected_carol_dislikes_bob = gh .hydrate_relationship (2 , carol .id , bob .id ,
190+ "DISLIKES" , {})
191+ assert path .relationships == (expected_alice_knows_bob ,
192+ expected_carol_dislikes_bob )
162193 assert list (path ) == [expected_alice_knows_bob , expected_carol_dislikes_bob ]
163194
164195
0 commit comments