Skip to content

Commit 101796c

Browse files
committed
[soc2010/test-refactor] Converted custom_columns doctest to unittest.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/test-refactor@13346 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 75302ff commit 101796c

File tree

3 files changed

+82
-66
lines changed

3 files changed

+82
-66
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"pk": 2,
4+
"model": "custom_columns.author",
5+
"fields": {
6+
"first_name": "Peter",
7+
"last_name": "Jones"
8+
}
9+
},
10+
{
11+
"pk": 1,
12+
"model": "custom_columns.author",
13+
"fields": {
14+
"first_name": "John",
15+
"last_name": "Smith"
16+
}
17+
},
18+
{
19+
"pk": 1,
20+
"model": "custom_columns.article",
21+
"fields": {
22+
"headline": "Django lets you build web apps easily",
23+
"authors": [
24+
2,
25+
1
26+
]
27+
}
28+
}
29+
]

tests/modeltests/custom_columns/models.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,69 +37,3 @@ def __unicode__(self):
3737

3838
class Meta:
3939
ordering = ('headline',)
40-
41-
__test__ = {'API_TESTS':"""
42-
# Create a Author.
43-
>>> a = Author(first_name='John', last_name='Smith')
44-
>>> a.save()
45-
46-
>>> a.id
47-
1
48-
49-
# Create another author
50-
>>> a2 = Author(first_name='Peter', last_name='Jones')
51-
>>> a2.save()
52-
53-
# Create an article
54-
>>> art = Article(headline='Django lets you build web apps easily')
55-
>>> art.save()
56-
>>> art.authors = [a, a2]
57-
58-
# Although the table and column names on Author have been set to custom values,
59-
# nothing about using the Author model has changed...
60-
61-
# Query the available authors
62-
>>> Author.objects.all()
63-
[<Author: Peter Jones>, <Author: John Smith>]
64-
65-
>>> Author.objects.filter(first_name__exact='John')
66-
[<Author: John Smith>]
67-
68-
>>> Author.objects.get(first_name__exact='John')
69-
<Author: John Smith>
70-
71-
>>> Author.objects.filter(firstname__exact='John')
72-
Traceback (most recent call last):
73-
...
74-
FieldError: Cannot resolve keyword 'firstname' into field. Choices are: article, first_name, id, last_name
75-
76-
>>> a = Author.objects.get(last_name__exact='Smith')
77-
>>> a.first_name
78-
u'John'
79-
>>> a.last_name
80-
u'Smith'
81-
>>> a.firstname
82-
Traceback (most recent call last):
83-
...
84-
AttributeError: 'Author' object has no attribute 'firstname'
85-
>>> a.last
86-
Traceback (most recent call last):
87-
...
88-
AttributeError: 'Author' object has no attribute 'last'
89-
90-
# Although the Article table uses a custom m2m table,
91-
# nothing about using the m2m relationship has changed...
92-
93-
# Get all the authors for an article
94-
>>> art.authors.all()
95-
[<Author: Peter Jones>, <Author: John Smith>]
96-
97-
# Get the articles for an author
98-
>>> a.article_set.all()
99-
[<Article: Django lets you build web apps easily>]
100-
101-
# Query the authors across the m2m relation
102-
>>> art.authors.filter(last_name='Jones')
103-
[<Author: Peter Jones>]
104-
105-
"""}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from django.test import TestCase
2+
3+
from django.core.exceptions import FieldError
4+
5+
from models import Author, Article
6+
7+
class CustomColumnsTestCase(TestCase):
8+
fixtures = ['custom_columns_testdata.json']
9+
10+
def test_column_interface(self):
11+
# Although the table and column names on Author have been set to
12+
# custom values, nothing about using the Author model has
13+
# changed...
14+
self.assertEqual(Author.objects.get(first_name='John').id,
15+
1)
16+
17+
# Query the available authors
18+
self.assertQuerysetEqual(Author.objects.all(),
19+
['<Author: Peter Jones>', '<Author: John Smith>'])
20+
self.assertQuerysetEqual(Author.objects.filter(first_name__exact='John'),
21+
['<Author: John Smith>'])
22+
self.assertEqual(repr(Author.objects.get(first_name__exact='John')),
23+
'<Author: John Smith>')
24+
self.assertRaises(FieldError,
25+
Author.objects.filter,
26+
firstname__exact='John')
27+
28+
js = Author.objects.get(last_name__exact='Smith')
29+
30+
self.assertEqual(js.first_name,
31+
u'John')
32+
self.assertEqual(js.last_name,
33+
u'Smith')
34+
self.assertRaises(AttributeError,
35+
getattr,
36+
js, 'firstname')
37+
self.assertRaises(AttributeError,
38+
getattr,
39+
js, 'last')
40+
41+
# Although the Article table uses a custom m2m table,
42+
# nothing about using the m2m relationship has changed...
43+
44+
# Get all the authors for an article
45+
art = Article.objects.get(headline='Django lets you build web apps easily')
46+
self.assertQuerysetEqual(art.authors.all(),
47+
['<Author: Peter Jones>', '<Author: John Smith>'])
48+
# Get the articles for an author
49+
self.assertQuerysetEqual(js.article_set.all(),
50+
['<Article: Django lets you build web apps easily>'])
51+
# Query the authors across the m2m relation
52+
self.assertQuerysetEqual(art.authors.filter(last_name='Jones'),
53+
['<Author: Peter Jones>'])

0 commit comments

Comments
 (0)