Skip to content

Commit ca90d15

Browse files
committed
Migrated the custom_managers tests. Thanks to Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13774 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent b7f6004 commit ca90d15

File tree

2 files changed

+71
-48
lines changed

2 files changed

+71
-48
lines changed

tests/modeltests/custom_managers/models.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -57,51 +57,3 @@ class Car(models.Model):
5757

5858
def __unicode__(self):
5959
return self.name
60-
61-
__test__ = {'API_TESTS':"""
62-
>>> p1 = Person(first_name='Bugs', last_name='Bunny', fun=True)
63-
>>> p1.save()
64-
>>> p2 = Person(first_name='Droopy', last_name='Dog', fun=False)
65-
>>> p2.save()
66-
>>> Person.objects.get_fun_people()
67-
[<Person: Bugs Bunny>]
68-
69-
# The RelatedManager used on the 'books' descriptor extends the default manager
70-
>>> from modeltests.custom_managers.models import PublishedBookManager
71-
>>> isinstance(p2.books, PublishedBookManager)
72-
True
73-
74-
>>> b1 = Book(title='How to program', author='Rodney Dangerfield', is_published=True)
75-
>>> b1.save()
76-
>>> b2 = Book(title='How to be smart', author='Albert Einstein', is_published=False)
77-
>>> b2.save()
78-
79-
# The default manager, "objects", doesn't exist,
80-
# because a custom one was provided.
81-
>>> Book.objects
82-
Traceback (most recent call last):
83-
...
84-
AttributeError: type object 'Book' has no attribute 'objects'
85-
86-
# The RelatedManager used on the 'authors' descriptor extends the default manager
87-
>>> from modeltests.custom_managers.models import PersonManager
88-
>>> isinstance(b2.authors, PersonManager)
89-
True
90-
91-
>>> Book.published_objects.all()
92-
[<Book: How to program>]
93-
94-
>>> c1 = Car(name='Corvette', mileage=21, top_speed=180)
95-
>>> c1.save()
96-
>>> c2 = Car(name='Neon', mileage=31, top_speed=100)
97-
>>> c2.save()
98-
>>> Car.cars.order_by('name')
99-
[<Car: Corvette>, <Car: Neon>]
100-
>>> Car.fast_cars.all()
101-
[<Car: Corvette>]
102-
103-
# Each model class gets a "_default_manager" attribute, which is a reference
104-
# to the first manager defined in the class. In this case, it's "cars".
105-
>>> Car._default_manager.order_by('name')
106-
[<Car: Corvette>, <Car: Neon>]
107-
"""}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from django.test import TestCase
2+
3+
from models import Person, Book, Car, PersonManager, PublishedBookManager
4+
5+
6+
class CustomManagerTests(TestCase):
7+
def test_manager(self):
8+
p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True)
9+
p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False)
10+
11+
self.assertQuerysetEqual(
12+
Person.objects.get_fun_people(), [
13+
"Bugs Bunny"
14+
],
15+
unicode
16+
)
17+
# The RelatedManager used on the 'books' descriptor extends the default
18+
# manager
19+
self.assertTrue(isinstance(p2.books, PublishedBookManager))
20+
21+
b1 = Book.published_objects.create(
22+
title="How to program", author="Rodney Dangerfield", is_published=True
23+
)
24+
b2 = Book.published_objects.create(
25+
title="How to be smart", author="Albert Einstein", is_published=False
26+
)
27+
28+
# The default manager, "objects", doesn't exist, because a custom one
29+
# was provided.
30+
self.assertRaises(AttributeError, lambda: Book.objects)
31+
32+
# The RelatedManager used on the 'authors' descriptor extends the
33+
# default manager
34+
self.assertTrue(isinstance(b2.authors, PersonManager))
35+
36+
self.assertQuerysetEqual(
37+
Book.published_objects.all(), [
38+
"How to program",
39+
],
40+
lambda b: b.title
41+
)
42+
43+
c1 = Car.cars.create(name="Corvette", mileage=21, top_speed=180)
44+
c2 = Car.cars.create(name="Neon", mileage=31, top_speed=100)
45+
46+
self.assertQuerysetEqual(
47+
Car.cars.order_by("name"), [
48+
"Corvette",
49+
"Neon",
50+
],
51+
lambda c: c.name
52+
)
53+
54+
self.assertQuerysetEqual(
55+
Car.fast_cars.all(), [
56+
"Corvette",
57+
],
58+
lambda c: c.name
59+
)
60+
61+
# Each model class gets a "_default_manager" attribute, which is a
62+
# reference to the first manager defined in the class. In this case,
63+
# it's "cars".
64+
65+
self.assertQuerysetEqual(
66+
Car._default_manager.order_by("name"), [
67+
"Corvette",
68+
"Neon",
69+
],
70+
lambda c: c.name
71+
)

0 commit comments

Comments
 (0)