From 57a38282a9a583983c6625d3e81731f9b461786f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20G=C3=A9rard?= Date: Mon, 18 Feb 2019 22:03:03 +0100 Subject: [PATCH] Add DeprecationWarning for EmbeddedDocument.save & .reload - those will be removed soon --- docs/changelog.rst | 1 + mongoengine/document.py | 6 ++++++ tests/document/instance.py | 24 +++++++++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 25e1a585..dbd328d8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -8,6 +8,7 @@ Development - Fix .only() working improperly after using .count() of the same instance of QuerySet - POTENTIAL BREAKING CHANGE: All result fields are now passed, including internal fields (_cls, _id) when using `QuerySet.as_pymongo` #1976 - Fix InvalidStringData error when using modify on a BinaryField #1127 +- DEPRECATION: `EmbeddedDocument.save` & `.reload` are marked as deprecated and will be removed in a next version of mongoengine #1552 ================= Changes in 0.16.3 diff --git a/mongoengine/document.py b/mongoengine/document.py index 7a491b7d..57364ae6 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -90,9 +90,15 @@ class EmbeddedDocument(six.with_metaclass(DocumentMetaclass, BaseDocument)): return data def save(self, *args, **kwargs): + warnings.warn("EmbeddedDocument.save is deprecated and will be removed in a next version of mongoengine." + "Use the parent document's .save() or ._instance.save()", + DeprecationWarning, stacklevel=2) self._instance.save(*args, **kwargs) def reload(self, *args, **kwargs): + warnings.warn("EmbeddedDocument.reload is deprecated and will be removed in a next version of mongoengine." + "Use the parent document's .reload() or ._instance.reload()", + DeprecationWarning, stacklevel=2) self._instance.reload(*args, **kwargs) diff --git a/tests/document/instance.py b/tests/document/instance.py index 5319ace4..39e47524 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -7,6 +7,8 @@ import uuid import weakref from datetime import datetime + +import warnings from bson import DBRef, ObjectId from pymongo.errors import DuplicateKeyError @@ -1482,7 +1484,7 @@ class InstanceTest(MongoDBTestCase): Message.drop_collection() # All objects share the same id, but each in a different collection - user = User(id=1, name='user-name')#.save() + user = User(id=1, name='user-name') # .save() message = Message(id=1, author=user).save() message.author.name = 'tutu' @@ -2000,7 +2002,6 @@ class InstanceTest(MongoDBTestCase): child_record.delete() self.assertEqual(Record.objects(name='parent').get().children, []) - def test_reverse_delete_rule_with_custom_id_field(self): """Ensure that a referenced document with custom primary key is also deleted upon deletion. @@ -3086,6 +3087,24 @@ class InstanceTest(MongoDBTestCase): "UNDEFINED", system.nodes["node"].parameters["param"].macros["test"].value) + def test_embedded_document_save_reload_warning(self): + """Relates to #1570""" + class Embedded(EmbeddedDocument): + pass + + class Doc(Document): + emb = EmbeddedDocumentField(Embedded) + + doc = Doc(emb=Embedded()).save() + doc.emb.save() # Make sure its still working + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) + with self.assertRaises(DeprecationWarning): + doc.emb.save() + + with self.assertRaises(DeprecationWarning): + doc.emb.reload() + def test_embedded_document_equality(self): class Test(Document): field = StringField(required=True) @@ -3381,7 +3400,6 @@ class InstanceTest(MongoDBTestCase): class User(Document): company = ReferenceField(Company) - # Ensure index creation exception aren't swallowed (#1688) with self.assertRaises(DuplicateKeyError): User.objects().select_related()