Fixed #15545 -- Corrected the admin filterspecs tests to be non-dependent on PK allocation or model ordering. Thanks to Łukasz Rekucki for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15741 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2011-03-03 23:49:44 +00:00
parent 4e76f0f793
commit d05bb1384a
1 changed files with 25 additions and 21 deletions

View File

@ -8,19 +8,22 @@ from django.utils.encoding import force_unicode
from models import Book
def select_by(dictlist, key, value):
return [x for x in dictlist if x[key] == value][0]
class FilterSpecsTests(TestCase):
def setUp(self):
# Users
alfred = User.objects.create_user('alfred', 'alfred@example.com')
bob = User.objects.create_user('bob', 'alfred@example.com')
self.alfred = User.objects.create_user('alfred', 'alfred@example.com')
self.bob = User.objects.create_user('bob', 'bob@example.com')
lisa = User.objects.create_user('lisa', 'lisa@example.com')
#Books
bio_book = Book.objects.create(title='Django: a biography', year=1999, author=alfred)
django_book = Book.objects.create(title='The Django Book', year=None, author=bob)
self.bio_book = Book.objects.create(title='Django: a biography', year=1999, author=self.alfred)
self.django_book = Book.objects.create(title='The Django Book', year=None, author=self.bob)
gipsy_book = Book.objects.create(title='Gipsy guitar for dummies', year=2002)
gipsy_book.contributors = [bob, lisa]
gipsy_book.contributors = [self.bob, lisa]
gipsy_book.save()
self.request_factory = RequestFactory()
@ -75,15 +78,16 @@ class FilterSpecsTests(TestCase):
self.assertEqual(choices[-1]['selected'], True)
self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')
request = self.request_factory.get('/', {'author__id__exact': '1'})
request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk})
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct choice is selected
filterspec = changelist.get_filters(request)[0][1]
self.assertEqual(force_unicode(filterspec.title()), u'author')
choices = list(filterspec.choices(changelist))
self.assertEqual(choices[1]['selected'], True)
self.assertEqual(choices[1]['query_string'], '?author__id__exact=1')
# order of choices depends on User model, which has no order
choice = select_by(filterspec.choices(changelist), "display", "alfred")
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?author__id__exact=%d' % self.alfred.pk)
def test_RelatedFilterSpec_ManyToMany(self):
modeladmin = BookAdmin(Book, admin.site)
@ -101,15 +105,15 @@ class FilterSpecsTests(TestCase):
self.assertEqual(choices[-1]['selected'], True)
self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')
request = self.request_factory.get('/', {'contributors__id__exact': '2'})
request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk})
changelist = self.get_changelist(request, Book, modeladmin)
# Make sure the correct choice is selected
filterspec = changelist.get_filters(request)[0][2]
self.assertEqual(force_unicode(filterspec.title()), u'user')
choices = list(filterspec.choices(changelist))
self.assertEqual(choices[2]['selected'], True)
self.assertEqual(choices[2]['query_string'], '?contributors__id__exact=2')
choice = select_by(filterspec.choices(changelist), "display", "bob")
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk)
def test_RelatedFilterSpec_reverse_relationships(self):
@ -129,15 +133,15 @@ class FilterSpecsTests(TestCase):
self.assertEqual(choices[-1]['selected'], True)
self.assertEqual(choices[-1]['query_string'], '?books_authored__isnull=True')
request = self.request_factory.get('/', {'books_authored__id__exact': '1'})
request = self.request_factory.get('/', {'books_authored__id__exact': self.bio_book.pk})
changelist = self.get_changelist(request, User, modeladmin)
# Make sure the correct choice is selected
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_unicode(filterspec.title()), u'book')
choices = list(filterspec.choices(changelist))
self.assertEqual(choices[1]['selected'], True)
self.assertEqual(choices[1]['query_string'], '?books_authored__id__exact=1')
choice = select_by(filterspec.choices(changelist), "display", self.bio_book.title)
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?books_authored__id__exact=%d' % self.bio_book.pk)
# M2M relationship -----
request = self.request_factory.get('/', {'books_contributed__isnull': 'True'})
@ -153,15 +157,15 @@ class FilterSpecsTests(TestCase):
self.assertEqual(choices[-1]['selected'], True)
self.assertEqual(choices[-1]['query_string'], '?books_contributed__isnull=True')
request = self.request_factory.get('/', {'books_contributed__id__exact': '2'})
request = self.request_factory.get('/', {'books_contributed__id__exact': self.django_book.pk})
changelist = self.get_changelist(request, User, modeladmin)
# Make sure the correct choice is selected
filterspec = changelist.get_filters(request)[0][1]
self.assertEqual(force_unicode(filterspec.title()), u'book')
choices = list(filterspec.choices(changelist))
self.assertEqual(choices[2]['selected'], True)
self.assertEqual(choices[2]['query_string'], '?books_contributed__id__exact=2')
choice = select_by(filterspec.choices(changelist), "display", self.django_book.title)
self.assertEqual(choice['selected'], True)
self.assertEqual(choice['query_string'], '?books_contributed__id__exact=%d' % self.django_book.pk)
class CustomUserAdmin(UserAdmin):
list_filter = ('books_authored', 'books_contributed')