Skip to content

Commit

Permalink
Rename identity to id
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhen Li committed Mar 18, 2016
1 parent e6d031b commit 5ecd06d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
41 changes: 20 additions & 21 deletions neo4j/v1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
of these classes.
"""


from .packstream import Structure


class Entity(object):
""" Base class for Node and Relationship.
"""
identity = None
id = None
properties = None

def __init__(self, properties=None, **kwproperties):
Expand All @@ -41,15 +40,15 @@ def __init__(self, properties=None, **kwproperties):

def __eq__(self, other):
try:
return self.identity == other.identity
return self.id == other.id
except AttributeError:
return False

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash(self.identity)
return hash(self.id)

def __len__(self):
return len(self.properties)
Expand Down Expand Up @@ -82,18 +81,18 @@ class Node(Entity):
labels = None

@classmethod
def hydrate(cls, identity, labels, properties=None):
def hydrate(cls, id_, labels, properties=None):
inst = cls(labels, properties)
inst.identity = identity
inst.id = id_
return inst

def __init__(self, labels=None, properties=None, **kwproperties):
super(Node, self).__init__(properties, **kwproperties)
self.labels = set(labels or set())

def __repr__(self):
return "<Node identity=%r labels=%r properties=%r>" % \
(self.identity, self.labels, self.properties)
return "<Node id=%r labels=%r properties=%r>" % \
(self.id, self.labels, self.properties)


class BaseRelationship(Entity):
Expand All @@ -113,9 +112,9 @@ class Relationship(BaseRelationship):
end = None

@classmethod
def hydrate(cls, identity, start, end, type, properties=None):
def hydrate(cls, id_, start, end, type, properties=None):
inst = cls(start, end, type, properties)
inst.identity = identity
inst.id = id_
return inst

def __init__(self, start, end, type, properties=None, **kwproperties):
Expand All @@ -126,12 +125,12 @@ def __init__(self, start, end, type, properties=None, **kwproperties):
self.end = end

def __repr__(self):
return "<Relationship identity=%r start=%r end=%r type=%r properties=%r>" % \
(self.identity, self.start, self.end, self.type, self.properties)
return "<Relationship id=%r start=%r end=%r type=%r properties=%r>" % \
(self.id, self.start, self.end, self.type, self.properties)

def unbind(self):
inst = UnboundRelationship(self.type, self.properties)
inst.identity = self.identity
inst.id = self.id
return inst


Expand All @@ -140,21 +139,21 @@ class UnboundRelationship(BaseRelationship):
"""

@classmethod
def hydrate(cls, identity, type, properties=None):
def hydrate(cls, id_, type, properties=None):
inst = cls(type, properties)
inst.identity = identity
inst.id = id_
return inst

def __init__(self, type, properties=None, **kwproperties):
super(UnboundRelationship, self).__init__(type, properties, **kwproperties)

def __repr__(self):
return "<UnboundRelationship identity=%r type=%r properties=%r>" % \
(self.identity, self.type, self.properties)
return "<UnboundRelationship id=%r type=%r properties=%r>" % \
(self.id, self.type, self.properties)

def bind(self, start, end):
inst = Relationship(start, end, self.type, self.properties)
inst.identity = self.identity
inst.id = self.id
return inst


Expand All @@ -174,9 +173,9 @@ def hydrate(cls, nodes, rels, sequence):
assert rel_index != 0
next_node = nodes[sequence[2 * i + 1]]
if rel_index > 0:
entities.append(rels[rel_index - 1].bind(last_node.identity, next_node.identity))
entities.append(rels[rel_index - 1].bind(last_node.id, next_node.id))
else:
entities.append(rels[-rel_index - 1].bind(next_node.identity, last_node.identity))
entities.append(rels[-rel_index - 1].bind(next_node.id, last_node.id))
entities.append(next_node)
last_node = next_node
return cls(*entities)
Expand All @@ -187,7 +186,7 @@ def __init__(self, start_node, *rels_and_nodes):

def __repr__(self):
return "<Path start=%r end=%r size=%s>" % \
(self.start.identity, self.end.identity, len(self))
(self.start.id, self.end.id, len(self))

