Updated tests to stop leaking models in shared AdminSite.

This would break upcoming changes and AdminSite assumptions about
having an app_config for each application that has registered
models.
This commit is contained in:
Riccardo Magliocchetti 2015-03-30 18:33:26 +02:00 committed by Tim Graham
parent 97bb48d9ba
commit a429a502ea
11 changed files with 50 additions and 46 deletions

View File

@ -77,6 +77,7 @@ class CarAdmin(admin.ModelAdmin):
request, obj, post_url_continue=reverse('admin:admin_custom_urls_car_history', args=[obj.pk]))
admin.site.register(Action, ActionAdmin)
admin.site.register(Person, PersonAdmin)
admin.site.register(Car, CarAdmin)
site = admin.AdminSite(name='admin_custom_urls')
site.register(Action, ActionAdmin)
site.register(Person, PersonAdmin)
site.register(Car, CarAdmin)

View File

@ -50,7 +50,7 @@ class AdminCustomUrlsTest(TestCase):
"""
Ensure GET on the add_view works.
"""
add_url = reverse('admin:admin_custom_urls_action_add')
add_url = reverse('admin_custom_urls:admin_custom_urls_action_add')
self.assertTrue(add_url.endswith('/!add/'))
response = self.client.get(add_url)
self.assertIsInstance(response, TemplateResponse)
@ -61,7 +61,7 @@ class AdminCustomUrlsTest(TestCase):
Ensure GET on the add_view plus specifying a field value in the query
string works.
"""
response = self.client.get(reverse('admin:admin_custom_urls_action_add'), {'name': 'My Action'})
response = self.client.get(reverse('admin_custom_urls:admin_custom_urls_action_add'), {'name': 'My Action'})
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'value="My Action"')
@ -74,7 +74,7 @@ class AdminCustomUrlsTest(TestCase):
"name": 'Action added through a popup',
"description": "Description of added action",
}
response = self.client.post(reverse('admin:admin_custom_urls_action_add'), post_data)
response = self.client.post(reverse('admin_custom_urls:admin_custom_urls_action_add'), post_data)
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'dismissAddRelatedObjectPopup')
self.assertContains(response, 'Action added through a popup')
@ -85,7 +85,7 @@ class AdminCustomUrlsTest(TestCase):
"""
# Should get the change_view for model instance with PK 'add', not show
# the add_view
url = reverse('admin:%s_action_change' % Action._meta.app_label,
url = reverse('admin_custom_urls:%s_action_change' % Action._meta.app_label,
args=(quote('add'),))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
@ -93,7 +93,7 @@ class AdminCustomUrlsTest(TestCase):
# Should correctly get the change_view for the model instance with the
# funny-looking PK (the one with a 'path/to/html/document.html' value)
url = reverse('admin:%s_action_change' % Action._meta.app_label,
url = reverse('admin_custom_urls:%s_action_change' % Action._meta.app_label,
args=(quote("path/to/html/document.html"),))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
@ -110,11 +110,11 @@ class AdminCustomUrlsTest(TestCase):
post_data = {'name': 'John Doe'}
self.assertEqual(Person.objects.count(), 0)
response = self.client.post(
reverse('admin:admin_custom_urls_person_add'), post_data)
reverse('admin_custom_urls:admin_custom_urls_person_add'), post_data)
persons = Person.objects.all()
self.assertEqual(len(persons), 1)
self.assertRedirects(
response, reverse('admin:admin_custom_urls_person_history', args=[persons[0].pk]))
response, reverse('admin_custom_urls:admin_custom_urls_person_history', args=[persons[0].pk]))
def test_post_save_change_redirect(self):
"""
@ -128,9 +128,9 @@ class AdminCustomUrlsTest(TestCase):
person = Person.objects.all()[0]
post_data = {'name': 'Jack Doe'}
response = self.client.post(
reverse('admin:admin_custom_urls_person_change', args=[person.pk]), post_data)
reverse('admin_custom_urls:admin_custom_urls_person_change', args=[person.pk]), post_data)
self.assertRedirects(
response, reverse('admin:admin_custom_urls_person_delete', args=[person.pk]))
response, reverse('admin_custom_urls:admin_custom_urls_person_delete', args=[person.pk]))
def test_post_url_continue(self):
"""
@ -140,8 +140,8 @@ class AdminCustomUrlsTest(TestCase):
post_data = {'name': 'SuperFast', '_continue': '1'}
self.assertEqual(Car.objects.count(), 0)
response = self.client.post(
reverse('admin:admin_custom_urls_car_add'), post_data)
reverse('admin_custom_urls:admin_custom_urls_car_add'), post_data)
cars = Car.objects.all()
self.assertEqual(len(cars), 1)
self.assertRedirects(
response, reverse('admin:admin_custom_urls_car_history', args=[cars[0].pk]))
response, reverse('admin_custom_urls:admin_custom_urls_car_history', args=[cars[0].pk]))

