Commit cf9d1d4dd52bf3ee4ab05ef1aac6825f6a6b0ec5

added new custom managers tests. Added another assert to clean things up.

Commit diff

django_sqlalchemy/test/asserts.py

 
33from nose import tools
44from nose.tools import *
55
6__all__ = ['assert_instance_of', 'assert_not_instance_of', 'assert_none', 'assert_not_none'] + tools.__all__
6__all__ = ['assert_instance_of', 'assert_not_instance_of',
7 'assert_none', 'assert_not_none',
8 'assert_list_same'] + tools.__all__
79
810def assert_instance_of(expected, actual, msg=None):
911 """Verify that object is an instance of expected """
2121
2222def assert_not_none(actual, msg=None):
2323 """verify that item is None"""
24 assert_not_equal(None, actual, msg)
24 assert_not_equal(None, actual, msg)
25
26def assert_list_same(expected, actual, msg=None):
27 """verify that the list contains the expected"""
28 assert_equal([repr(e) for e in expected],
29 [repr(a) for a in actual])
toggle raw diff

tests/managers/__init__.py

tests/managers/test_custom_managers.py

 
1from django_sqlalchemy.test import *
2from django_sqlalchemy.backend import metadata
3from django.db import models
4
5# An example of a custom manager called "objects".
6
7class PersonManager(models.Manager):
8 def get_fun_people(self):
9 return self.filter(fun=True)
10
11class Person(models.Model):
12 first_name = models.CharField(max_length=30)
13 last_name = models.CharField(max_length=30)
14 fun = models.BooleanField()
15 objects = PersonManager()
16
17 def __unicode__(self):
18 return u"%s %s" % (self.first_name, self.last_name)
19
20# An example of a custom manager that sets get_query_set().
21
22class PublishedBookManager(models.Manager):
23 def get_query_set(self):
24 return super(PublishedBookManager, self).get_query_set().filter(is_published=True)
25
26class Book(models.Model):
27 title = models.CharField(max_length=50)
28 author = models.CharField(max_length=30)
29 is_published = models.BooleanField()
30 published_objects = PublishedBookManager()
31 authors = models.ManyToManyField(Person, related_name='books')
32
33 def __unicode__(self):
34 return self.title
35
36# An example of providing multiple custom managers.
37
38class FastCarManager(models.Manager):
39 def get_query_set(self):
40 return super(FastCarManager, self).get_query_set().filter(top_speed__gt=150)
41
42class Car(models.Model):
43 name = models.CharField(max_length=10)
44 mileage = models.IntegerField()
45 top_speed = models.IntegerField(help_text="In miles per hour.")
46 cars = models.Manager()
47 fast_cars = FastCarManager()
48
49 def __unicode__(self):
50 return self.name
51
52metadata.create_all()
53
54p1 = Person(first_name='Bugs', last_name='Bunny', fun=True)
55p1.save()
56p2 = Person(first_name='Droopy', last_name='Dog', fun=False)
57p2.save()
58
59b1 = Book(title='How to program', author='Rodney Dangerfield', is_published=True)
60b1.save()
61b2 = Book(title='How to be smart', author='Albert Einstein', is_published=False)
62b2.save()
63
64c1 = Car(name='Corvette', mileage=21, top_speed=180)
65c1.save()
66c2 = Car(name='Neon', mileage=31, top_speed=100)
67c2.save()
68
69class TestCustomManager(object):
70 def setup(self):
71 pass
72
73 def test_should_see_custom_manager_method(self):
74 assert_list_same([p1], Person.objects.get_fun_people())
75
76 def test_should_extend_default_manager(self):
77 assert_instance_of(PublishedBookManager, p2.books)
78
79 @raises(AttributeError)
80 def test_should_not_contain_a_default_manager_if_custom_provided(self):
81 Book.objects
82
83 def test_should_extend_default_manager_with_related_manager(self):
84 assert_instance_of(PersonManager, b2.authors)
85
86 def test_should_only_return_published_objects(self):
87 assert_list_same([b1], Book.published_objects.all())
88
89 def test_should_order_by(self):
90 assert_list_same([c1, c2], Car.cars.order_by('name'))
91 assert_list_same([c1], Car.fast_cars.all())
92
93 def test_should_return_default_manager_as_first_manager_in_class(self):
94 assert_list_same([c1, c2], Car._default_manager.order_by('name'))
toggle raw diff

tests/settings.py

 
1515 'apps.news',
1616 'apps.norelations',
1717 'apps.categories',
18 # 'django.contrib.auth',
18 'django.contrib.auth',
1919 )
2020
2121TEMPLATE_LOADERS = (
toggle raw diff