Skip to content

Commit dd091ff

Browse files
committed
Ensure we always use identities for relationship endpoints
1 parent 8381e37 commit dd091ff

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

neo4j/v1/typesystem.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def hydrate(cls, identity, start, end, type, properties=None):
119119
return inst
120120

121121
def __init__(self, start, end, type, properties=None, **kwproperties):
122+
assert isinstance(start, int)
123+
assert isinstance(end, int)
122124
super(Relationship, self).__init__(type, properties, **kwproperties)
123125
self.start = start
124126
self.end = end
@@ -172,9 +174,9 @@ def hydrate(cls, nodes, rels, sequence):
172174
assert rel_index != 0
173175
next_node = nodes[sequence[2 * i + 1]]
174176
if rel_index > 0:
175-
entities.append(rels[rel_index - 1].bind(last_node, next_node))
177+
entities.append(rels[rel_index - 1].bind(last_node.identity, next_node.identity))
176178
else:
177-
entities.append(rels[-rel_index - 1].bind(next_node, last_node))
179+
entities.append(rels[-rel_index - 1].bind(next_node.identity, last_node.identity))
178180
entities.append(next_node)
179181
return cls(*entities)
180182

test/test_typesystem.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,35 @@ def test_null_properties(self):
5757

5858
def test_node_equality(self):
5959
node_1 = Node()
60-
node_1.identity = "node/1234"
60+
node_1.identity = 1234
6161
node_2 = Node()
62-
node_2.identity = "node/1234"
62+
node_2.identity = 1234
6363
node_3 = Node()
64-
node_3.identity = "node/5678"
64+
node_3.identity = 5678
6565
assert node_1 == node_2
6666
assert node_1 != node_3
6767
assert node_1 != "this is not a node"
6868

6969
def test_node_hashing(self):
7070
node_1 = Node()
71-
node_1.identity = "node/1234"
71+
node_1.identity = 1234
7272
node_2 = Node()
73-
node_2.identity = "node/1234"
73+
node_2.identity = 1234
7474
node_3 = Node()
75-
node_3.identity = "node/5678"
75+
node_3.identity = 5678
7676
assert hash(node_1) == hash(node_2)
7777
assert hash(node_1) != hash(node_3)
7878

7979

8080
class RelationshipTestCase(TestCase):
8181

8282
def test_can_create_relationship(self):
83-
alice = Node({"Person"}, {"name": "Alice", "age": 33})
84-
bob = Node({"Person"}, {"name": "Bob", "age": 44})
85-
alice_knows_bob = Relationship(alice, bob, "KNOWS", {"since": 1999})
86-
assert alice_knows_bob.start is alice
83+
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
84+
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
85+
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
86+
assert alice_knows_bob.start == alice.identity
8787
assert alice_knows_bob.type == "KNOWS"
88-
assert alice_knows_bob.end is bob
88+
assert alice_knows_bob.end == bob.identity
8989
assert set(alice_knows_bob.keys()) == {"since"}
9090
assert set(alice_knows_bob.values()) == {1999}
9191
assert set(alice_knows_bob.items()) == {("since", 1999)}
@@ -108,11 +108,11 @@ def test_can_create_unbound_relationship(self):
108108
class PathTestCase(TestCase):
109109

110110
def test_can_create_path(self):
111-
alice = Node({"Person"}, {"name": "Alice", "age": 33})
112-
bob = Node({"Person"}, {"name": "Bob", "age": 44})
113-
carol = Node({"Person"}, {"name": "Carol", "age": 55})
114-
alice_knows_bob = Relationship(alice, bob, "KNOWS", {"since": 1999})
115-
carol_dislikes_bob = Relationship(carol, bob, "DISLIKES")
111+
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
112+
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
113+
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
114+
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
115+
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
116116
path = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
117117
assert path.start == alice
118118
assert path.end == carol
@@ -122,11 +122,11 @@ def test_can_create_path(self):
122122
assert repr(path)
123123

