Made _cls etc optional, merged sort to order_by
This commit is contained in:
@@ -126,6 +126,36 @@ class DocumentTest(unittest.TestCase):
|
||||
self.assertEqual(Employee._meta['collection'],
|
||||
self.Person._meta['collection'])
|
||||
|
||||
def test_allow_inheritance(self):
|
||||
"""Ensure that inheritance may be disabled on simple classes and that
|
||||
_cls and _types will not be used.
|
||||
"""
|
||||
class Animal(Document):
|
||||
meta = {'allow_inheritance': False}
|
||||
name = StringField()
|
||||
|
||||
Animal.drop_collection()
|
||||
|
||||
def create_dog_class():
|
||||
class Dog(Animal):
|
||||
pass
|
||||
self.assertRaises(ValueError, create_dog_class)
|
||||
|
||||
# Check that _cls etc aren't present on simple documents
|
||||
dog = Animal(name='dog')
|
||||
dog.save()
|
||||
collection = self.db[Animal._meta['collection']]
|
||||
obj = collection.find_one()
|
||||
self.assertFalse('_cls' in obj)
|
||||
self.assertFalse('_types' in obj)
|
||||
|
||||
Animal.drop_collection()
|
||||
|
||||
def create_employee_class():
|
||||
class Employee(self.Person):
|
||||
meta = {'allow_inheritance': False}
|
||||
self.assertRaises(ValueError, create_employee_class)
|
||||
|
||||
def test_creation(self):
|
||||
"""Ensure that document may be created using keyword arguments.
|
||||
"""
|
||||
|
||||
@@ -137,23 +137,23 @@ class QuerySetTest(unittest.TestCase):
|
||||
self.Person.objects.delete()
|
||||
self.assertEqual(self.Person.objects.count(), 0)
|
||||
|
||||
def test_sort(self):
|
||||
"""Ensure that QuerySets may be sorted.
|
||||
def test_order_by(self):
|
||||
"""Ensure that QuerySets may be ordered.
|
||||
"""
|
||||
self.Person(name="User A", age=20).save()
|
||||
self.Person(name="User B", age=40).save()
|
||||
self.Person(name="User C", age=30).save()
|
||||
|
||||
names = [p.name for p in self.Person.objects.sort('-age')]
|
||||
names = [p.name for p in self.Person.objects.order_by('-age')]
|
||||
self.assertEqual(names, ['User B', 'User C', 'User A'])
|
||||
|
||||
names = [p.name for p in self.Person.objects.sort('+age')]
|
||||
names = [p.name for p in self.Person.objects.order_by('+age')]
|
||||
self.assertEqual(names, ['User A', 'User C', 'User B'])
|
||||
|
||||
names = [p.name for p in self.Person.objects.sort('age')]
|
||||
names = [p.name for p in self.Person.objects.order_by('age')]
|
||||
self.assertEqual(names, ['User A', 'User C', 'User B'])
|
||||
|
||||
ages = [p.age for p in self.Person.objects.sort('-name')]
|
||||
ages = [p.age for p in self.Person.objects.order_by('-name')]
|
||||
self.assertEqual(ages, [30, 40, 20])
|
||||
|
||||
def tearDown(self):
|
||||
|
||||
Reference in New Issue
Block a user