diff --git a/tests/regressiontests/admin_filterspecs/tests.py b/tests/regressiontests/admin_filterspecs/tests.py
index b4b3056..698a4eb 100644
a
|
b
|
from django.utils.encoding import force_unicode
|
8 | 8 | |
9 | 9 | from models import Book |
10 | 10 | |
| 11 | def select_by(dictlist, key, value): |
| 12 | return [x for x in dictlist if x[key] == value][0] |
| 13 | |
11 | 14 | class FilterSpecsTests(TestCase): |
12 | 15 | |
13 | 16 | def setUp(self): |
14 | 17 | # Users |
15 | | alfred = User.objects.create_user('alfred', 'alfred@example.com') |
16 | | bob = User.objects.create_user('bob', 'alfred@example.com') |
| 18 | self.alfred = User.objects.create_user('alfred', 'alfred@example.com') |
| 19 | self.bob = User.objects.create_user('bob', 'bob@example.com') |
17 | 20 | lisa = User.objects.create_user('lisa', 'lisa@example.com') |
18 | 21 | |
19 | 22 | #Books |
20 | | bio_book = Book.objects.create(title='Django: a biography', year=1999, author=alfred) |
21 | | django_book = Book.objects.create(title='The Django Book', year=None, author=bob) |
| 23 | self.bio_book = Book.objects.create(title='Django: a biography', year=1999, author=self.alfred) |
| 24 | self.django_book = Book.objects.create(title='The Django Book', year=None, author=self.bob) |
22 | 25 | gipsy_book = Book.objects.create(title='Gipsy guitar for dummies', year=2002) |
23 | | gipsy_book.contributors = [bob, lisa] |
| 26 | gipsy_book.contributors = [self.bob, lisa] |
24 | 27 | gipsy_book.save() |
25 | 28 | |
26 | 29 | self.request_factory = RequestFactory() |
… |
… |
class FilterSpecsTests(TestCase):
|
75 | 78 | self.assertEqual(choices[-1]['selected'], True) |
76 | 79 | self.assertEqual(choices[-1]['query_string'], '?author__isnull=True') |
77 | 80 | |
78 | | request = self.request_factory.get('/', {'author__id__exact': '1'}) |
| 81 | request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk}) |
79 | 82 | changelist = self.get_changelist(request, Book, modeladmin) |
80 | 83 | |
81 | 84 | # Make sure the correct choice is selected |
82 | 85 | filterspec = changelist.get_filters(request)[0][1] |
83 | 86 | self.assertEqual(force_unicode(filterspec.title()), u'author') |
84 | | choices = list(filterspec.choices(changelist)) |
85 | | self.assertEqual(choices[1]['selected'], True) |
86 | | self.assertEqual(choices[1]['query_string'], '?author__id__exact=1') |
| 87 | # order of choices depends on User model, which has no order |
| 88 | choice = select_by(filterspec.choices(changelist), "display", "alfred") |
| 89 | self.assertEqual(choice['selected'], True) |
| 90 | self.assertEqual(choice['query_string'], '?author__id__exact=%d' % self.alfred.pk) |
87 | 91 | |
88 | 92 | def test_RelatedFilterSpec_ManyToMany(self): |
89 | 93 | modeladmin = BookAdmin(Book, admin.site) |
… |
… |
class FilterSpecsTests(TestCase):
|
101 | 105 | self.assertEqual(choices[-1]['selected'], True) |
102 | 106 | self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True') |
103 | 107 | |
104 | | request = self.request_factory.get('/', {'contributors__id__exact': '2'}) |
| 108 | request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk}) |
105 | 109 | changelist = self.get_changelist(request, Book, modeladmin) |
106 | 110 | |
107 | 111 | # Make sure the correct choice is selected |
108 | 112 | filterspec = changelist.get_filters(request)[0][2] |
109 | 113 | self.assertEqual(force_unicode(filterspec.title()), u'user') |
110 | | choices = list(filterspec.choices(changelist)) |
111 | | self.assertEqual(choices[2]['selected'], True) |
112 | | self.assertEqual(choices[2]['query_string'], '?contributors__id__exact=2') |
| 114 | choice = select_by(filterspec.choices(changelist), "display", "bob") |
| 115 | self.assertEqual(choice['selected'], True) |
| 116 | self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk) |
113 | 117 | |
114 | 118 | |
115 | 119 | def test_RelatedFilterSpec_reverse_relationships(self): |
… |
… |
class FilterSpecsTests(TestCase):
|
129 | 133 | self.assertEqual(choices[-1]['selected'], True) |
130 | 134 | self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True') |
131 | 135 | |
132 | | request = self.request_factory.get('/', {'books_authored__id__exact': '1'}) |
| 136 | request = self.request_factory.get('/', {'books_authored__id__exact': self.bio_book.pk}) |
133 | 137 | changelist = self.get_changelist(request, User, modeladmin) |
134 | 138 | |
135 | 139 | # Make sure the correct choice is selected |
136 | 140 | filterspec = changelist.get_filters(request)[0][0] |
137 | 141 | self.assertEqual(force_unicode(filterspec.title()), u'book') |
138 | | choices = list(filterspec.choices(changelist)) |
139 | | self.assertEqual(choices[1]['selected'], True) |
140 | | self.assertEqual(choices[1]['query_string'], '?books_authored__id__exact=1') |
| 142 | choice = select_by(filterspec.choices(changelist), "display", self.bio_book.title) |
| 143 | self.assertEqual(choice['selected'], True) |
| 144 | self.assertEqual(choice['query_string'], '?books_authored__id__exact=%d' % self.bio_book.pk) |
141 | 145 | |
142 | 146 | # M2M relationship ----- |
143 | 147 | request = self.request_factory.get('/', {'books_contributed__isnull': 'True'}) |
… |
… |
class FilterSpecsTests(TestCase):
|
153 | 157 | self.assertEqual(choices[-1]['selected'], True) |
154 | 158 | self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True') |
155 | 159 | |
156 | | request = self.request_factory.get('/', {'books_contributed__id__exact': '2'}) |
| 160 | request = self.request_factory.get('/', {'books_contributed__id__exact': self.django_book.pk}) |
157 | 161 | changelist = self.get_changelist(request, User, modeladmin) |
158 | 162 | |
159 | 163 | # Make sure the correct choice is selected |
160 | 164 | filterspec = changelist.get_filters(request)[0][1] |
161 | 165 | self.assertEqual(force_unicode(filterspec.title()), u'book') |
162 | | choices = list(filterspec.choices(changelist)) |
163 | | self.assertEqual(choices[2]['selected'], True) |
164 | | self.assertEqual(choices[2]['query_string'], '?books_contributed__id__exact=2') |
| 166 | choice = select_by(filterspec.choices(changelist), "display", self.django_book.title) |
| 167 | self.assertEqual(choice['selected'], True) |
| 168 | self.assertEqual(choice['query_string'], '?books_contributed__id__exact=%d' % self.django_book.pk) |
165 | 169 | |
166 | 170 | class CustomUserAdmin(UserAdmin): |
167 | 171 | list_filter = ('books_authored', 'books_contributed') |