Skip to content

Commit

Permalink
Fixes #903 by reintroducing entity-level label/key overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
technige committed Jun 1, 2021
1 parent 591db36 commit b8d0b43
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
23 changes: 19 additions & 4 deletions py2neo/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,21 @@ def __db_merge__(self, tx, primary_label=None, primary_key=None):
node_dict = {}
for node in self.nodes:
if not self._is_bound(node, graph):
if node.__model__ is None:
p_label = primary_label
p_key = primary_key
else:
# Determine primary label
if node.__primarylabel__ is not None:
p_label = node.__primarylabel__
elif node.__model__ is not None:
p_label = node.__model__.__primarylabel__ or primary_label
else:
p_label = primary_label
# Determine primary key
if node.__primarykey__ is not None:
p_key = node.__primarykey__
elif node.__model__ is not None:
p_key = node.__model__.__primarykey__ or primary_key
else:
p_key = primary_key
# Add node to the node dictionary
key = (p_label, p_key, frozenset(node.labels))
node_dict.setdefault(key, []).append(node)

Expand Down Expand Up @@ -611,6 +620,12 @@ class Node(Entity):
#: OGM model class
__model__ = None

#: Entity-level override for merge label
__primarylabel__ = None

#: Entity-level override for merge key
__primarykey__ = None

@classmethod
def ref(cls, graph, identity):
obj = cls()
Expand Down
78 changes: 78 additions & 0 deletions test/integration/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,81 @@ class ModelB(Model):
graph.merge(node, label_a, "a")
assert node.identity != a_id
assert node.identity == b_id


def test_can_merge_node_with_merge_arguments(graph):
graph.delete_all()
a = Node("Person", name="Alice")
graph.merge(a, "Person", "name")
assert graph.nodes.match("Person", name="Alice").first() == a


def test_can_merge_node_with_primary_label_and_key(graph):
graph.delete_all()
a = Node("Person", name="Alice")
a.__primarylabel__ = "Person"
a.__primarykey__ = "name"
graph.merge(a)
assert graph.nodes.match("Person", name="Alice").first() == a


def test_can_merge_node_with_model(graph):
from py2neo.ogm import Model

class Person(Model):
__primarylabel__ = "Person"
__primarykey__ = "name"

graph.delete_all()

a = Node("Person", name="Alice")
a.__model__ = Person
graph.merge(a)
assert graph.nodes.match("Person", name="Alice").first() == a


def test_can_merge_node_with_model_overriding_arguments(graph):
from py2neo.ogm import Model

class Person(Model):
__primarylabel__ = "Person"
__primarykey__ = "name"

graph.delete_all()

a = Node("Person", name="Alice")
a.__model__ = Person
graph.merge(a, "Human", "nom")
assert graph.nodes.match("Person", name="Alice").first() == a


def test_can_merge_node_with_primary_label_overriding_model(graph):
from py2neo.ogm import Model

class Person(Model):
__primarylabel__ = "Human"
__primarykey__ = "name"

graph.delete_all()

a = Node("Person", name="Alice")
a.__model__ = Person
a.__primarylabel__ = "Person"
graph.merge(a)
assert graph.nodes.match("Person", name="Alice").first() == a


def test_can_merge_node_with_primary_key_overriding_model(graph):
from py2neo.ogm import Model

class Person(Model):
__primarylabel__ = "Person"
__primarykey__ = "nom"

graph.delete_all()

a = Node("Person", name="Alice")
a.__model__ = Person
a.__primarykey__ = "name"
graph.merge(a)
assert graph.nodes.match("Person", name="Alice").first() == a

0 comments on commit b8d0b43

Please sign in to comment.