Added Abstract Base Classes

Thanks to @theojulienne for the code :) #108
This commit is contained in:
Ross Lawley
2011-05-24 12:30:12 +01:00
parent 1126c85903
commit 088c40f9f2
2 changed files with 42 additions and 4 deletions

View File

@@ -29,6 +29,9 @@ class DocumentTest(unittest.TestCase):
age = IntField()
self.Person = Person
def tearDown(self):
self.Person.drop_collection()
def test_drop_collection(self):
"""Ensure that the collection may be dropped from the database.
"""
@@ -188,6 +191,34 @@ class DocumentTest(unittest.TestCase):
self.assertFalse('_cls' in comment.to_mongo())
self.assertFalse('_types' in comment.to_mongo())
def test_abstract_documents(self):
"""Ensure that a document superclass can be marked as abstract
thereby not using it as the name for the collection."""
class Animal(Document):
name = StringField()
meta = {'abstract': True}
class Fish(Animal): pass
class Guppy(Fish): pass
class Mammal(Animal):
meta = {'abstract': True}
class Human(Mammal): pass
self.assertFalse('collection' in Animal._meta)
self.assertFalse('collection' in Mammal._meta)
self.assertEqual(Fish._meta['collection'], 'fish')
self.assertEqual(Guppy._meta['collection'], 'fish')
self.assertEqual(Human._meta['collection'], 'human')
def create_bad_abstract():
class EvilHuman(Human):
evil = BooleanField(default=True)
meta = {'abstract': True}
self.assertRaises(ValueError, create_bad_abstract)
def test_collection_name(self):
"""Ensure that a collection with a specified name may be used.
"""
@@ -907,9 +938,6 @@ class DocumentTest(unittest.TestCase):
A.drop_collection()
B.drop_collection()
def tearDown(self):
self.Person.drop_collection()
def test_document_hash(self):
"""Test document in list, dict, set
"""