def __eq__(self, other):
try:
Expand Down
16 changes: 8 additions & 8 deletions test/tck/resultparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ def get_path(string_path):
string_path = string_path[1:-1]
n, string_path = get_node(string_path)
list_of_nodes_and_rel = [n]
n.identity = ++id
n.id = ++id
while string_path != '':
r, string_path, point_up = get_relationship(string_path)
n, string_path = get_node(string_path)
n.identity = ++id
n.id = ++id
if point_up:
r.start = list_of_nodes_and_rel[-1].identity
r.end = n.identity
r.identity = 0
r.start = list_of_nodes_and_rel[-1].id
r.end = n.id
r.id = 0
else:
r.start = n.identity
r.end = list_of_nodes_and_rel[-1].identity
r.identity = 0
r.start = n.id
r.end = list_of_nodes_and_rel[-1].id
r.id = 0
list_of_nodes_and_rel.append(r)
list_of_nodes_and_rel.append(n)
path = Path(list_of_nodes_and_rel[0], *list_of_nodes_and_rel[1:])
Expand Down
4 changes: 2 additions & 2 deletions test/tck/tck_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ def create_node(self, entity):

def create_path(self, entity):
content = {}
prev_id = entity.start.identity
prev_id = entity.start.id
p = []
for i, rel in enumerate(list(entity)):
n = entity.nodes[i + 1]
current_id = n.identity
current_id = n.id
if rel.start == prev_id and rel.end == current_id:
rel.start = i
rel.end = i + 1
Expand Down
40 changes: 20 additions & 20 deletions test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ def test_null_properties(self):

def test_node_equality(self):
node_1 = Node()
node_1.identity = 1234
node_1.id = 1234
node_2 = Node()
node_2.identity = 1234
node_2.id = 1234
node_3 = Node()
node_3.identity = 5678
node_3.id = 5678
assert node_1 == node_2
assert node_1 != node_3
assert node_1 != "this is not a node"

def test_node_hashing(self):
node_1 = Node()
node_1.identity = 1234
node_1.id = 1234
node_2 = Node()
node_2.identity = 1234
node_2.id = 1234
node_3 = Node()
node_3.identity = 5678
node_3.id = 5678
assert hash(node_1) == hash(node_2)
assert hash(node_1) != hash(node_3)

Expand All @@ -82,10 +82,10 @@ class RelationshipTestCase(TestCase):
def test_can_create_relationship(self):
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
assert alice_knows_bob.start == alice.identity
alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
assert alice_knows_bob.start == alice.id
assert alice_knows_bob.type == "KNOWS"
assert alice_knows_bob.end == bob.identity
assert alice_knows_bob.end == bob.id
assert set(alice_knows_bob.keys()) == {"since"}
assert set(alice_knows_bob.values()) == {1999}
assert set(alice_knows_bob.items()) == {("since", 1999)}
Expand All @@ -111,8 +111,8 @@ def test_can_create_path(self):
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
path = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
assert path.start == alice
assert path.end == carol
Expand All @@ -125,8 +125,8 @@ def test_can_hydrate_path(self):
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
path = Path.hydrate([alice, bob, carol],
[alice_knows_bob.unbind(), carol_dislikes_bob.unbind()],
[1, 1, -2, 2])
Expand All @@ -141,8 +141,8 @@ def test_path_equality(self):
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
path_1 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
path_2 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
assert path_1 == path_2
Expand All @@ -152,8 +152,8 @@ def test_path_hashing(self):
alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
alice_knows_bob = Relationship(alice.identity, bob.identity, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.identity, bob.identity, "DISLIKES")
alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
path_1 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
path_2 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
assert hash(path_1) == hash(path_2)
Expand All @@ -167,7 +167,7 @@ def test_can_hydrate_node_structure(self):
struct.append(["Person"])
struct.append({"name": "Alice"})
alice = hydrated(struct)
assert alice.identity == 123
assert alice.id == 123
assert alice.labels == {"Person"}
assert set(alice.keys()) == {"name"}
assert alice.get("name") == "Alice"
Expand All @@ -186,7 +186,7 @@ def test_can_hydrate_in_list(self):
alice_in_list = hydrated([struct])
assert isinstance(alice_in_list, list)
alice, = alice_in_list
assert alice.identity == 123
assert alice.id == 123
assert alice.labels == {"Person"}
assert set(alice.keys()) == {"name"}
assert alice.get("name") == "Alice"
Expand All @@ -199,7 +199,7 @@ def test_can_hydrate_in_dict(self):
alice_in_dict = hydrated({"foo": struct})
assert isinstance(alice_in_dict, dict)
alice = alice_in_dict["foo"]
assert alice.identity == 123
assert alice.id == 123
assert alice.labels == {"Person"}
assert set(alice.keys()) == {"name"}
assert alice.get("name") == "Alice"

0 comments on commit 5ecd06d

Please sign in to comment.