Ticket #11077: test_combine_urlconfs_poc.diff

File test_combine_urlconfs_poc.diff, 3.5 KB (added by Cory Walker, 14 years ago)

A proof of concept for the testing framework's URLconf-combining feature

  • 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############
Back to Top