Skip to content

Commit

Permalink
Check for _id property in subdocument
Browse files Browse the repository at this point in the history
If a subdocument contains an _id property, do not treat it as a
reference/relationship, instead ignore the _id property as the
_id from the root level document should take precedence. See #56
  • Loading branch information
johnymontana committed Feb 3, 2016
1 parent 7655b1a commit 8d8aab2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def build_node_with_reference(self, root_type, key, doc_id, document_key):
if document_key is None:
return
doc_type = key.split("_id")[0]

# ignore _id property of subdocuments
if not doc_type or doc_type == "":
return

parameters = {'_id':document_key}
statement = "MERGE (d:Document:`{doc_type}` {{ _id: {{parameters}}._id}})".format(doc_type=doc_type)
self.query_nodes.update({statement: {"parameters":parameters}})
Expand Down
31 changes: 31 additions & 0 deletions tests/test_neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def setUpClass(cls):
cls.docman = DocManager('http://localhost:7474/db/data', auto_commit_interval=0)

def _count(cls):
"""
Return the number of nodes in the graph
"""
return cls.neo4j_conn.order

def query(cls):
Expand Down Expand Up @@ -93,6 +96,34 @@ def test_insert(self):
self.assertEqual(result_set_1['name'], result_set_2['name'])
self.connector.doc_managers[0].graph.delete_all()

def test_subdocument_with_id(self):
"""
Test inserting a document with a subdocument containing an _id property.
In the current version of translating from document data model to property graph, the root level _id
field is included in all subdocument nodes in the graph. This test verifies there is no error when
inserting a document containing a subdocument with an _id property.
See https://github.com/neo4j-contrib/neo4j_doc_manager/issues/56
"""

doc = {
'_id': 'root_level_object_id',
'name': 'Bob',
'location': {
'_id': 'sub_document_object_id',
'city': 'Missoula',
'state': 'Montana'
}
}

self.connector.doc_managers[0].upsert(doc, "test.test_id", 1)
assert_soon(lambda: self._count() > 0)
result_set = self.neo4j_conn.find_one("location")
self.assertNotEqual(result_set, None)
self.assertEqual(result_set['_id'], 'root_level_object_id') # expect subdocument _id to be ignored


def test_remove(self):
"""Tests remove operations."""
self.conn['test']['test'].insert({'name': 'paulie'})
Expand Down

0 comments on commit 8d8aab2

Please sign in to comment.