PageRenderTime 53ms CodeModel.GetById 8ms app.highlight 39ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/admin_filterspecs/tests.py

https://code.google.com/p/mango-py/
Python | 211 lines | 138 code | 48 blank | 25 comment | 3 complexity | 231d0f51d371bf511056a5e86f76404e MD5 | raw file
  1from django.contrib.auth.admin import UserAdmin
  2from django.test import TestCase
  3from django.test.client import RequestFactory
  4from django.contrib.auth.models import User
  5from django.contrib import admin
  6from django.contrib.admin.views.main import ChangeList
  7from django.utils.encoding import force_unicode
  8
  9from models import Book, BoolTest
 10
 11def select_by(dictlist, key, value):
 12    return [x for x in dictlist if x[key] == value][0]
 13
 14class FilterSpecsTests(TestCase):
 15
 16    def setUp(self):
 17        # Users
 18        self.alfred = User.objects.create_user('alfred', 'alfred@example.com')
 19        self.bob = User.objects.create_user('bob', 'bob@example.com')
 20        lisa = User.objects.create_user('lisa', 'lisa@example.com')
 21
 22        #Books
 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)
 25        gipsy_book = Book.objects.create(title='Gipsy guitar for dummies', year=2002)
 26        gipsy_book.contributors = [self.bob, lisa]
 27        gipsy_book.save()
 28
 29        # BoolTests
 30        self.trueTest = BoolTest.objects.create(completed=True)
 31        self.falseTest = BoolTest.objects.create(completed=False)
 32
 33        self.request_factory = RequestFactory()
 34
 35
 36    def get_changelist(self, request, model, modeladmin):
 37        return ChangeList(request, model, modeladmin.list_display, modeladmin.list_display_links,
 38            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
 39            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
 40
 41    def test_AllValuesFilterSpec(self):
 42        modeladmin = BookAdmin(Book, admin.site)
 43
 44        request = self.request_factory.get('/', {'year__isnull': 'True'})
 45        changelist = self.get_changelist(request, Book, modeladmin)
 46
 47        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
 48        queryset = changelist.get_query_set()
 49
 50        # Make sure the last choice is None and is selected
 51        filterspec = changelist.get_filters(request)[0][0]
 52        self.assertEqual(force_unicode(filterspec.title()), u'year')
 53        choices = list(filterspec.choices(changelist))
 54        self.assertEqual(choices[-1]['selected'], True)
 55        self.assertEqual(choices[-1]['query_string'], '?year__isnull=True')
 56
 57        request = self.request_factory.get('/', {'year': '2002'})
 58        changelist = self.get_changelist(request, Book, modeladmin)
 59
 60        # Make sure the correct choice is selected
 61        filterspec = changelist.get_filters(request)[0][0]
 62        self.assertEqual(force_unicode(filterspec.title()), u'year')
 63        choices = list(filterspec.choices(changelist))
 64        self.assertEqual(choices[2]['selected'], True)
 65        self.assertEqual(choices[2]['query_string'], '?year=2002')
 66
 67    def test_RelatedFilterSpec_ForeignKey(self):
 68        modeladmin = BookAdmin(Book, admin.site)
 69
 70        request = self.request_factory.get('/', {'author__isnull': 'True'})
 71        changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links,
 72            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
 73            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
 74
 75        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
 76        queryset = changelist.get_query_set()
 77
 78        # Make sure the last choice is None and is selected
 79        filterspec = changelist.get_filters(request)[0][1]
 80        self.assertEqual(force_unicode(filterspec.title()), u'author')
 81        choices = list(filterspec.choices(changelist))
 82        self.assertEqual(choices[-1]['selected'], True)
 83        self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')
 84
 85        request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk})
 86        changelist = self.get_changelist(request, Book, modeladmin)
 87
 88        # Make sure the correct choice is selected
 89        filterspec = changelist.get_filters(request)[0][1]
 90        self.assertEqual(force_unicode(filterspec.title()), u'author')
 91        # order of choices depends on User model, which has no order
 92        choice = select_by(filterspec.choices(changelist), "display", "alfred")
 93        self.assertEqual(choice['selected'], True)
 94        self.assertEqual(choice['query_string'], '?author__id__exact=%d' % self.alfred.pk)
 95
 96    def test_RelatedFilterSpec_ManyToMany(self):
 97        modeladmin = BookAdmin(Book, admin.site)
 98
 99        request = self.request_factory.get('/', {'contributors__isnull': 'True'})
