Ticket #11077: test_combine_urlconfs.diff
File test_combine_urlconfs.diff, 8.7 KB (added by , 15 years ago) |
---|
-
django/test/testcases.py
1 1 import re 2 import sys 3 import imp 2 4 import unittest 3 5 from urlparse import urlsplit, urlunsplit 4 6 from xml.dom.minidom import parseString, Node … … 13 15 from django.test.client import Client 14 16 from django.utils import simplejson 15 17 from django.utils.encoding import smart_str 18 from django.utils.importlib import import_module 16 19 17 20 try: 18 21 all … … 210 213 transaction.rollback_unless_managed(using=conn) 211 214 212 215 class TransactionTestCase(unittest.TestCase): 216 # This can be modified to bypass what the settings are when running a test. 217 combine_urls = False 218 combine_urls_with = None 219 213 220 def _pre_setup(self): 214 221 """Performs any pre-test setup. This includes: 215 222 … … 242 249 def _urlconf_setup(self): 243 250 if hasattr(self, 'urls'): 244 251 self._old_root_urlconf = settings.ROOT_URLCONF 245 settings.ROOT_URLCONF = self.urls 252 if settings.TEST_COMBINE_URLCONFS or self.combine_urls: 253 # Put the module instance of self.urls into specified_urlconf. 254 if not isinstance(self.urls, basestring): 255 specified_urlconf = self.urls 256 else: 257 specified_urlconf = import_module(self.urls) 258 # Choose between settings.TEST_COMBINE_URLCONFS_WITH and 259 # settings.ROOT_URLCONF. 260 urls_to_combine = self.combine_urls_with or \ 261 settings.TEST_COMBINE_URLCONFS_WITH or settings.ROOT_URLCONF 262 # Do the above action, this time with urls_to_combine. 263 if not isinstance(urls_to_combine, basestring): 264 extra_urlconf = urls_to_combine 265 else: 266 extra_urlconf = import_module(urls_to_combine) 267 # Create a dynamic URLconf module that combines the urlpatterns. 268 # of specified_urlconf and extra_urlconf 269 dynamic_urlconf_name = 'dynamic_urls' 270 dynamic_urlconf = imp.new_module(dynamic_urlconf_name) 271 dynamic_urlconf.urlpatterns = specified_urlconf.urlpatterns + \ 272 extra_urlconf.urlpatterns 273 # Also combine handler404 and 500, favoring specified_urlconf 274 handler404 = getattr(specified_urlconf, 'handler404', None) or \ 275 getattr(extra_urlconf, 'handler404', None) 276 if handler404: 277 dynamic_urlconf.handler404 = handler404 278 handler500 = getattr(specified_urlconf, 'handler500', None) or \ 279 getattr(extra_urlconf, 'handler500', None) 280 if handler500: 281 dynamic_urlconf.handler500 = handler500 282 # Add our dynamic_urlconf to sys.modules and set 283 # settings.ROOT_URLCONF to it. 284 sys.modules[dynamic_urlconf_name] = dynamic_urlconf 285 settings.ROOT_URLCONF = dynamic_urlconf 286 else: 287 settings.ROOT_URLCONF = self.urls 246 288 clear_url_caches() 247 289 248 290 def __call__(self, result=None): -
django/conf/global_settings.py
506 506 TEST_DATABASE_CHARSET = None 507 507 TEST_DATABASE_COLLATION = None 508 508 509 # If true, the testing framework will combine the URLconf instead of overwrite 510 # it if a TestCase.urls is specified 511 TEST_COMBINE_URLCONFS = False 512 513 # The URLconf to be combined with the TestCase.urls setting. If None, the 514 # ROOT_URLCONF specified in settings.py will be used instead. 515 TEST_COMBINE_URLCONFS_WITH = None 516 509 517 ############ 510 518 # FIXTURES # 511 519 ############ -
tests/regressiontests/test_client_regress/combine_urls.py
1 from django.conf.urls.defaults import * 2 import views 3 4 urlpatterns = patterns('', 5 url(r'^custom_arg_view/(?P<name>.+)/$', views.view_with_argument, name='custom_arg_view'), 6 ) -
tests/regressiontests/test_client_regress/models.py
583 583 url = reverse('arg_view', args=['somename']) 584 584 self.assertEquals(url, '/arg_view/somename/') 585 585 586 class UrlconfCombinationTests(TestCase): 587 urls = 'regressiontests.test_client_regress.combine_urls' 588 combine_urls = True 589 590 def test_urlconf_was_changed(self): 591 "Should have access to urls from combine_urls and ROOT_URLCONF" 592 url = reverse('arg_view', args=['somename']) 593 self.assertEquals(url, '/test_client_regress/arg_view/somename/') 594 url = reverse('custom_arg_view', args=['somename']) 595 self.assertEquals(url, '/custom_arg_view/somename/') 596 597 class UrlconfCombinationWithTests(TestCase): 598 urls = 'regressiontests.test_client_regress.combine_urls' 599 combine_urls_with = 'regressiontests.test_client_regress.urls' 600 combine_urls = True 601 602 def test_urlconf_was_changed(self): 603 "Should have access to test_client_regress.combine_urls and test_client_regress.urls" 604 url = reverse('arg_view', args=['somename']) 605 self.assertEquals(url, '/arg_view/somename/') 606 url = reverse('custom_arg_view', args=['somename']) 607 self.assertEquals(url, '/custom_arg_view/somename/') 608 586 609 # This test needs to run *after* UrlconfSubstitutionTests; the zz prefix in the 587 610 # name is to ensure alphabetical ordering. 588 611 class zzUrlconfSubstitutionTests(TestCase): -
docs/topics/testing.txt
1054 1054 This test case will use the contents of ``myapp.test_urls`` as the 1055 1055 URLconf for the duration of the test case. 1056 1056 1057 .. versionadded:: 1.2 1058 1059 The use of ``TestCase.urls`` has a downside, though. Since it overrides 1060 ``ROOT_URLCONF``, your project's other urls will not be accessible. This is 1061 normally not a problem, but custom templates with reverses to external apps will 1062 cause the tests to raise ``NoReverseMatch`` exceptions. This problem can happen 1063 with :mod:`django.contrib.auth` and other apps that support custom templates and 1064 set ``TestCase.urls``. Custom templates that include a ``base.html`` with links to 1065 various places around the site are common sources of this problem. 1066 1067 This situation can be fixed by combining the ``TestCase.urls`` with your 1068 project's ``ROOT_URLCONF``. You can also choose to combine a custom URLconf 1069 designed specifically for testing. See :setting:`TEST_COMBINE_URLCONFS` and 1070 :setting:`TEST_COMBINE_URLCONFS_WITH` for more information. 1071 1057 1072 .. _emptying-test-outbox: 1058 1073 1059 1074 Multi-database support -
docs/ref/settings.txt
1420 1420 Output, as a string, that the template system should use for invalid (e.g. 1421 1421 misspelled) variables. See :ref:`invalid-template-variables`.. 1422 1422 1423 .. setting:: TEST_COMBINE_URLCONFS 1424 1425 TEST_COMBINE_URLCONFS 1426 --------------------- 1427 1428 .. versionadded:: 1.2 1429 1430 Default: ``False`` 1431 1432 If ``True``, the testing framework will combine the URLconf instead of overwrite 1433 it if a ``TestCase.urls`` is specified. The URLconf merged into the 1434 ``TestCase.urls`` will be :setting:`ROOT_URLCONF` unless 1435 :setting:`TEST_COMBINE_URLCONFS_WITH` is set. 1436 1437 This setting is useful for cases where apps like :mod:`django.contrib.auth` set 1438 ``TestCase.urls`` to override the :setting:`ROOT_URLCONF` with a custom testing 1439 URLconf. This setting will fix the problem where custom templates with reverses 1440 to external apps cause the tests to fail with ``NoReverseMatch`` errors. 1441 1442 .. setting:: TEST_COMBINE_URLCONFS_WITH 1443 1444 TEST_COMBINE_URLCONFS_WITH 1445 -------------------------- 1446 1447 .. versionadded:: 1.2 1448 1449 Default: ``None`` 1450 1451 The URLconf to be combined with the ``TestCase.urls`` setting if it is set. If 1452 ``None``, the :setting:`ROOT_URLCONF` will be used instead. This setting will 1453 only be used if :setting:`TEST_COMBINE_URLCONFS` is ``True``. This can either be 1454 a string pointing to a module or an actual module, much like 1455 :setting:`ROOT_URLCONF`. 1456 1423 1457 .. setting:: TEST_RUNNER 1424 1458 1425 1459 TEST_RUNNER