Skip to content

Commit cc4703d

Browse files
committed
[soc2010/test-refactor] Converted custom_managers doctests to unittests.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/test-refactor@13349 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 4ffeb59 commit cc4703d

File tree

3 files changed

+95
-48
lines changed

3 files changed

+95
-48
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[
2+
{
3+
"pk": 1,
4+
"model": "custom_managers.person",
5+
"fields": {
6+
"fun": true,
7+
"first_name": "Bugs",
8+
"last_name": "Bunny"
9+
}
10+
},
11+
{
12+
"pk": 2,
13+
"model": "custom_managers.person",
14+
"fields": {
15+
"fun": false,
16+
"first_name": "Droopy",
17+
"last_name": "Dog"
18+
}
19+
},
20+
{
21+
"pk": 1,
22+
"model": "custom_managers.book",
23+
"fields": {
24+
"authors": [],
25+
"is_published": true,
26+
"author": "Rodney Dangerfield",
27+
"title": "How to program"
28+
}
29+
},
30+
{
31+
"pk": 1,
32+
"model": "custom_managers.car",
33+
"fields": {
34+
"mileage": 21,
35+
"top_speed": 180,
36+
"name": "Corvette"
37+
}
38+
},
39+
{
40+
"pk": 2,
41+
"model": "custom_managers.car",
42+
"fields": {
43+
"mileage": 31,
44+
"top_speed": 100,
45+
"name": "Neon"
46+
}
47+
}
48+
]

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from django.test import TestCase
2+
3+
from models import PersonManager, PublishedBookManager
4+
from models import Person, Book, Car
5+
6+
class CustomManagersTestCase(TestCase):
7+
fixtures = ['custom_managers_testdata.json']
8+
9+
def test_related_manager(self):
10+
self.assertQuerysetEqual(Person.objects.get_fun_people(),
11+
['<Person: Bugs Bunny>'])
12+
13+
14+
# The RelatedManager used on the 'books' descriptor extends
15+
# the default manager
16+
p2 = Person.objects.get(first_name='Droopy')
17+
self.assertTrue(isinstance(p2.books, PublishedBookManager))
18+
19+
20+
# The default manager, "objects", doesn't exist, because a
21+
# custom one was provided.
22+
self.assertRaises(AttributeError,
23+
getattr,
24+
Book, 'objects')
25+
26+
27+
# The RelatedManager used on the 'authors' descriptor extends
28+
# the default manager
29+
b2 = Book(title='How to be smart',
30+
author='Albert Einstein',
31+
is_published=False)
32+
b2.save()
33+
34+
self.assertTrue(isinstance(b2.authors, PersonManager))
35+
self.assertQuerysetEqual(Book.published_objects.all(),
36+
['<Book: How to program>'])
37+
38+
self.assertQuerysetEqual(Car.cars.order_by('name'),
39+
['<Car: Corvette>', '<Car: Neon>'])
40+
self.assertQuerysetEqual(Car.fast_cars.all(),
41+
['<Car: Corvette>'])
42+
43+
# Each model class gets a "_default_manager" attribute, which
44+
# is a reference to the first manager defined in the class. In
45+
# this case, it's "cars".
46+
self.assertQuerysetEqual(Car._default_manager.order_by('name'),
47+
['<Car: Corvette>', '<Car: Neon>'])

0 commit comments

Comments
 (0)