| 1 | from django.test import TestCase |
| 2 | from django.test.client import RequestFactory |
| 3 | from django.contrib.auth.models import User |
| 4 | |
| 5 | from django.contrib import admin |
| 6 | from django.contrib.admin.views.main import ChangeList |
| 7 | |
| 8 | from models import Book |
| 9 | |
| 10 | class FilterSpecsTests(TestCase): |
| 11 | |
| 12 | def setUp(self): |
| 13 | # Users |
| 14 | alfred = User.objects.create_user('alfred', 'alfred@example.com') |
| 15 | bob = User.objects.create_user('bob', 'alfred@example.com') |
| 16 | lisa = User.objects.create_user('lisa', 'lisa@example.com') |
| 17 | |
| 18 | #Books |
| 19 | bio_book = Book.objects.create(title='Django: a biography', year=1999, author=alfred) |
| 20 | django_book = Book.objects.create(title='The Django Book', year=None, author=bob) |
| 21 | gipsy_book = Book.objects.create(title='Gipsy guitar for dummies', year=2002) |
| 22 | gipsy_book.contributors = [bob, lisa] |
| 23 | gipsy_book.save() |
| 24 | |
| 25 | self.request_factory = RequestFactory() |
| 26 | |
| 27 | def test_AllValuesFilterSpec(self): |
| 28 | modeladmin = BookAdmin(Book, admin.site) |
| 29 | |
| 30 | request = self.request_factory.get('/', {'year__isnull': 'True'}) |
| 31 | changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links, |
| 32 | modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, |
| 33 | modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin) |
| 34 | |
| 35 | # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters |
| 36 | queryset = changelist.get_query_set() |
| 37 | |
| 38 | # Make sure the last choice is None and is selected |
| 39 | filterspec = changelist.get_filters(request)[0][0] |
| 40 | self.assertEquals(filterspec.title(), u'year') |
| 41 | choices = list(filterspec.choices(changelist)) |
| 42 | self.assertEquals(choices[3]['selected'], True) |
| 43 | self.assertEquals(choices[3]['query_string'], '?year__isnull=True') |
| 44 | |
| 45 | request = self.request_factory.get('/', {'year': '2002'}) |
| 46 | changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links, |
| 47 | modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, |
| 48 | modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin) |
| 49 | |
| 50 | # Make sure the correct choice is selected |
| 51 | filterspec = changelist.get_filters(request)[0][0] |
| 52 | self.assertEquals(filterspec.title(), u'year') |
| 53 | choices = list(filterspec.choices(changelist)) |
| 54 | self.assertEquals(choices[2]['selected'], True) |
| 55 | self.assertEquals(choices[2]['query_string'], '?year=2002') |
| 56 | |
| 57 | def test_RelatedFilterSpec_ForeignKey(self): |
| 58 | modeladmin = BookAdmin(Book, admin.site) |
| 59 | |
| 60 | request = self.request_factory.get('/', {'author__isnull': 'True'}) |
| 61 | changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links, |
| 62 | modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, |
| 63 | modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin) |
| 64 | |
| 65 | # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters |
| 66 | queryset = changelist.get_query_set() |
| 67 | |
| 68 | # Make sure the last choice is None and is selected |
| 69 | filterspec = changelist.get_filters(request)[0][1] |
| 70 | self.assertEquals(filterspec.title(), u'author') |
| 71 | choices = list(filterspec.choices(changelist)) |
| 72 | self.assertEquals(choices[4]['selected'], True) |
| 73 | self.assertEquals(choices[4]['query_string'], '?author__isnull=True') |
| 74 | |
| 75 | request = self.request_factory.get('/', {'author__id__exact': '1'}) |
| 76 | changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links, |
| 77 | modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, |
| 78 | modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin) |
| 79 | |
| 80 | # Make sure the correct choice is selected |
| 81 | filterspec = changelist.get_filters(request)[0][1] |
| 82 | self.assertEquals(filterspec.title(), u'author') |
| 83 | choices = list(filterspec.choices(changelist)) |
| 84 | self.assertEquals(choices[1]['selected'], True) |
| 85 | self.assertEquals(choices[1]['query_string'], '?author__id__exact=1') |
| 86 | |
| 87 | def test_RelatedFilterSpec_ManyToMany(self): |
| 88 | modeladmin = BookAdmin(Book, admin.site) |
| 89 | |
| 90 | request = self.request_factory.get('/', {'contributors__isnull': 'True'}) |
| 91 | changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links, |
| 92 | modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, |
| 93 | modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin) |
| 94 | |
| 95 | # Make sure changelist.get_query_set() does not raise IncorrectLookupParameters |
| 96 | queryset = changelist.get_query_set() |
| 97 | |
| 98 | # Make sure the last choice is None and is selected |
| 99 | filterspec = changelist.get_filters(request)[0][2] |
| 100 | self.assertEquals(filterspec.title(), u'user') |
| 101 | choices = list(filterspec.choices(changelist)) |
| 102 | self.assertEquals(choices[4]['selected'], True) |
| 103 | self.assertEquals(choices[4]['query_string'], '?contributors__isnull=True') |
| 104 | |
| 105 | request = self.request_factory.get('/', {'contributors__id__exact': '2'}) |
| 106 | changelist = ChangeList(request, Book, modeladmin.list_display, modeladmin.list_display_links, |
| 107 | modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, |
| 108 | modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_editable, modeladmin) |
| 109 | |
| 110 | # Make sure the correct choice is selected |
| 111 | filterspec = changelist.get_filters(request)[0][2] |
| 112 | self.assertEquals(filterspec.title(), u'user') |
| 113 | choices = list(filterspec.choices(changelist)) |
| 114 | self.assertEquals(choices[2]['selected'], True) |
| 115 | self.assertEquals(choices[2]['query_string'], '?contributors__id__exact=2') |
| 116 | |
| 117 | class BookAdmin(admin.ModelAdmin): |
| 118 | list_filter = ('year', 'author', 'contributors') |
| 119 | order_by = '-id' |