Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When an object is already created, and is not replacing, skip all required fields that were not modified. #347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/frameworks/test_motor_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ async def io_validate(field, value):
class Dummy(Document):
required_name = fields.StrField(required=True)
always_io_fail = fields.IntField(io_validate=io_validate)
optional_field = fields.StrField()

with pytest.raises(ma.ValidationError) as exc:
await Dummy().commit()
Expand All @@ -300,6 +301,13 @@ class Dummy(Document):
with pytest.raises(ma.ValidationError) as exc:
await dummy.commit()
assert exc.value.messages == {'required_name': ['Missing data for required field.']}
# Test update of projected document does not require excluded fields
dummy = Dummy(required_name='do_not_fail_on_partial_update')
await dummy.commit()
dummy = await Dummy.find_one({'required_name': 'do_not_fail_on_partial_update'}, {'optional_field': 1})
dummy.optional_field = 'update me!'
await dummy.commit()


loop.run_until_complete(do_test())

Expand Down
7 changes: 7 additions & 0 deletions tests/frameworks/test_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def io_validate(field, value):
class Dummy(Document):
required_name = fields.StrField(required=True)
always_io_fail = fields.IntField(io_validate=io_validate)
optional_field = fields.StrField()

with pytest.raises(ma.ValidationError) as exc:
Dummy().commit()
Expand All @@ -215,6 +216,12 @@ class Dummy(Document):
with pytest.raises(ma.ValidationError) as exc:
dummy.commit()
assert exc.value.messages == {'required_name': ['Missing data for required field.']}
# Test update of projected document does not require excluded fields
dummy = Dummy(required_name='do_not_fail_on_partial_update')
dummy.commit()
dummy = Dummy.find_one({'required_name': 'do_not_fail_on_partial_update'}, {'optional_field': 1})
dummy.optional_field = 'update me!'
dummy.commit()

def test_reference(self, classroom_model):
teacher = classroom_model.Teacher(name='M. Strickland')
Expand Down
7 changes: 7 additions & 0 deletions tests/frameworks/test_txmongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def io_validate(field, value):
class Dummy(Document):
required_name = fields.StrField(required=True)
always_io_fail = fields.IntField(io_validate=io_validate)
optional_field = fields.StrField()

with pytest.raises(ma.ValidationError) as exc:
yield Dummy().commit()
Expand All @@ -256,6 +257,12 @@ class Dummy(Document):
with pytest.raises(ma.ValidationError) as exc:
yield dummy.commit()
assert exc.value.messages == {'required_name': ['Missing data for required field.']}
# Test update of projected document does not require excluded fields
dummy = Dummy(required_name='do_not_fail_on_partial_update')
yield dummy.commit()
dummy = yield Dummy.find_one({'required_name': 'do_not_fail_on_partial_update'}, {'optional_field': 1})
dummy.optional_field = 'update me!'
yield dummy.commit()

@pytest_inlineCallbacks
def test_reference(self, classroom_model):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ class MySchema(BaseSchema):
'listed': {0: {'required': ['Missing data for required field.']}},
'dicted': {'a': {'value': {'required': ['Missing data for required field.']}}},
}
# Required validate should filter on modified fields if passed in
d.load({})
d.required_validate(partial_fields=[])
with pytest.raises(ma.ValidationError) as exc:
d.required_validate(partial_fields=['required'])
assert exc.value.messages == {
'required': ['Missing data for required field.']
}

def test_unkown_field_in_db(self):
class MySchema(BaseSchema):
Expand Down
4 changes: 3 additions & 1 deletion umongo/data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ def _add_missing_fields(self):
else:
self._data[mongo_name] = field.missing

def required_validate(self):
def required_validate(self, partial_fields=None):
errors = {}
for name, field in self.schema.fields.items():
if partial_fields is not None and name not in partial_fields:
continue
value = self._data[field.attribute or name]
if field.required and value is ma.missing:
errors[name] = [_("Missing data for required field.")]
Expand Down
4 changes: 2 additions & 2 deletions umongo/embedded_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def clear_modified(self):
"""
self._data.clear_modified()

def required_validate(self):
self._data.required_validate()
def required_validate(self, partial_fields=None):
self._data.required_validate(partial_fields=partial_fields)

@classmethod
def build_from_mongo(cls, data, use_cls=True):
Expand Down
2 changes: 1 addition & 1 deletion umongo/frameworks/motor_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def commit(self, io_validate_all=False, conditions=None, replace=False):
additional_filter = await self.__coroutined_pre_update()
if additional_filter:
query.update(map_query(additional_filter, self.schema.fields))
self.required_validate()
self.required_validate(self._data.get_modified_fields())
await self.io_validate(validate_all=io_validate_all)
if replace:
payload = self._data.to_mongo(update=False)
Expand Down
2 changes: 1 addition & 1 deletion umongo/frameworks/pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def commit(self, io_validate_all=False, conditions=None, replace=False):
additional_filter = self.pre_update()
if additional_filter:
query.update(map_query(additional_filter, self.schema.fields))
self.required_validate()
self.required_validate(partial_fields=self._data.get_modified_fields())
self.io_validate(validate_all=io_validate_all)
if replace:
payload = self._data.to_mongo(update=False)
Expand Down
2 changes: 1 addition & 1 deletion umongo/frameworks/txmongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def commit(self, io_validate_all=False, conditions=None, replace=False):
additional_filter = yield maybeDeferred(self.pre_update)
if additional_filter:
query.update(map_query(additional_filter, self.schema.fields))
self.required_validate()
self.required_validate(self._data.get_modified_fields())
yield self.io_validate(validate_all=io_validate_all)
if replace:
payload = self._data.to_mongo(update=False)
Expand Down