Ticket #15545: ticket15545.diff

File ticket15545.diff, 6.1 KB (added by Łukasz Rekucki, 14 years ago)
  • tests/regressiontests/admin_filterspecs/tests.py

    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  
    88
    99from models import Book
    1010
     11def select_by(dictlist, key, value):
     12    return [x for x in dictlist if x[key] == value][0]
     13
    1114class FilterSpecsTests(TestCase):
    1215
    1316    def setUp(self):
    1417        # 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')
    1720        lisa = User.objects.create_user('lisa', 'lisa@example.com')
    1821
    1922        #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)
    2225        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]
    2427        gipsy_book.save()
    2528
    2629        self.request_factory = RequestFactory()
    class FilterSpecsTests(TestCase):  
    7578        self.assertEqual(choices[-1]['selected'], True)
    7679        self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')
    7780
    78         request = self.request_factory.get('/', {'author__id__exact': '1'})
     81        request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk})
    7982        changelist = self.get_changelist(request, Book, modeladmin)
    8083
    8184        # Make sure the correct choice is selected
    8285        filterspec = changelist.get_filters(request)[0][1]
    8386        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)
    8791
    8892    def test_RelatedFilterSpec_ManyToMany(self):
    8993        modeladmin = BookAdmin(Book, admin.site)
    class FilterSpecsTests(TestCase):  
    101105        self.assertEqual(choices[-1]['selected'], True)
    102106        self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')
    103107
    104         request = self.request_factory.get('/', {'contributors__id__exact': '2'})
     108        request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk})
    105109        changelist = self.get_changelist(request, Book, modeladmin)
    106110
    107111        # Make sure the correct choice is selected
    108112        filterspec = changelist.get_filters(request)[0][2]
    109113        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)
    113117
    114118
    115119    def test_RelatedFilterSpec_reverse_relationships(self):
    class FilterSpecsTests(TestCase):  
    129133        self.assertEqual(choices[-1]['selected'], True)
    130134        self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True')
    131135
    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})
    133137        changelist = self.get_changelist(request, User, modeladmin)
    134138
    135139        # Make sure the correct choice is selected
    136140        filterspec = changelist.get_filters(request)[0][0]
    137141        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)
    141145
    142146        # M2M relationship -----
    143147        request = self.request_factory.get('/', {'books_contributed__isnull': 'True'})
    class FilterSpecsTests(TestCase):  
    153157        self.assertEqual(choices[-1]['selected'], True)
    154158        self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True')
    155159
    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})
    157161        changelist = self.get_changelist(request, User, modeladmin)
    158162
    159163        # Make sure the correct choice is selected
    160164        filterspec = changelist.get_filters(request)[0][1]
    161165        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)
    165169
    166170class CustomUserAdmin(UserAdmin):
    167171    list_filter = ('books_authored', 'books_contributed')
Back to Top