~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/regressiontests/templates/loaders.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Test cases for the template loaders
3
 
 
4
 
Note: This test requires setuptools!
5
 
"""
6
 
 
7
 
from django.conf import settings
8
 
 
9
 
if __name__ == '__main__':
10
 
    settings.configure()
11
 
 
12
 
import sys
13
 
import pkg_resources
14
 
import imp
15
 
import os.path
16
 
 
17
 
from django.template import TemplateDoesNotExist, Context
18
 
from django.template.loaders.eggs import Loader as EggLoader
19
 
from django.template import loader
20
 
from django.utils import unittest, six
21
 
from django.utils._os import upath
22
 
from django.utils.six import StringIO
23
 
 
24
 
 
25
 
# Mock classes and objects for pkg_resources functions.
26
 
class MockProvider(pkg_resources.NullProvider):
27
 
    def __init__(self, module):
28
 
        pkg_resources.NullProvider.__init__(self, module)
29
 
        self.module = module
30
 
 
31
 
    def _has(self, path):
32
 
        return path in self.module._resources
33
 
 
34
 
    def _isdir(self, path):
35
 
        return False
36
 
 
37
 
    def get_resource_stream(self, manager, resource_name):
38
 
        return self.module._resources[resource_name]
39
 
 
40
 
    def _get(self, path):
41
 
        return self.module._resources[path].read()
42
 
 
43
 
class MockLoader(object):
44
 
    pass
45
 
 
46
 
def create_egg(name, resources):
47
 
    """
48
 
    Creates a mock egg with a list of resources.
49
 
 
50
 
    name: The name of the module.
51
 
    resources: A dictionary of resources. Keys are the names and values the data.
52
 
    """
53
 
    egg = imp.new_module(name)
54
 
    egg.__loader__ = MockLoader()
55
 
    egg._resources = resources
56
 
    sys.modules[name] = egg
57
 
 
58
 
 
59
 
class EggLoaderTest(unittest.TestCase):
60
 
    def setUp(self):
61
 
        pkg_resources._provider_factories[MockLoader] = MockProvider
62
 
 
63
 
        self.empty_egg = create_egg("egg_empty", {})
64
 
        self.egg_1 = create_egg("egg_1", {
65
 
            os.path.normcase('templates/y.html'): StringIO("y"),
66
 
            os.path.normcase('templates/x.txt'): StringIO("x"),
67
 
        })
68
 
        self._old_installed_apps = settings.INSTALLED_APPS
69
 
        settings.INSTALLED_APPS = []
70
 
 
71
 
    def tearDown(self):
72
 
        settings.INSTALLED_APPS = self._old_installed_apps
73
 
 
74
 
    def test_empty(self):
75
 
        "Loading any template on an empty egg should fail"
76
 
        settings.INSTALLED_APPS = ['egg_empty']
77
 
        egg_loader = EggLoader()
78
 
        self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
79
 
 
80
 
    def test_non_existing(self):
81
 
        "Template loading fails if the template is not in the egg"
82
 
        settings.INSTALLED_APPS = ['egg_1']
83
 
        egg_loader = EggLoader()
84
 
        self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "not-existing.html")
85
 
 
86
 
    def test_existing(self):
87
 
        "A template can be loaded from an egg"
88
 
        settings.INSTALLED_APPS = ['egg_1']
89
 
        egg_loader = EggLoader()
90
 
        contents, template_name = egg_loader.load_template_source("y.html")
91
 
        self.assertEqual(contents, "y")
92
 
        self.assertEqual(template_name, "egg:egg_1:templates/y.html")
93
 
 
94
 
    def test_not_installed(self):
95
 
        "Loading an existent template from an egg not included in INSTALLED_APPS should fail"
96
 
        settings.INSTALLED_APPS = []
97
 
        egg_loader = EggLoader()
98
 
        self.assertRaises(TemplateDoesNotExist, egg_loader.load_template_source, "y.html")
99
 
 
100
 
class CachedLoader(unittest.TestCase):
101
 
    def setUp(self):
102
 
        self.old_TEMPLATE_LOADERS = settings.TEMPLATE_LOADERS
103
 
        settings.TEMPLATE_LOADERS = (
104
 
            ('django.template.loaders.cached.Loader', (
105
 
                    'django.template.loaders.filesystem.Loader',
106
 
                )
107
 
            ),
108
 
        )
109
 
    def tearDown(self):
110
 
        settings.TEMPLATE_LOADERS = self.old_TEMPLATE_LOADERS
111
 
 
112
 
    def test_templatedir_caching(self):
113
 
        "Check that the template directories form part of the template cache key. Refs #13573"
114
 
        # Retrive a template specifying a template directory to check
115
 
        t1, name = loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'first'),))
116
 
        # Now retrieve the same template name, but from a different directory
117
 
        t2, name = loader.find_template('test.html', (os.path.join(os.path.dirname(upath(__file__)), 'templates', 'second'),))
118
 
 
119
 
        # The two templates should not have the same content
120
 
        self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
121
 
 
122
 
class RenderToStringTest(unittest.TestCase):
123
 
 
124
 
    def setUp(self):
125
 
        self._old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
126
 
        settings.TEMPLATE_DIRS = (
127
 
            os.path.join(os.path.dirname(upath(__file__)), 'templates'),
128
 
        )
129
 
 
130
 
    def tearDown(self):
131
 
        settings.TEMPLATE_DIRS = self._old_TEMPLATE_DIRS
132
 
 
133
 
    def test_basic(self):
134
 
        self.assertEqual(loader.render_to_string('test_context.html'), 'obj:')
135
 
 
136
 
    def test_basic_context(self):
137
 
        self.assertEqual(loader.render_to_string('test_context.html',
138
 
                                                 {'obj': 'test'}), 'obj:test')
139
 
 
140
 
    def test_existing_context_kept_clean(self):
141
 
        context = Context({'obj': 'before'})
142
 
        output = loader.render_to_string('test_context.html', {'obj': 'after'},
143
 
                                         context_instance=context)
144
 
        self.assertEqual(output, 'obj:after')
145
 
        self.assertEqual(context['obj'], 'before')
146
 
 
147
 
    def test_empty_list(self):
148
 
        six.assertRaisesRegex(self, TemplateDoesNotExist,
149
 
                                'No template names provided$',
150
 
                                loader.render_to_string, [])
151
 
 
152
 
 
153
 
    def test_select_templates_from_empty_list(self):
154
 
        six.assertRaisesRegex(self, TemplateDoesNotExist,
155
 
                                'No template names provided$',
156
 
                                loader.select_template, [])