~ubuntu-branches/ubuntu/jaunty/python-django/jaunty-updates

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

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 unittest
13
 
import sys
14
 
import pkg_resources
15
 
import imp
16
 
import StringIO
17
 
import os.path
18
 
 
19
 
from django.template import TemplateDoesNotExist
20
 
from django.template.loaders.eggs import load_template_source as lts_egg
21
 
 
22
 
# Mock classes and objects for pkg_resources functions.
23
 
class MockProvider(pkg_resources.NullProvider):
24
 
    def __init__(self, module):
25
 
        pkg_resources.NullProvider.__init__(self, module)
26
 
        self.module = module
27
 
 
28
 
    def _has(self, path):
29
 
        return path in self.module._resources
30
 
 
31
 
    def _isdir(self,path):
32
 
        return False
33
 
 
34
 
    def get_resource_stream(self, manager, resource_name):
35
 
        return self.module._resources[resource_name]
36
 
 
37
 
    def _get(self, path):
38
 
        return self.module._resources[path].read()
39
 
 
40
 
class MockLoader(object):
41
 
    pass
42
 
 
43
 
def create_egg(name, resources):
44
 
    """
45
 
    Creates a mock egg with a list of resources.
46
 
 
47
 
    name: The name of the module.
48
 
    resources: A dictionary of resources. Keys are the names and values the the data.
49
 
    """
50
 
    egg = imp.new_module(name)
51
 
    egg.__loader__ = MockLoader()
52
 
    egg._resources = resources
53
 
    sys.modules[name] = egg
54
 
 
55
 
class EggLoader(unittest.TestCase):
56
 
    def setUp(self):
57
 
        pkg_resources._provider_factories[MockLoader] = MockProvider
58
 
 
59
 
        self.empty_egg = create_egg("egg_empty", {})
60
 
        self.egg_1 = create_egg("egg_1", {
61
 
            os.path.normcase('templates/y.html') : StringIO.StringIO("y"),
62
 
            os.path.normcase('templates/x.txt') : StringIO.StringIO("x"),
63
 
        })
64
 
        self._old_installed_apps = settings.INSTALLED_APPS
65
 
        settings.INSTALLED_APPS = []
66
 
 
67
 
    def tearDown(self):
68
 
        settings.INSTALLED_APPS = self._old_installed_apps
69
 
 
70
 
    def test_empty(self):
71
 
        "Loading any template on an empty egg should fail"
72
 
        settings.INSTALLED_APPS = ['egg_empty']
73
 
        self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
74
 
 
75
 
    def test_non_existing(self):
76
 
        "Template loading fails if the template is not in the egg"
77
 
        settings.INSTALLED_APPS = ['egg_1']
78
 
        self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
79
 
 
80
 
    def test_existing(self):
81
 
        "A template can be loaded from an egg"
82
 
        settings.INSTALLED_APPS = ['egg_1']
83
 
        contents, template_name = lts_egg("y.html")
84
 
        self.assertEqual(contents, "y")
85
 
        self.assertEqual(template_name, "egg:egg_1:templates/y.html")
86
 
 
87
 
    def test_not_installed(self):
88
 
        "Loading an existent template from an egg not included in INSTALLED_APPS should fail"
89
 
        settings.INSTALLED_APPS = []
90
 
        self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
91
 
 
92
 
if __name__ == "__main__":
93
 
    unittest.main()