From 91dad4060f437037ad35e3892b1953bdfc2d0128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20W=C3=B3jcik?= Date: Wed, 28 Dec 2016 00:51:47 -0500 Subject: [PATCH] raise an error when trying to save an abstract document (#1449) --- mongoengine/document.py | 4 +++- tests/document/instance.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index e86a45d9..0fa2460d 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -313,6 +313,9 @@ class Document(BaseDocument): .. versionchanged:: 0.10.7 Add signal_kwargs argument """ + if self._meta.get('abstract'): + raise InvalidDocumentError('Cannot save an abstract document.') + signal_kwargs = signal_kwargs or {} signals.pre_save.send(self.__class__, document=self, **signal_kwargs) @@ -828,7 +831,6 @@ class Document(BaseDocument): """ Lists all of the indexes that should be created for given collection. It includes all the indexes from super- and sub-classes. """ - if cls._meta.get('abstract'): return [] diff --git a/tests/document/instance.py b/tests/document/instance.py index d961f034..b187f766 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -435,6 +435,15 @@ class InstanceTest(unittest.TestCase): person.to_dbref() + def test_save_abstract_document(self): + """Saving an abstract document should fail.""" + class Doc(Document): + name = StringField() + meta = {'abstract': True} + + with self.assertRaises(InvalidDocumentError): + Doc(name='aaa').save() + def test_reload(self): """Ensure that attributes may be reloaded. """