Merge pull request #2187 from bagerard/improve_error_message_InvalidDocumentError
Improve error message from InvalidDocumentError
This commit is contained in:
		| @@ -17,6 +17,7 @@ Development | |||||||
|     - If you catch/use ``MongoEngineConnectionError`` in your code, you'll have to rename it. |     - If you catch/use ``MongoEngineConnectionError`` in your code, you'll have to rename it. | ||||||
| - BREAKING CHANGE: Positional arguments when instantiating a document are no longer supported. #2103 | - BREAKING CHANGE: Positional arguments when instantiating a document are no longer supported. #2103 | ||||||
|     - From now on keyword arguments (e.g. ``Doc(field_name=value)``) are required. |     - From now on keyword arguments (e.g. ``Doc(field_name=value)``) are required. | ||||||
|  | - Improve error message related to InvalidDocumentError #2180 | ||||||
| - Fix updating/modifying/deleting/reloading a document that's sharded by a field with ``db_field`` specified. #2125 | - Fix updating/modifying/deleting/reloading a document that's sharded by a field with ``db_field`` specified. #2125 | ||||||
| - ``ListField`` now accepts an optional ``max_length`` parameter. #2110 | - ``ListField`` now accepts an optional ``max_length`` parameter. #2110 | ||||||
| - Switch from nosetest to pytest as test runner #2114 | - Switch from nosetest to pytest as test runner #2114 | ||||||
|   | |||||||
| @@ -732,7 +732,10 @@ class BaseDocument(object): | |||||||
|             only_fields = [] |             only_fields = [] | ||||||
|  |  | ||||||
|         if son and not isinstance(son, dict): |         if son and not isinstance(son, dict): | ||||||
|             raise ValueError("The source SON object needs to be of type 'dict'") |             raise ValueError( | ||||||
|  |                 "The source SON object needs to be of type 'dict' but a '%s' was found" | ||||||
|  |                 % type(son) | ||||||
|  |             ) | ||||||
|  |  | ||||||
|         # Get the class name from the document, falling back to the given |         # Get the class name from the document, falling back to the given | ||||||
|         # class if unavailable |         # class if unavailable | ||||||
| @@ -770,7 +773,9 @@ class BaseDocument(object): | |||||||
|                     errors_dict[field_name] = e |                     errors_dict[field_name] = e | ||||||
|  |  | ||||||
|         if errors_dict: |         if errors_dict: | ||||||
|             errors = "\n".join(["%s - %s" % (k, v) for k, v in errors_dict.items()]) |             errors = "\n".join( | ||||||
|  |                 ["Field '%s' - %s" % (k, v) for k, v in errors_dict.items()] | ||||||
|  |             ) | ||||||
|             msg = "Invalid data to create a `%s` instance.\n%s" % ( |             msg = "Invalid data to create a `%s` instance.\n%s" % ( | ||||||
|                 cls._class_name, |                 cls._class_name, | ||||||
|                 errors, |                 errors, | ||||||
|   | |||||||
| @@ -3643,6 +3643,32 @@ class TestInstance(MongoDBTestCase): | |||||||
|         with pytest.raises(DuplicateKeyError): |         with pytest.raises(DuplicateKeyError): | ||||||
|             User.objects().select_related() |             User.objects().select_related() | ||||||
|  |  | ||||||
|  |     def test_embedded_document_failed_while_loading_instance_when_it_is_not_a_dict( | ||||||
|  |         self | ||||||
|  |     ): | ||||||
|  |         class LightSaber(EmbeddedDocument): | ||||||
|  |             color = StringField() | ||||||
|  |  | ||||||
|  |         class Jedi(Document): | ||||||
|  |             light_saber = EmbeddedDocumentField(LightSaber) | ||||||
|  |  | ||||||
|  |         coll = Jedi._get_collection() | ||||||
|  |         Jedi(light_saber=LightSaber(color="red")).save() | ||||||
|  |         _ = list(Jedi.objects)  # Ensure a proper document loads without errors | ||||||
|  |  | ||||||
|  |         # Forces a document with a wrong shape (may occur in case of migration) | ||||||
|  |         value = u"I_should_be_a_dict" | ||||||
|  |         coll.insert_one({"light_saber": value}) | ||||||
|  |  | ||||||
|  |         with self.assertRaises(InvalidDocumentError) as cm: | ||||||
|  |             list(Jedi.objects) | ||||||
|  |  | ||||||
|  |         self.assertEqual( | ||||||
|  |             str(cm.exception), | ||||||
|  |             "Invalid data to create a `Jedi` instance.\nField 'light_saber' - The source SON object needs to be of type 'dict' but a '%s' was found" | ||||||
|  |             % type(value), | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |  | ||||||
| class ObjectKeyTestCase(MongoDBTestCase): | class ObjectKeyTestCase(MongoDBTestCase): | ||||||
|     def test_object_key_simple_document(self): |     def test_object_key_simple_document(self): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user