Ticket #11077: test_combine_urlconfs_docs.patch

File test_combine_urlconfs_docs.patch, 6.1 KB (added by Cory Walker, 14 years ago)

Same as before but with documentation

  • django/test/testcases.py

     
    11import re
     2import sys
     3import imp
    24import unittest
    35from urlparse import urlsplit, urlunsplit
    46from xml.dom.minidom import parseString, Node
     
    1315from django.test.client import Client
    1416from django.utils import simplejson
    1517from django.utils.encoding import smart_str
     18from django.utils.importlib import import_module
    1619
    1720try:
    1821    all
     
    242245    def _urlconf_setup(self):
    243246        if hasattr(self, 'urls'):
    244247            self._old_root_urlconf = settings.ROOT_URLCONF
    245             settings.ROOT_URLCONF = self.urls
     248            if settings.TEST_COMBINE_URLCONFS:
     249                # Put the module instance of self.urls into specified_urlconf
     250                if not isinstance(self.urls, basestring):
     251                    specified_urlconf = self.urls
     252                else:
     253                    specified_urlconf = import_module(self.urls)
     254                # Choose between settings.TEST_COMBINE_URLCONFS_WITH and settings.ROOT_URLCONF
     255                urls_to_combine = settings.TEST_COMBINE_URLCONFS_WITH or settings.ROOT_URLCONF
     256                # Do the above action, this time with urls_to_combine
     257                if not isinstance(urls_to_combine, basestring):
     258                    extra_urlconf = urls_to_combine
     259                else:
     260                    extra_urlconf = import_module(urls_to_combine)
     261                # Create a dynamic URLconf module that combines the urlpatterns
     262                # of specified_urlconf and extra_urlconf
     263                dynamic_urlconf_name = 'dynamic_urls'
     264                dynamic_urlconf = imp.new_module(dynamic_urlconf_name)
     265                dynamic_urlconf.urlpatterns = specified_urlconf.urlpatterns + \
     266                        extra_urlconf.urlpatterns
     267                # Also combine handler404 and 500, favoring specified_urlconf
     268                handler404 = getattr(specified_urlconf, 'handler404', None) or \
     269                        getattr(extra_urlconf, 'handler404', None)
     270                if handler404:
     271                    dynamic_urlconf.handler404 = handler404
     272                handler500 = getattr(specified_urlconf, 'handler404', None) or \
     273                        getattr(extra_urlconf, 'handler404', None)
     274                if handler500:
     275                    dynamic_urlconf.handler500 = handler500
     276                # Add our dynamic_urlconf to sys.modules and set
     277                # settings.ROOT_URLCONF to it
     278                sys.modules[dynamic_urlconf_name] = dynamic_urlconf
     279                settings.ROOT_URLCONF = dynamic_urlconf
     280            else:
     281                settings.ROOT_URLCONF = self.urls
    246282            clear_url_caches()
    247283
    248284    def __call__(self, result=None):
  • django/conf/global_settings.py

     
    501501TEST_DATABASE_CHARSET = None
    502502TEST_DATABASE_COLLATION = None
    503503
     504# If true, the testing framework will combine the URLconf instead of overwrite
     505# it if a TestCase.urls is specified
     506TEST_COMBINE_URLCONFS = False
     507
     508# The URLconf to be combined with the TestCase.urls setting. If None, the
     509# ROOT_URLCONF specified in settings.py will be used instead.
     510TEST_COMBINE_URLCONFS_WITH = None
     511
    504512############
    505513# FIXTURES #
    506514############
  • docs/topics/testing.txt

     
    10541054This test case will use the contents of ``myapp.test_urls`` as the
    10551055URLconf for the duration of the test case.
    10561056
     1057The use of ``TestCase.urls`` has a downside, though. Since it overrides
     1058``ROOT_URLCONF``, your project's other urls will not be accessible. This is
     1059normally not a problem, but custom templates with reverses to external apps will
     1060cause the tests to raise ``NoReverseMatch`` exceptions. This problem can happen
     1061with :mod:`django.contrib.auth` and other apps that support custom templates and
     1062set ``TestCase.urls``. Custom templates that include a ``base.html`` with links to
     1063various places around the site are common sources of this problem.
     1064
     1065This situation can be fixed by combining the ``TestCase.urls`` with your
     1066project's ``ROOT_URLCONF``. You can also choose to combine a custom URLconf
     1067designed specifically for testing. See :setting:`TEST_COMBINE_URLCONFS` and
     1068:setting:`TEST_COMBINE_URLCONFS_WITH` for more information.
     1069
    10571070.. _emptying-test-outbox:
    10581071
    10591072Multi-database support
  • docs/ref/settings.txt

     
    14041404Output, as a string, that the template system should use for invalid (e.g.
    14051405misspelled) variables. See :ref:`invalid-template-variables`..
    14061406
     1407.. setting:: TEST_COMBINE_URLCONFS
     1408
     1409TEST_COMBINE_URLCONFS
     1410---------------------
     1411
     1412Default: ``False``
     1413
     1414If ``True``, the testing framework will combine the URLconf instead of overwrite
     1415it if a ``TestCase.urls`` is specified. The URLconf merged into the
     1416``TestCase.urls`` will be :setting:`ROOT_URLCONF` unless
     1417:setting:`TEST_COMBINE_URLCONFS_WITH` is set.
     1418
     1419This setting is useful for cases where apps like :mod:`django.contrib.auth` set
     1420``TestCase.urls`` to override the :setting:`ROOT_URLCONF` with a custom testing
     1421URLconf. This setting will fix the problem where custom templates with reverses
     1422to external apps cause the tests to fail with ``NoReverseMatch`` errors.
     1423
     1424.. setting:: TEST_COMBINE_URLCONFS_WITH
     1425
     1426TEST_COMBINE_URLCONFS_WITH
     1427--------------------------
     1428
     1429Default: ``None``
     1430
     1431The URLconf to be combined with the ``TestCase.urls`` setting if it is set. If
     1432``None``, the :setting:`ROOT_URLCONF` will be used instead. This setting will
     1433only be used if :setting:`TEST_COMBINE_URLCONFS` is ``True``. This can either be
     1434a string pointing to a module or an actual module, much like
     1435:setting:`ROOT_URLCONF`.
     1436
    14071437.. setting:: TEST_RUNNER
    14081438
    14091439TEST_RUNNER
Back to Top