Fixed #22572 -- override_settings(ROOT_URLCONF) didn't clear urlresolvers._urlconfs.

Thanks Anubhav Joshi and Tim Graham for the reviews.
This commit is contained in:
Loic Bistuer 2014-05-04 21:29:49 +07:00
parent 3b7c66a3ac
commit 942556df2f
3 changed files with 34 additions and 16 deletions

View File

@ -129,5 +129,6 @@ def complex_setting_changed(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def root_urlconf_changed(**kwargs): def root_urlconf_changed(**kwargs):
if kwargs['setting'] == 'ROOT_URLCONF': if kwargs['setting'] == 'ROOT_URLCONF':
from django.core.urlresolvers import clear_url_caches from django.core.urlresolvers import clear_url_caches, set_urlconf
clear_url_caches() clear_url_caches()
set_urlconf(None)

View File

@ -4,7 +4,7 @@ from __future__ import unicode_literals
import unittest import unittest
from django.conf.urls import url from django.conf.urls import url
from django.core.urlresolvers import reverse from django.core.urlresolvers import NoReverseMatch, reverse
from django.db import connection from django.db import connection
from django.forms import EmailField, IntegerField from django.forms import EmailField, IntegerField
from django.http import HttpResponse from django.http import HttpResponse
@ -15,6 +15,7 @@ from django.test.utils import CaptureQueriesContext, override_settings
from django.utils import six from django.utils import six
from .models import Person from .models import Person
from .views import empty_response
class SkippingTestCase(TestCase): class SkippingTestCase(TestCase):
@ -667,30 +668,42 @@ class AssertFieldOutputTests(SimpleTestCase):
self.assertFieldOutput(MyCustomField, {}, {}, empty_value=None) self.assertFieldOutput(MyCustomField, {}, {}, empty_value=None)
# for OverrideSettingsTests
def fake_view(request):
pass
class FirstUrls: class FirstUrls:
urlpatterns = [url(r'first/$', fake_view, name='first')] urlpatterns = [url(r'first/$', empty_response, name='first')]
class SecondUrls: class SecondUrls:
urlpatterns = [url(r'second/$', fake_view, name='second')] urlpatterns = [url(r'second/$', empty_response, name='second')]
class OverrideSettingsTests(TestCase): class OverrideSettingsTests(TestCase):
"""
#21518 -- If neither override_settings nor a settings_changed receiver # #21518 -- If neither override_settings nor a settings_changed receiver
clears the URL cache between tests, then one of these two test methods will # clears the URL cache between tests, then one of test_first or
fail. # test_second will fail.
"""
@override_settings(ROOT_URLCONF=FirstUrls) @override_settings(ROOT_URLCONF=FirstUrls)
def test_first(self): def test_urlconf_first(self):
reverse('first') reverse('first')
@override_settings(ROOT_URLCONF=SecondUrls) @override_settings(ROOT_URLCONF=SecondUrls)
def test_second(self): def test_urlconf_second(self):
reverse('second') reverse('second')
def test_urlconf_cache(self):
self.assertRaises(NoReverseMatch, lambda: reverse('first'))
self.assertRaises(NoReverseMatch, lambda: reverse('second'))
with override_settings(ROOT_URLCONF=FirstUrls):
self.client.get(reverse('first'))
self.assertRaises(NoReverseMatch, lambda: reverse('second'))
with override_settings(ROOT_URLCONF=SecondUrls):
self.assertRaises(NoReverseMatch, lambda: reverse('first'))
self.client.get(reverse('second'))
self.client.get(reverse('first'))
self.assertRaises(NoReverseMatch, lambda: reverse('second'))
self.assertRaises(NoReverseMatch, lambda: reverse('first'))
self.assertRaises(NoReverseMatch, lambda: reverse('second'))

View File

@ -13,3 +13,7 @@ def get_person(request, pk):
def no_template_used(request): def no_template_used(request):
template = loader.get_template_from_string("This is a string-based template") template = loader.get_template_from_string("This is a string-based template")
return HttpResponse(template.render(Context({}))) return HttpResponse(template.render(Context({})))
def empty_response(request):
return HttpResponse('')