100        changelist = self.get_changelist(request, Book, modeladmin)
101
102        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
103        queryset = changelist.get_query_set()
104
105        # Make sure the last choice is None and is selected
106        filterspec = changelist.get_filters(request)[0][2]
107        self.assertEqual(force_unicode(filterspec.title()), u'user')
108        choices = list(filterspec.choices(changelist))
109        self.assertEqual(choices[-1]['selected'], True)
110        self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')
111
112        request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk})
113        changelist = self.get_changelist(request, Book, modeladmin)
114
115        # Make sure the correct choice is selected
116        filterspec = changelist.get_filters(request)[0][2]
117        self.assertEqual(force_unicode(filterspec.title()), u'user')
118        choice = select_by(filterspec.choices(changelist), "display", "bob")
119        self.assertEqual(choice['selected'], True)
120        self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk)
121
122
123    def test_RelatedFilterSpec_reverse_relationships(self):
124        modeladmin = CustomUserAdmin(User, admin.site)
125
126        # FK relationship -----
127        request = self.request_factory.get('/', {'books_authored__isnull': 'True'})
128        changelist = self.get_changelist(request, User, modeladmin)
129
130        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
131        queryset = changelist.get_query_set()
132
133        # Make sure the last choice is None and is selected
134        filterspec = changelist.get_filters(request)[0][0]
135        self.assertEqual(force_unicode(filterspec.title()), u'book')
136        choices = list(filterspec.choices(changelist))
137        self.assertEqual(choices[-1]['selected'], True)
138        self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True')
139
140        request = self.request_factory.get('/', {'books_authored__id__exact': self.bio_book.pk})
141        changelist = self.get_changelist(request, User, modeladmin)
142
143        # Make sure the correct choice is selected
144        filterspec = changelist.get_filters(request)[0][0]
145        self.assertEqual(force_unicode(filterspec.title()), u'book')
146        choice = select_by(filterspec.choices(changelist), "display", self.bio_book.title)
147        self.assertEqual(choice['selected'], True)
148        self.assertEqual(choice['query_string'], '?books_authored__id__exact=%d' % self.bio_book.pk)
149
150        # M2M relationship -----
151        request = self.request_factory.get('/', {'books_contributed__isnull': 'True'})
152        changelist = self.get_changelist(request, User, modeladmin)
153
154        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
155        queryset = changelist.get_query_set()
156
157        # Make sure the last choice is None and is selected
158        filterspec = changelist.get_filters(request)[0][1]
159        self.assertEqual(force_unicode(filterspec.title()), u'book')
160        choices = list(filterspec.choices(changelist))
161        self.assertEqual(choices[-1]['selected'], True)
162        self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True')
163
164        request = self.request_factory.get('/', {'books_contributed__id__exact': self.django_book.pk})
165        changelist = self.get_changelist(request, User, modeladmin)
166
167        # Make sure the correct choice is selected
168        filterspec = changelist.get_filters(request)[0][1]
169        self.assertEqual(force_unicode(filterspec.title()), u'book')
170        choice = select_by(filterspec.choices(changelist), "display", self.django_book.title)
171        self.assertEqual(choice['selected'], True)
172        self.assertEqual(choice['query_string'], '?books_contributed__id__exact=%d' % self.django_book.pk)
173
174    def test_BooleanFilterSpec(self):
175        modeladmin = BoolTestAdmin(BoolTest, admin.site)
176
177        request = self.request_factory.get('/')
178        changelist = ChangeList(request, BoolTest, modeladmin.list_display, modeladmin.list_display_links,
179            modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields,
180            modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin)
181
182        # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters
183        queryset = changelist.get_query_set()
184
185        # Make sure the last choice is None and is selected
186        filterspec = changelist.get_filters(request)[0][0]
187        self.assertEqual(force_unicode(filterspec.title()), u'completed')
188        choices = list(filterspec.choices(changelist))
189        self.assertEqual(choices[-1]['selected'], False)
190        self.assertEqual(choices[-1]['query_string'], '?completed__exact=0')
191
192        request = self.request_factory.get('/', {'completed__exact': 1})
193        changelist = self.get_changelist(request, BoolTest, modeladmin)
194
195        # Make sure the correct choice is selected
196        filterspec = changelist.get_filters(request)[0][0]
197        self.assertEqual(force_unicode(filterspec.title()), u'completed')
198        # order of choices depends on User model, which has no order
199        choice = select_by(filterspec.choices(changelist), "display", "Yes")
200        self.assertEqual(choice['selected'], True)
201        self.assertEqual(choice['query_string'], '?completed__exact=1')
202
203class CustomUserAdmin(UserAdmin):
204    list_filter = ('books_authored', 'books_contributed')
205
206class BookAdmin(admin.ModelAdmin):
207    list_filter = ('year', 'author', 'contributors')
208    order_by = '-id'
209
210class BoolTestAdmin(admin.ModelAdmin):
211    list_filter = ('completed',)