Merge pull request #417 from ProgressiveCompany/delta-dbref-false-bug
BaseDocument._delta doesn't properly end it's path at Documents when using `dbref=False`
This commit is contained in:
commit
a66d516777
@ -4,7 +4,7 @@ import numbers
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
from bson import json_util
|
from bson import json_util, ObjectId
|
||||||
from bson.dbref import DBRef
|
from bson.dbref import DBRef
|
||||||
from bson.son import SON
|
from bson.son import SON
|
||||||
|
|
||||||
@ -454,7 +454,7 @@ class BaseDocument(object):
|
|||||||
d = doc
|
d = doc
|
||||||
new_path = []
|
new_path = []
|
||||||
for p in parts:
|
for p in parts:
|
||||||
if isinstance(d, DBRef):
|
if isinstance(d, (ObjectId, DBRef)):
|
||||||
break
|
break
|
||||||
elif isinstance(d, list) and p.isdigit():
|
elif isinstance(d, list) and p.isdigit():
|
||||||
d = d[int(p)]
|
d = d[int(p)]
|
||||||
|
@ -313,17 +313,17 @@ class DeltaTest(unittest.TestCase):
|
|||||||
self.circular_reference_deltas_2(DynamicDocument, Document)
|
self.circular_reference_deltas_2(DynamicDocument, Document)
|
||||||
self.circular_reference_deltas_2(DynamicDocument, DynamicDocument)
|
self.circular_reference_deltas_2(DynamicDocument, DynamicDocument)
|
||||||
|
|
||||||
def circular_reference_deltas_2(self, DocClass1, DocClass2):
|
def circular_reference_deltas_2(self, DocClass1, DocClass2, dbref=True):
|
||||||
|
|
||||||
class Person(DocClass1):
|
class Person(DocClass1):
|
||||||
name = StringField()
|
name = StringField()
|
||||||
owns = ListField(ReferenceField('Organization'))
|
owns = ListField(ReferenceField('Organization', dbref=dbref))
|
||||||
employer = ReferenceField('Organization')
|
employer = ReferenceField('Organization', dbref=dbref)
|
||||||
|
|
||||||
class Organization(DocClass2):
|
class Organization(DocClass2):
|
||||||
name = StringField()
|
name = StringField()
|
||||||
owner = ReferenceField('Person')
|
owner = ReferenceField('Person', dbref=dbref)
|
||||||
employees = ListField(ReferenceField('Person'))
|
employees = ListField(ReferenceField('Person', dbref=dbref))
|
||||||
|
|
||||||
Person.drop_collection()
|
Person.drop_collection()
|
||||||
Organization.drop_collection()
|
Organization.drop_collection()
|
||||||
@ -355,6 +355,8 @@ class DeltaTest(unittest.TestCase):
|
|||||||
self.assertEqual(o.owner, p)
|
self.assertEqual(o.owner, p)
|
||||||
self.assertEqual(e.employer, o)
|
self.assertEqual(e.employer, o)
|
||||||
|
|
||||||
|
return person, organization, employee
|
||||||
|
|
||||||
def test_delta_db_field(self):
|
def test_delta_db_field(self):
|
||||||
self.delta_db_field(Document)
|
self.delta_db_field(Document)
|
||||||
self.delta_db_field(DynamicDocument)
|
self.delta_db_field(DynamicDocument)
|
||||||
@ -686,6 +688,29 @@ class DeltaTest(unittest.TestCase):
|
|||||||
self.assertEqual(doc._get_changed_fields(), ['list_field'])
|
self.assertEqual(doc._get_changed_fields(), ['list_field'])
|
||||||
self.assertEqual(doc._delta(), ({}, {'list_field': 1}))
|
self.assertEqual(doc._delta(), ({}, {'list_field': 1}))
|
||||||
|
|
||||||
|
def test_delta_with_dbref_true(self):
|
||||||
|
person, organization, employee = self.circular_reference_deltas_2(Document, Document, True)
|
||||||
|
employee.name = 'test'
|
||||||
|
|
||||||
|
self.assertEqual(organization._get_changed_fields(), ['employees.0.name'])
|
||||||
|
|
||||||
|
updates, removals = organization._delta()
|
||||||
|
self.assertEqual({}, removals)
|
||||||
|
self.assertTrue('employees.0' in updates)
|
||||||
|
|
||||||
|
organization.save()
|
||||||
|
|
||||||
|
def test_delta_with_dbref_false(self):
|
||||||
|
person, organization, employee = self.circular_reference_deltas_2(Document, Document, False)
|
||||||
|
employee.name = 'test'
|
||||||
|
|
||||||
|
self.assertEqual(organization._get_changed_fields(), ['employees.0.name'])
|
||||||
|
|
||||||
|
updates, removals = organization._delta()
|
||||||
|
self.assertEqual({}, removals)
|
||||||
|
self.assertTrue('employees.0' in updates)
|
||||||
|
|
||||||
|
organization.save()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user