Made minor optimizations to admin_ordering tests

This commit is contained in:
Claude Paroz 2013-11-21 20:57:58 +01:00
parent 2b4bed6dbd
commit e6d5f7ae49
1 changed files with 30 additions and 36 deletions

View File

@ -1,9 +1,9 @@
from __future__ import unicode_literals
from django.test import TestCase, RequestFactory
from django.contrib import admin
from django.contrib.admin.options import ModelAdmin
from django.contrib.auth.models import User
from django.test import TestCase, RequestFactory
from .models import (Band, Song, SongInlineDefaultOrdering,
SongInlineNewOrdering, DynOrderingBandAdmin)
@ -30,12 +30,11 @@ class TestAdminOrdering(TestCase):
def setUp(self):
self.request_factory = RequestFactory()
b1 = Band(name='Aerosmith', bio='', rank=3)
b1.save()
b2 = Band(name='Radiohead', bio='', rank=1)
b2.save()
b3 = Band(name='Van Halen', bio='', rank=2)
b3.save()
Band.objects.bulk_create([
Band(name='Aerosmith', bio='', rank=3),
Band(name='Radiohead', bio='', rank=1),
Band(name='Van Halen', bio='', rank=2),
])
def test_default_ordering(self):
"""
@ -44,7 +43,7 @@ class TestAdminOrdering(TestCase):
"""
ma = ModelAdmin(Band, None)
names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
self.assertListEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
def test_specified_ordering(self):
"""
@ -55,7 +54,7 @@ class TestAdminOrdering(TestCase):
ordering = ('rank',) # default ordering is ('name',)
ma = BandAdmin(Band, None)
names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
self.assertListEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
def test_dynamic_ordering(self):
"""
@ -67,10 +66,10 @@ class TestAdminOrdering(TestCase):
request.user = super_user
ma = DynOrderingBandAdmin(Band, None)
names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
self.assertListEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
request.user = other_user
names = [b.name for b in ma.get_queryset(request)]
self.assertEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
self.assertListEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
class TestInlineModelAdminOrdering(TestCase):
@ -80,40 +79,35 @@ class TestInlineModelAdminOrdering(TestCase):
"""
def setUp(self):
b = Band(name='Aerosmith', bio='', rank=3)
b.save()
self.b = b
s1 = Song(band=b, name='Pink', duration=235)
s1.save()
s2 = Song(band=b, name='Dude (Looks Like a Lady)', duration=264)
s2.save()
s3 = Song(band=b, name='Jaded', duration=214)
s3.save()
self.band = Band.objects.create(name='Aerosmith', bio='', rank=3)
Song.objects.bulk_create([
Song(band=self.band, name='Pink', duration=235),
Song(band=self.band, name='Dude (Looks Like a Lady)', duration=264),
Song(band=self.band, name='Jaded', duration=214),
])
def test_default_ordering(self):
"""
The default ordering should be by name, as specified in the inner Meta
class.
"""
inline = SongInlineDefaultOrdering(self.b, None)
inline = SongInlineDefaultOrdering(self.band, None)
names = [s.name for s in inline.get_queryset(request)]
self.assertEqual(['Dude (Looks Like a Lady)', 'Jaded', 'Pink'], names)
self.assertListEqual(['Dude (Looks Like a Lady)', 'Jaded', 'Pink'], names)
def test_specified_ordering(self):
"""
Let's check with ordering set to something different than the default.
"""
inline = SongInlineNewOrdering(self.b, None)
inline = SongInlineNewOrdering(self.band, None)
names = [s.name for s in inline.get_queryset(request)]
self.assertEqual(['Jaded', 'Pink', 'Dude (Looks Like a Lady)'], names)
self.assertListEqual(['Jaded', 'Pink', 'Dude (Looks Like a Lady)'], names)
class TestRelatedFieldsAdminOrdering(TestCase):
def setUp(self):
self.b1 = Band(name='Pink Floyd', bio='', rank=1)
self.b1.save()
self.b2 = Band(name='Foo Fighters', bio='', rank=5)
self.b2.save()
self.b1 = Band.objects.create(name='Pink Floyd', bio='', rank=1)
self.b2 = Band.objects.create(name='Foo Fighters', bio='', rank=5)
# we need to register a custom ModelAdmin (instead of just using
# ModelAdmin) because the field creator tries to find the ModelAdmin
@ -122,12 +116,17 @@ class TestRelatedFieldsAdminOrdering(TestCase):
pass
admin.site.register(Song, SongAdmin)
def tearDown(self):
admin.site.unregister(Song)
if Band in admin.site._registry:
admin.site.unregister(Band)
def check_ordering_of_field_choices(self, correct_ordering):
fk_field = admin.site._registry[Song].formfield_for_foreignkey(Song.band.field)
m2m_field = admin.site._registry[Song].formfield_for_manytomany(Song.other_interpreters.field)
self.assertEqual(list(fk_field.queryset), correct_ordering)
self.assertEqual(list(m2m_field.queryset), correct_ordering)
self.assertListEqual(list(fk_field.queryset), correct_ordering)
self.assertListEqual(list(m2m_field.queryset), correct_ordering)
def test_no_admin_fallback_to_model_ordering(self):
# should be ordered by name (as defined by the model)
@ -143,13 +142,8 @@ class TestRelatedFieldsAdminOrdering(TestCase):
def test_admin_ordering_beats_model_ordering(self):
class StaticOrderingBandAdmin(admin.ModelAdmin):
ordering = ('rank', )
ordering = ('rank',)
admin.site.register(Band, StaticOrderingBandAdmin)
# should be ordered by rank (defined by the ModelAdmin)
self.check_ordering_of_field_choices([self.b1, self.b2])
def tearDown(self):
admin.site.unregister(Song)
if Band in admin.site._registry:
admin.site.unregister(Band)