View File

@ -1,6 +1,7 @@
from django.conf.urls import include, url
from django.contrib import admin
from .models import site
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(site.urls)),
]

View File

@ -16,4 +16,5 @@ class City(models.Model):
def __str__(self):
return self.name
admin.site.register(City, admin.OSMGeoAdmin)
site = admin.AdminSite(name='admin_gis')
site.register(City, admin.OSMGeoAdmin)

View File

@ -8,7 +8,7 @@ if HAS_GEOS:
from django.contrib.gis.geos import Point
from .admin import UnmodifiableAdmin
from .models import City
from .models import site, City
@skipUnlessDBFeature("gis_enabled")
@ -16,14 +16,14 @@ if HAS_GEOS:
class GeoAdminTest(TestCase):
def test_ensure_geographic_media(self):
geoadmin = admin.site._registry[City]
geoadmin = site._registry[City]
admin_js = geoadmin.media.render_js()
self.assertTrue(any(geoadmin.openlayers_url in js for js in admin_js))
def test_olmap_OSM_rendering(self):
delete_all_btn = """<a href="javascript:geodjango_point.clearFeatures()">Delete all Features</a>"""
original_geoadmin = admin.site._registry[City]
original_geoadmin = site._registry[City]
params = original_geoadmin.get_map_widget(City._meta.get_field('point')).params
result = original_geoadmin.get_map_widget(City._meta.get_field('point'))(
).render('point', Point(-79.460734, 40.18476), params)
@ -33,21 +33,21 @@ class GeoAdminTest(TestCase):
self.assertIn(delete_all_btn, result)
admin.site.unregister(City)
admin.site.register(City, UnmodifiableAdmin)
site.unregister(City)
site.register(City, UnmodifiableAdmin)
try:
geoadmin = admin.site._registry[City]
geoadmin = site._registry[City]
params = geoadmin.get_map_widget(City._meta.get_field('point')).params
result = geoadmin.get_map_widget(City._meta.get_field('point'))(
).render('point', Point(-79.460734, 40.18476), params)
self.assertNotIn(delete_all_btn, result)
finally:
admin.site.unregister(City)
admin.site.register(City, original_geoadmin.__class__)
site.unregister(City)
site.register(City, original_geoadmin.__class__)
def test_olmap_WMS_rendering(self):
geoadmin = admin.GeoModelAdmin(City, admin.site)
geoadmin = admin.GeoModelAdmin(City, site)
result = geoadmin.get_map_widget(City._meta.get_field('point'))(
).render('point', Point(-79.460734, 40.18476))
self.assertIn(
@ -59,7 +59,7 @@ class GeoAdminTest(TestCase):
"""
Check that changes are accurately noticed by OpenLayersWidget.
"""
geoadmin = admin.site._registry[City]
geoadmin = site._registry[City]
form = geoadmin.get_changelist_form(None)()
has_changed = form.fields['point'].has_changed

View File

@ -2,5 +2,6 @@ from django.contrib import admin
from .models import ProxyTrackerUser, TrackerUser
admin.site.register(TrackerUser)
admin.site.register(ProxyTrackerUser)
site = admin.AdminSite(name='admin_proxy')
site.register(TrackerUser)
site.register(ProxyTrackerUser)

View File

@ -433,17 +433,17 @@ class ProxyModelAdminTests(TestCase):
proxy = ProxyTrackerUser.objects.get(name='Django Pony')
user_str = 'Tracker user: <a href="%s">%s</a>' % (
reverse('admin:proxy_models_trackeruser_change', args=(user.pk,)), user
reverse('admin_proxy:proxy_models_trackeruser_change', args=(user.pk,)), user
)
proxy_str = 'Proxy tracker user: <a href="%s">%s</a>' % (
reverse('admin:proxy_models_proxytrackeruser_change', args=(proxy.pk,)), proxy
reverse('admin_proxy:proxy_models_proxytrackeruser_change', args=(proxy.pk,)), proxy
)
self.client.login(username='super', password='secret')
response = self.client.get(reverse('admin:proxy_models_trackeruser_delete', args=(user.pk,)))
response = self.client.get(reverse('admin_proxy:proxy_models_trackeruser_delete', args=(user.pk,)))
delete_str = response.context['deleted_objects'][0]
self.assertEqual(delete_str, user_str)
response = self.client.get(reverse('admin:proxy_models_proxytrackeruser_delete', args=(proxy.pk,)))
response = self.client.get(reverse('admin_proxy:proxy_models_proxytrackeruser_delete', args=(proxy.pk,)))
delete_str = response.context['deleted_objects'][0]
self.assertEqual(delete_str, proxy_str)
self.client.logout()

View File

@ -1,6 +1,7 @@
from django.conf.urls import include, url
from django.contrib import admin
from .admin import site
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(site.urls)),
]

