Skip to content

Commit

Permalink
Merge pull request #121 from Nobatek/dev_unknown_field_in_db_error
Browse files Browse the repository at this point in the history
Raise exception with Document name when unknown field found in DB
  • Loading branch information
touilleMan authored Oct 24, 2017
2 parents ec95d87 + 6b1c859 commit 90368d7
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 11 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ History

dev
---
* Raise UnknownFieldInDBError when an unknown field is found in database
and not using BaseNonStrictDataProxy (see #121)
* Fix (non fatal) crash in garbage collector when using WrappedCursor with mongomock

0.15.0 (2017-08-15)
Expand Down
Binary file modified examples/flask/translations/fr/LC_MESSAGES/messages.mo
Binary file not shown.
10 changes: 7 additions & 3 deletions examples/flask/translations/fr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: emmanuel.leblond@gmail.com\n"
"POT-Creation-Date: 2017-10-23 10:11+0200\n"
"POT-Creation-Date: 2017-10-24 09:39+0200\n"
"PO-Revision-Date: 2016-04-19 12:09+0200\n"
"Last-Translator: Emmanuel Leblond <emmanuel.leblond@gmail.com>\n"
"Last-Translator: Jérôme Lafréchoux <jerome@jolimont.fr>\n"
"Language: fr\n"
"Language-Team: fr <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n > 1)\n"
Expand All @@ -30,8 +30,12 @@ msgstr "L'ensemble des valeurs des champs {fields} doit être unique."
msgid "Reference not found for document {document}."
msgstr "Référence non trouvée pour document {document}."

#: umongo/data_proxy.py:72
msgid "{cls}: unknown \"{key}\" field found in DB."
msgstr "{cls}: Champ \"{key}\" inconnu trouvé dans la base de données."

# Marshmallow fields #
#: umongo/data_proxy.py:217
#: umongo/data_proxy.py:222
msgid "Missing data for required field."
msgstr "Valeur manquante pour un champ obligatoire."

Expand Down
10 changes: 7 additions & 3 deletions messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ msgid ""
msgstr ""
"Project-Id-Version: umongo 0.15.0\n"
"Report-Msgid-Bugs-To: emmanuel.leblond@gmail.com\n"
"POT-Creation-Date: 2017-10-23 10:11+0200\n"
"POT-Creation-Date: 2017-10-24 09:39+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: Emmanuel Leblond <emmanuel.leblond@gmail.com>\n"
"Last-Translator: Jérôme Lafréchoux <jerome@jolimont.fr>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
Expand All @@ -29,7 +29,11 @@ msgstr ""
msgid "Reference not found for document {document}."
msgstr ""

#: umongo/data_proxy.py:217
#: umongo/data_proxy.py:72
msgid "{cls}: unknown \"{key}\" field found in DB."
msgstr ""

#: umongo/data_proxy.py:222
msgid "Missing data for required field."
msgstr ""

Expand Down
13 changes: 12 additions & 1 deletion tests/test_data_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class MySchema(EmbeddedSchema):

MyDataProxy = data_proxy_factory('My', MySchema())
d = MyDataProxy()
with pytest.raises(KeyError):
with pytest.raises(exceptions.UnknownFieldInDBError):
d.from_mongo({'in_front': 42})
d.from_mongo({'in_mongo': 42})
assert d.get('in_front') == 42
Expand Down Expand Up @@ -424,6 +424,17 @@ class MySchema(EmbeddedSchema):
d.required_validate()
assert exc.value.messages == {'listed': {0: {'required': ['Missing data for required field.']}}}

def test_unkown_field_in_db(self):
class MySchema(EmbeddedSchema):
field = fields.IntField(attribute='mongo_field')

DataProxy = data_proxy_factory('My', MySchema())
d = DataProxy()
d.from_mongo({'mongo_field': 42})
assert d._data == {'mongo_field': 42}
with pytest.raises(exceptions.UnknownFieldInDBError):
d.from_mongo({'mongo_field': 42, 'xxx': 'foo'})


class TestNonStrictDataProxy(BaseTest):

Expand Down
2 changes: 1 addition & 1 deletion tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ class Meta:
strict = False

data_with_bonus = {'a': 42, 'b': 'foo'}
with pytest.raises(KeyError):
with pytest.raises(exceptions.UnknownFieldInDBError):
StrictDoc.build_from_mongo(data_with_bonus)

non_strict_doc = NonStrictDoc.build_from_mongo(data_with_bonus)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_embedded_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ class Meta:
strict = False

data_with_bonus = {'a': 42, 'b': 'foo'}
with pytest.raises(KeyError):
with pytest.raises(exceptions.UnknownFieldInDBError):
StrictEmbeddedDoc.build_from_mongo(data_with_bonus)

non_strict_doc = NonStrictEmbeddedDoc.build_from_mongo(data_with_bonus)
Expand Down
9 changes: 7 additions & 2 deletions umongo/data_proxy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from marshmallow import ValidationError, missing

from .abstract import BaseDataObject
from .exceptions import FieldNotLoadedError
from .exceptions import FieldNotLoadedError, UnknownFieldInDBError
from .i18n import gettext as _


Expand Down Expand Up @@ -64,7 +64,12 @@ def _to_mongo_update(self):
def from_mongo(self, data, partial=False):
self._data = {}
for k, v in data.items():
field = self._fields_from_mongo_key[k]
try:
field = self._fields_from_mongo_key[k]
except KeyError:
raise UnknownFieldInDBError(
_('{cls}: unknown "{key}" field found in DB.'
.format(key=k, cls=self.__class__.__name__)))
self._data[k] = field.deserialize_from_mongo(v)
if partial:
self._collect_partial_fields(data.keys(), as_mongo_fields=True)
Expand Down
4 changes: 4 additions & 0 deletions umongo/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ class FieldNotLoadedError(UMongoError):

class NoCompatibleBuilderError(UMongoError):
pass


class UnknownFieldInDBError(UMongoError):
pass

0 comments on commit 90368d7

Please sign in to comment.