From 071562d75534b95a4f7ed415290659b8e4a4042e Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 2 Dec 2011 00:11:25 -0800 Subject: [PATCH] Fixed issue with dynamic documents deltas Closes #377 --- AUTHORS | 1 + docs/changelog.rst | 1 + mongoengine/base.py | 3 ++- tests/dynamic_document.py | 9 +++++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 0dce8c41..d5433943 100644 --- a/AUTHORS +++ b/AUTHORS @@ -82,3 +82,4 @@ that much better: * aid * yamaneko1212 * dave mankoff + * Alexander G. Morano diff --git a/docs/changelog.rst b/docs/changelog.rst index cbb20e37..30d67074 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Fixed issue with dynamic documents deltas - Added Reverse Delete Rule support to ListFields - MapFields aren't supported - Added customisable cascade kwarg options - Fixed Handle None values for non-required fields diff --git a/mongoengine/base.py b/mongoengine/base.py index 2fd40ec4..29be8764 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -954,6 +954,7 @@ class BaseDocument(object): set_fields = self._get_changed_fields() set_data = {} unset_data = {} + parts = [] if hasattr(self, '_changed_fields'): set_data = {} # Fetch each set item from its path @@ -980,7 +981,7 @@ class BaseDocument(object): # If we've set a value that ain't the default value dont unset it. default = None - if self._dynamic and parts[0] in self._dynamic_fields: + if self._dynamic and len(parts) and parts[0] in self._dynamic_fields: del(set_data[path]) unset_data[path] = 1 continue diff --git a/tests/dynamic_document.py b/tests/dynamic_document.py index 3d808a18..b40ba062 100644 --- a/tests/dynamic_document.py +++ b/tests/dynamic_document.py @@ -36,6 +36,15 @@ class DynamicDocTest(unittest.TestCase): # Confirm no changes to self.Person self.assertFalse(hasattr(self.Person, 'age')) + def test_dynamic_document_delta(self): + """Ensures simple dynamic documents can delta correctly""" + p = self.Person(name="James", age=34) + self.assertEquals(p._delta(), ({'_types': ['Person'], 'age': 34, 'name': 'James', '_cls': 'Person'}, {})) + + p.doc = 123 + del(p.doc) + self.assertEquals(p._delta(), ({'_types': ['Person'], 'age': 34, 'name': 'James', '_cls': 'Person'}, {'doc': 1})) + def test_change_scope_of_variable(self): """Test changing the scope of a dynamic field has no adverse effects""" p = self.Person()