Skip to content

Commit

Permalink
Merge pull request #746 from neo4j-contrib/task/reserved-keys
Browse files Browse the repository at this point in the history
Improve reserved keyword error ; add tests
  • Loading branch information
mariusconjeaud authored Sep 22, 2023
2 parents 90b7a5d + 2a3636b commit c8fd6f0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
17 changes: 15 additions & 2 deletions neomodel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,21 @@ def __new__(mcs, name, bases, namespace):
else:
if "deleted" in namespace:
raise ValueError(
"Class property called 'deleted' conflicts "
"with neomodel internals."
"Property name 'deleted' is not allowed as it conflicts with neomodel internals."
)
elif "id" in namespace:
raise ValueError(
"""
Property name 'id' is not allowed as it conflicts with neomodel internals.
Consider using 'uid' or 'identifier' as id is also a Neo4j internal.
"""
)
elif "element_id" in namespace:
raise ValueError(
"""
Property name 'element_id' is not allowed as it conflicts with neomodel internals.
Consider using 'uid' or 'identifier' as element_id is also a Neo4j internal.
"""
)
for key, value in (
(x, y) for x, y in namespace.items() if isinstance(y, Property)
Expand Down
5 changes: 4 additions & 1 deletion neomodel/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def __new__(mcs, name, bases, dct):
inst = super().__new__(mcs, name, bases, dct)
for key, value in dct.items():
if issubclass(value.__class__, Property):
if key == "source" or key == "target":
raise ValueError(
"Property names 'source' and 'target' are not allowed as they conflict with neomodel internals."
)
value.name = key
value.owner = inst

Expand Down Expand Up @@ -153,4 +157,3 @@ def inflate(cls, rel):
srel._end_node_element_id_property = rel.end_node.element_id
srel.element_id_property = rel.element_id
return srel

31 changes: 31 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from neomodel import (
DateProperty,
IntegerProperty,
Relationship,
StringProperty,
StructuredNode,
StructuredRel,
install_labels,
)
from neomodel.core import db
Expand Down Expand Up @@ -296,3 +298,32 @@ class DateTest(StructuredNode):
birthdate = DateProperty()

user = DateTest(birthdate=datetime.now()).save()


def test_reserved_property_keys():
error_match = r".*is not allowed as it conflicts with neomodel internals.*"
with raises(ValueError, match=error_match):

class ReservedPropertiesDeletedNode(StructuredNode):
deleted = StringProperty()

with raises(ValueError, match=error_match):

class ReservedPropertiesIdNode(StructuredNode):
id = StringProperty()

with raises(ValueError, match=error_match):

class ReservedPropertiesElementIdNode(StructuredNode):
element_id = StringProperty()

error_match = r"Property names 'source' and 'target' are not allowed as they conflict with neomodel internals."
with raises(ValueError, match=error_match):

class ReservedPropertiesSourceRel(StructuredRel):
source = StringProperty()

with raises(ValueError, match=error_match):

class ReservedPropertiesTargetRel(StructuredRel):
target = StringProperty()

0 comments on commit c8fd6f0

Please sign in to comment.