View File

@ -6,10 +6,10 @@ from .models import Event, Timestamp
class EventAdmin(admin.ModelAdmin):
list_display = ('dt',)
admin.site.register(Event, EventAdmin)
class TimestampAdmin(admin.ModelAdmin):
readonly_fields = ('created', 'updated')
admin.site.register(Timestamp, TimestampAdmin)
site = admin.AdminSite(name='admin_tz')
site.register(Event, EventAdmin)
site.register(Timestamp, TimestampAdmin)

View File

@ -1100,26 +1100,26 @@ class AdminTests(TestCase):
@requires_tz_support
def test_changelist(self):
e = Event.objects.create(dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
response = self.client.get(reverse('admin:timezones_event_changelist'))
response = self.client.get(reverse('admin_tz:timezones_event_changelist'))
self.assertContains(response, e.dt.astimezone(EAT).isoformat())
def test_changelist_in_other_timezone(self):
e = Event.objects.create(dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
with timezone.override(ICT):
response = self.client.get(reverse('admin:timezones_event_changelist'))
response = self.client.get(reverse('admin_tz:timezones_event_changelist'))
self.assertContains(response, e.dt.astimezone(ICT).isoformat())
@requires_tz_support
def test_change_editable(self):
e = Event.objects.create(dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
response = self.client.get(reverse('admin:timezones_event_change', args=(e.pk,)))
response = self.client.get(reverse('admin_tz:timezones_event_change', args=(e.pk,)))
self.assertContains(response, e.dt.astimezone(EAT).date().isoformat())
self.assertContains(response, e.dt.astimezone(EAT).time().isoformat())
def test_change_editable_in_other_timezone(self):
e = Event.objects.create(dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
with timezone.override(ICT):
response = self.client.get(reverse('admin:timezones_event_change', args=(e.pk,)))
response = self.client.get(reverse('admin_tz:timezones_event_change', args=(e.pk,)))
self.assertContains(response, e.dt.astimezone(ICT).date().isoformat())
self.assertContains(response, e.dt.astimezone(ICT).time().isoformat())
@ -1128,7 +1128,7 @@ class AdminTests(TestCase):
Timestamp.objects.create()
# re-fetch the object for backends that lose microseconds (MySQL)
t = Timestamp.objects.get()
response = self.client.get(reverse('admin:timezones_timestamp_change', args=(t.pk,)))
response = self.client.get(reverse('admin_tz:timezones_timestamp_change', args=(t.pk,)))
self.assertContains(response, t.created.astimezone(EAT).isoformat())
def test_change_readonly_in_other_timezone(self):
@ -1136,5 +1136,5 @@ class AdminTests(TestCase):
# re-fetch the object for backends that lose microseconds (MySQL)
t = Timestamp.objects.get()
with timezone.override(ICT):
response = self.client.get(reverse('admin:timezones_timestamp_change', args=(t.pk,)))
response = self.client.get(reverse('admin_tz:timezones_timestamp_change', args=(t.pk,)))
self.assertContains(response, t.created.astimezone(ICT).isoformat())

View File

@ -1,8 +1,7 @@
from django.conf.urls import include, url
from django.contrib import admin
from . import admin as tz_admin # NOQA: register tz_admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(tz_admin.site.urls)),
]