added test for abstract document without pk creation and adapted behaviour

This commit is contained in:
mrigal 2015-04-16 14:33:16 +02:00 committed by Matthieu Rigal
parent 8909d1d144
commit 1862bcf867
3 changed files with 14 additions and 1 deletions

View File

@ -184,7 +184,7 @@ class BaseDocument(object):
self__initialised = False
# Check if the user has created a new instance of a class
if (self._is_document and self__initialised
and self__created and name == self._meta['id_field']):
and self__created and name == self._meta.get('id_field')):
super(BaseDocument, self).__setattr__('_created', False)
super(BaseDocument, self).__setattr__(name, value)

View File

@ -152,6 +152,8 @@ class Document(BaseDocument):
"""
def fget(self):
if not 'id_field' in self._meta:
return None
return getattr(self, self._meta['id_field'])
def fset(self, value):

View File

@ -307,6 +307,17 @@ class InheritanceTest(unittest.TestCase):
doc = Animal(name='dog')
self.assertFalse('_cls' in doc.to_mongo())
def test_abstract_document_creation_does_not_fail(self):
class City(Document):
continent = StringField()
meta = {'abstract': True,
'allow_inheritance': False}
bkk = City(continent='asia')
self.assertEqual(None, bkk.pk)
# TODO: expected error? Shouldn'twe created a new error type
self.assertRaises(KeyError, lambda: setattr(bkk, 'pk', 1))
def test_allow_inheritance_embedded_document(self):
"""Ensure embedded documents respect inheritance
"""