| 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | Test cases for the template loaders |
| 4 | """ |
| 5 | |
| 6 | from django.conf import settings |
| 7 | |
| 8 | if __name__ == '__main__': |
| 9 | settings.configure() |
| 10 | |
| 11 | import unittest |
| 12 | import sys |
| 13 | import pkg_resources |
| 14 | import imp |
| 15 | import StringIO |
| 16 | |
| 17 | from django.template import TemplateDoesNotExist |
| 18 | from django.template.loaders.eggs import load_template_source as lts_egg |
| 19 | |
| 20 | |
| 21 | #Mock classes and objects for pkg_resources functions |
| 22 | class MockProvider(pkg_resources.NullProvider): |
| 23 | |
| 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): pass |
| 41 | |
| 42 | |
| 43 | |
| 44 | def create_egg(name, resources): |
| 45 | """ |
| 46 | Creates a mock egg with a list of resources |
| 47 | |
| 48 | name: The name of the module |
| 49 | resources: A dictionary of resources. Keys are the names and values the the data. |
| 50 | |
| 51 | """ |
| 52 | egg = imp.new_module(name) |
| 53 | egg.__loader__ = MockLoader() |
| 54 | egg._resources = resources |
| 55 | sys.modules[name] = egg |
| 56 | |
| 57 | |
| 58 | class EggLoader(unittest.TestCase): |
| 59 | |
| 60 | def setUp(self): |
| 61 | pkg_resources._provider_factories[MockLoader] = MockProvider |
| 62 | |
| 63 | self.empty_egg = create_egg("egg_emtpy", {}) |
| 64 | self.egg_1 = create_egg("egg_1", { |
| 65 | 'templates/y.html' : StringIO.StringIO("y"), |
| 66 | 'templates/x.txt' : StringIO.StringIO("x"), |
| 67 | }) |
| 68 | settings.INSTALLED_APPS = [] |
| 69 | |
| 70 | |
| 71 | def test_emtpy(self): |
| 72 | """ |
| 73 | Loading any template on an empty egg should fail |
| 74 | """ |
| 75 | settings.INSTALLED_APPS = ['egg_emtpy'] |
| 76 | self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html") |
| 77 | |
| 78 | def test_non_existing(self): |
| 79 | settings.INSTALLED_APPS = ['egg_emtpy'] |
| 80 | self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html") |
| 81 | |
| 82 | def test_existing(self): |
| 83 | settings.INSTALLED_APPS = ['egg_1'] |
| 84 | contents, template_name = lts_egg("y.html") |
| 85 | self.assertEqual(contents, "y") |
| 86 | self.assertEqual(template_name, "egg:egg_1:templates/y.html") |
| 87 | |
| 88 | def test_not_installed(self): |
| 89 | """ |
| 90 | Loading an existing template on a egg not included in INSTALLED_APPS |
| 91 | should fail |
| 92 | """ |
| 93 | settings.INSTALLED_APPS = [] |
| 94 | self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html") |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | unittest.main() |