Kaydet (Commit) adf5d75a authored tarafından Riccardo Magliocchetti's avatar Riccardo Magliocchetti Kaydeden (comit) Tim Graham

Refs #24553 -- Isolated admin_* tests.

This fixes a regression with runtests.py --reverse after
bd53db5e

We need to avoid leaking model registration in the default AdminSite.
üst 4157c502
This diff is collapsed.
......@@ -25,6 +25,8 @@ class MockSuperUser(object):
request = MockRequest()
request.user = MockSuperUser()
site = admin.AdminSite()
class TestAdminOrdering(TestCase):
"""
......@@ -46,7 +48,7 @@ class TestAdminOrdering(TestCase):
The default ordering should be by name, as specified in the inner Meta
class.
"""
ma = ModelAdmin(Band, admin.site)
ma = ModelAdmin(Band, site)
names = [b.name for b in ma.get_queryset(request)]
self.assertListEqual(['Aerosmith', 'Radiohead', 'Van Halen'], names)
......@@ -57,7 +59,7 @@ class TestAdminOrdering(TestCase):
"""
class BandAdmin(ModelAdmin):
ordering = ('rank',) # default ordering is ('name',)
ma = BandAdmin(Band, admin.site)
ma = BandAdmin(Band, site)
names = [b.name for b in ma.get_queryset(request)]
self.assertListEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
......@@ -69,7 +71,7 @@ class TestAdminOrdering(TestCase):
other_user = User.objects.create(username='other')
request = self.request_factory.get('/')
request.user = super_user
ma = DynOrderingBandAdmin(Band, admin.site)
ma = DynOrderingBandAdmin(Band, site)
names = [b.name for b in ma.get_queryset(request)]
self.assertListEqual(['Radiohead', 'Van Halen', 'Aerosmith'], names)
request.user = other_user
......@@ -96,7 +98,7 @@ class TestInlineModelAdminOrdering(TestCase):
The default ordering should be by name, as specified in the inner Meta
class.
"""
inline = SongInlineDefaultOrdering(self.band, admin.site)
inline = SongInlineDefaultOrdering(self.band, site)
names = [s.name for s in inline.get_queryset(request)]
self.assertListEqual(['Dude (Looks Like a Lady)', 'Jaded', 'Pink'], names)
......@@ -104,7 +106,7 @@ class TestInlineModelAdminOrdering(TestCase):
"""
Let's check with ordering set to something different than the default.
"""
inline = SongInlineNewOrdering(self.band, admin.site)
inline = SongInlineNewOrdering(self.band, site)
names = [s.name for s in inline.get_queryset(request)]
self.assertListEqual(['Jaded', 'Pink', 'Dude (Looks Like a Lady)'], names)
......@@ -119,16 +121,16 @@ class TestRelatedFieldsAdminOrdering(TestCase):
# for the related model
class SongAdmin(admin.ModelAdmin):
pass
admin.site.register(Song, SongAdmin)
site.register(Song, SongAdmin)
def tearDown(self):
admin.site.unregister(Song)
if Band in admin.site._registry:
admin.site.unregister(Band)
site.unregister(Song)
if Band in site._registry:
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)
fk_field = site._registry[Song].formfield_for_foreignkey(Song.band.field)
m2m_field = site._registry[Song].formfield_for_manytomany(Song.other_interpreters.field)
self.assertListEqual(list(fk_field.queryset), correct_ordering)
self.assertListEqual(list(m2m_field.queryset), correct_ordering)
......@@ -140,7 +142,7 @@ class TestRelatedFieldsAdminOrdering(TestCase):
def test_admin_with_no_ordering_fallback_to_model_ordering(self):
class NoOrderingBandAdmin(admin.ModelAdmin):
pass
admin.site.register(Band, NoOrderingBandAdmin)
site.register(Band, NoOrderingBandAdmin)
# should be ordered by name (as defined by the model)
self.check_ordering_of_field_choices([self.b2, self.b1])
......@@ -148,7 +150,7 @@ class TestRelatedFieldsAdminOrdering(TestCase):
def test_admin_ordering_beats_model_ordering(self):
class StaticOrderingBandAdmin(admin.ModelAdmin):
ordering = ('rank',)
admin.site.register(Band, StaticOrderingBandAdmin)
site.register(Band, StaticOrderingBandAdmin)
# should be ordered by rank (defined by the ModelAdmin)
self.check_ordering_of_field_choices([self.b1, self.b2])
......@@ -170,8 +172,8 @@ class TestRelatedFieldsAdminOrdering(TestCase):
class StaticOrderingBandAdmin(admin.ModelAdmin):
ordering = ('rank',)
admin.site.unregister(Song)
admin.site.register(Song, SongAdmin)
admin.site.register(Band, StaticOrderingBandAdmin)
site.unregister(Song)
site.register(Song, SongAdmin)
site.register(Band, StaticOrderingBandAdmin)
self.check_ordering_of_field_choices([self.b2])
......@@ -104,6 +104,7 @@ class TestRegistrationDecorator(TestCase):
isinstance(self.default_site._registry[Person],
admin.options.ModelAdmin)
)
self.default_site.unregister(Person)
def test_custom_site_registration(self):
register(Person, site=self.custom_site)(NameAdmin)
......@@ -118,10 +119,12 @@ class TestRegistrationDecorator(TestCase):
isinstance(self.default_site._registry[Traveler],
admin.options.ModelAdmin)
)
self.default_site.unregister(Traveler)
self.assertTrue(
isinstance(self.default_site._registry[Place],
admin.options.ModelAdmin)
)
self.default_site.unregister(Place)
def test_wrapped_class_not_a_model_admin(self):
self.assertRaisesMessage(ValueError, 'Wrapped class must subclass ModelAdmin.',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment