Ticket #7611: test_template_loader_r7958.diff

File test_template_loader_r7958.diff, 3.3 KB (added by Jason Yan, 16 years ago)
  • django/test/testcases.py

     
    5858              named fixtures.
    5959            * If the Test Case class has a 'urls' member, replace the
    6060              ROOT_URLCONF with it.
     61            * If the Test Case class has a 'templates` member, replace the
     62              TEMPLATE_DIRS with it, and include the test template loader.
    6163            * Clearing the mail test outbox.
    6264        """
    6365        call_command('flush', verbosity=0, interactive=False)
     
    6971            self._old_root_urlconf = settings.ROOT_URLCONF
    7072            settings.ROOT_URLCONF = self.urls
    7173            clear_url_caches()
     74        if hasattr(self, 'templates'):
     75            self._old_template_loaders = settings.TEMPLATE_LOADERS
     76            self._old_template_dirs = settings.TEMPLATE_DIRS
     77            settings.TEMPLATE_LOADERS = ('django.test.template_loader.load_template_source',) + settings.TEMPLATE_LOADERS
     78            settings.TEMPLATE_DIRS = self.templates
    7279        mail.outbox = []
    7380
    7481    def __call__(self, result=None):
     
    104111        if hasattr(self, '_old_root_urlconf'):
    105112            settings.ROOT_URLCONF = self._old_root_urlconf
    106113            clear_url_caches()
     114        if hasattr(self, '_old_template_loaders'):
     115            settings.TEMPLATE_LOADERS = self._old_template_loaders
     116            settings.TEMPLATE_DIRS = self._old_template_dirs
    107117
    108118    def assertRedirects(self, response, expected_url, status_code=302,
    109119                        target_status_code=200, host=None):
  • django/test/template_loader.py

     
     1from django.conf import settings
     2from django.template import TemplateDoesNotExist
     3
     4def get_template_sources(template_name, template_dirs=None):
     5    # We don't use template_dirs as a list of directories.  Instead,
     6    # template_dirs is a dictionary with the template_name as the key and
     7    # the contents of the template as the value.
     8    if not template_dirs:
     9        template_dirs = settings.TEMPLATE_DIRS
     10    return template_dirs
     11
     12def load_template_source(template_name, template_dirs=None):
     13    template_dirs = get_template_sources(template_name, template_dirs)
     14    if template_name in template_dirs:
     15        return (template_dirs[template_name], template_name)
     16    else:
     17        raise TemplateDoesNotExist, template_name
     18load_template_source.is_usable = True
  • django/contrib/auth/tests.py

     
    6060class PasswordResetTest(TestCase):
    6161    fixtures = ['authtestdata.json']
    6262    urls = 'django.contrib.auth.urls'
     63    templates = {
     64        'registration/password_reset_form.html': '{% if form.email.errors %}{{ form.email.html_error_list }}{% endif %}',
     65        'registration/password_reset_email.html': '',
     66    }
     67
    6368    def test_email_not_found(self):
    6469        "Error is raised if the provided email address isn't currently registered"
    6570        response = self.client.get('/password_reset/')
Back to Top