124124
def test_can_hydrate_path(self):
125-
alice = Node({"Person"}, {"name": "Alice", "age": 33})
126-
bob = Node({"Person"}, {"name": "Bob", "age": 44})
127-
carol = Node({"Person"}, {"name": "Carol", "age": 55})
128-
alice_knows_bob = Relationship(alice, bob, "KNOWS", {"since": 1999})
129-
carol_dislikes_bob = Relationship(carol, bob, "DISLIKES")
125+
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
126+
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
127+
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
128+
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
129+
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
130130
path = Path.hydrate([alice, bob, carol],
131131
[alice_knows_bob.unbind(), carol_dislikes_bob.unbind()],
132132
[1, 1, -2, 2])
@@ -138,22 +138,22 @@ def test_can_hydrate_path(self):
138138
assert repr(path)
139139

140140
def test_path_equality(self):
141-
alice = Node({"Person"}, {"name": "Alice", "age": 33})
142-
bob = Node({"Person"}, {"name": "Bob", "age": 44})
143-
carol = Node({"Person"}, {"name": "Carol", "age": 55})
144-
alice_knows_bob = Relationship(alice, bob, "KNOWS", {"since": 1999})
145-
carol_dislikes_bob = Relationship(carol, bob, "DISLIKES")
141+
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
142+
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
143+
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
144+
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
145+
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
146146
path_1 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
147147
path_2 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
148148
assert path_1 == path_2
149149
assert path_1 != "this is not a path"
150150

151151
def test_path_hashing(self):
152-
alice = Node({"Person"}, {"name": "Alice", "age": 33})
153-
bob = Node({"Person"}, {"name": "Bob", "age": 44})
154-
carol = Node({"Person"}, {"name": "Carol", "age": 55})
155-
alice_knows_bob = Relationship(alice, bob, "KNOWS", {"since": 1999})
156-
carol_dislikes_bob = Relationship(carol, bob, "DISLIKES")
152+
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
153+
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
154+
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
155+
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
156+
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
157157
path_1 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
158158
path_2 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
159159
assert hash(path_1) == hash(path_2)
@@ -163,11 +163,11 @@ class HydrationTestCase(TestCase):
163163

164164
def test_can_hydrate_node_structure(self):
165165
struct = Structure(3, b'N')
166-
struct.append("node/123")
166+
struct.append(123)
167167
struct.append(["Person"])
168168
struct.append({"name": "Alice"})
169169
alice = hydrated(struct)
170-
assert alice.identity == "node/123"
170+
assert alice.identity == 123
171171
assert alice.labels == {"Person"}
172172
assert set(alice.keys()) == {"name"}
173173
assert alice.get("name") == "Alice"
@@ -180,26 +180,26 @@ def test_hydrating_unknown_structure_returns_same(self):
180180

181181
def test_can_hydrate_in_list(self):
182182
struct = Structure(3, b'N')
183-
struct.append("node/123")
183+
struct.append(123)
184184
struct.append(["Person"])
185185
struct.append({"name": "Alice"})
186186
alice_in_list = hydrated([struct])
187187
assert isinstance(alice_in_list, list)
188188
alice, = alice_in_list
189-
assert alice.identity == "node/123"
189+
assert alice.identity == 123
190190
assert alice.labels == {"Person"}
191191
assert set(alice.keys()) == {"name"}
192192
assert alice.get("name") == "Alice"
193193

194194
def test_can_hydrate_in_dict(self):
195195
struct = Structure(3, b'N')
196-
struct.append("node/123")
196+
struct.append(123)
197197
struct.append(["Person"])
198198
struct.append({"name": "Alice"})
199199
alice_in_dict = hydrated({"foo": struct})
200200
assert isinstance(alice_in_dict, dict)
201201
alice = alice_in_dict["foo"]
202-
assert alice.identity == "node/123"
202+
assert alice.identity == 123
203203
assert alice.labels == {"Person"}
204204
assert set(alice.keys()) == {"name"}
205205
assert alice.get("name") == "Alice"

0 commit comments

Comments
 (0)