PageRenderTime 235ms CodeModel.GetById 141ms app.highlight 10ms RepoModel.GetById 70ms app.codeStats 7ms

/tests/regressiontests/utils/module_loading.py

https://code.google.com/p/mango-py/
Python | 119 lines | 74 code | 23 blank | 22 comment | 1 complexity | 9e4181615fee7e5df5af9100a66b8a55 MD5 | raw file
  1import os
  2import sys
  3from zipimport import zipimporter
  4
  5from django.utils import unittest
  6from django.utils.importlib import import_module
  7from django.utils.module_loading import module_has_submodule
  8
  9
 10class DefaultLoader(unittest.TestCase):
 11    def test_loader(self):
 12        "Normal module existence can be tested"
 13        test_module = import_module('regressiontests.utils.test_module')
 14
 15        # An importable child
 16        self.assertTrue(module_has_submodule(test_module, 'good_module'))
 17        mod = import_module('regressiontests.utils.test_module.good_module')
 18        self.assertEqual(mod.content, 'Good Module')
 19
 20        # A child that exists, but will generate an import error if loaded
 21        self.assertTrue(module_has_submodule(test_module, 'bad_module'))
 22        self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.bad_module')
 23
 24        # A child that doesn't exist
 25        self.assertFalse(module_has_submodule(test_module, 'no_such_module'))
 26        self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.no_such_module')
 27
 28        # Don't be confused by caching of import misses
 29        import types  # causes attempted import of regressiontests.utils.types
 30        self.assertFalse(module_has_submodule(sys.modules['regressiontests.utils'], 'types'))
 31
 32class EggLoader(unittest.TestCase):
 33    def setUp(self):
 34        self.old_path = sys.path[:]
 35        self.egg_dir = '%s/eggs' % os.path.dirname(__file__)
 36
 37    def tearDown(self):
 38        sys.path = self.old_path
 39        sys.path_importer_cache.clear()
 40
 41        sys.modules.pop('egg_module.sub1.sub2.bad_module', None)
 42        sys.modules.pop('egg_module.sub1.sub2.good_module', None)
 43        sys.modules.pop('egg_module.sub1.sub2', None)
 44        sys.modules.pop('egg_module.sub1', None)
 45        sys.modules.pop('egg_module.bad_module', None)
 46        sys.modules.pop('egg_module.good_module', None)
 47        sys.modules.pop('egg_module', None)
 48
 49    def test_shallow_loader(self):
 50        "Module existence can be tested inside eggs"
 51        egg_name = '%s/test_egg.egg' % self.egg_dir
 52        sys.path.append(egg_name)
 53        egg_module = import_module('egg_module')
 54
 55        # An importable child
 56        self.assertTrue(module_has_submodule(egg_module, 'good_module'))
 57        mod = import_module('egg_module.good_module')
 58        self.assertEqual(mod.content, 'Good Module')
 59
 60        # A child that exists, but will generate an import error if loaded
 61        self.assertTrue(module_has_submodule(egg_module, 'bad_module'))
 62        self.assertRaises(ImportError, import_module, 'egg_module.bad_module')
 63
 64        # A child that doesn't exist
 65        self.assertFalse(module_has_submodule(egg_module, 'no_such_module'))
 66        self.assertRaises(ImportError, import_module, 'egg_module.no_such_module')
 67
 68    def test_deep_loader(self):
 69        "Modules deep inside an egg can still be tested for existence"
 70        egg_name = '%s/test_egg.egg' % self.egg_dir
 71        sys.path.append(egg_name)
 72        egg_module = import_module('egg_module.sub1.sub2')
 73
 74        # An importable child
 75        self.assertTrue(module_has_submodule(egg_module, 'good_module'))
 76        mod = import_module('egg_module.sub1.sub2.good_module')
 77        self.assertEqual(mod.content, 'Deep Good Module')
 78
 79        # A child that exists, but will generate an import error if loaded
 80        self.assertTrue(module_has_submodule(egg_module, 'bad_module'))
 81        self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.bad_module')
 82
 83        # A child that doesn't exist
 84        self.assertFalse(module_has_submodule(egg_module, 'no_such_module'))
 85        self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.no_such_module')
 86
 87class TestFinder(object):
 88    def __init__(self, *args, **kwargs):
 89        self.importer = zipimporter(*args, **kwargs)
 90
 91    def find_module(self, path):
 92        importer = self.importer.find_module(path)
 93        if importer is None:
 94            return
 95        return TestLoader(importer)
 96
 97class TestLoader(object):
 98    def __init__(self, importer):
 99        self.importer = importer
100
101    def load_module(self, name):
102        mod = self.importer.load_module(name)
103        mod.__loader__ = self
104        return mod
105
106class CustomLoader(EggLoader):
107    """The Custom Loader test is exactly the same as the EggLoader, but
108    it uses a custom defined Loader and Finder that is intentionally
109    split into two classes. Although the EggLoader combines both functions
110    into one class, this isn't required.
111    """
112    def setUp(self):
113        super(CustomLoader, self).setUp()
114        sys.path_hooks.insert(0, TestFinder)
115        sys.path_importer_cache.clear()
116
117    def tearDown(self):
118        super(CustomLoader, self).tearDown()
119        sys.path_hooks.pop(0)