Skip to content

Commit

Permalink
Merge pull request #253 from Scille/fix_partial_not_loaded_access
Browse files Browse the repository at this point in the history
Don't modify data proxy on partial not loaded access
  • Loading branch information
lafrech committed Apr 25, 2020
2 parents f2752e9 + 414c655 commit 2594b7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 15 additions & 0 deletions tests/test_data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,27 @@ class MySchema(EmbeddedSchema):
d.from_mongo({'loaded': "foo", 'loaded_but_empty': missing}, partial=True)
assert d.partial is True
for field in ('with_default', 'normal'):
val = d._data[field]
with pytest.raises(exceptions.FieldNotLoadedError):
d.get(field)
assert d._data[field] == val
with pytest.raises(exceptions.FieldNotLoadedError):
d.set(field, "test")
assert d._data[field] == val
with pytest.raises(exceptions.FieldNotLoadedError):
d.delete(field)
assert d._data[field] == val
for field in ('normal', 'in_mongo_field'):
val = d._data[field]
with pytest.raises(exceptions.FieldNotLoadedError):
d.get_by_mongo_name(field)
assert d._data[field] == val
with pytest.raises(exceptions.FieldNotLoadedError):
d.set_by_mongo_name(field, "test")
assert d._data[field] == val
with pytest.raises(exceptions.FieldNotLoadedError):
d.delete_by_mongo_name(field)
assert d._data[field] == val
assert d.get('loaded') == "foo"
assert d.get('loaded_but_empty') is missing
d.set('loaded_but_empty', "bar")
Expand Down
5 changes: 2 additions & 3 deletions umongo/data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,14 @@ def load(self, data, partial=False):
self._add_missing_fields()

def get_by_mongo_name(self, name):
value = self._data[name]
if self._fields_from_mongo_key[name] in self.not_loaded_fields:
raise FieldNotLoadedError(name)
return value
return self._data[name]

def set_by_mongo_name(self, name, value):
self._data[name] = value
if self._fields_from_mongo_key[name] in self.not_loaded_fields:
raise FieldNotLoadedError(name)
self._data[name] = value
self._mark_as_modified(name)

def delete_by_mongo_name(self, name):
Expand Down

0 comments on commit 2594b7b

Please sign in to comment.