Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 1 | from .. import util |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 2 | from . import util as import_util |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import unittest |
| 5 | import importlib |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class ParentModuleTests(unittest.TestCase): |
| 9 | |
| 10 | """Importing a submodule should import the parent modules.""" |
| 11 | |
| 12 | def test_import_parent(self): |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 13 | with util.mock_modules('pkg.__init__', 'pkg.module') as mock: |
| 14 | with util.import_state(meta_path=[mock]): |
Brett Cannon | d720b36 | 2009-02-01 04:28:04 +0000 | [diff] [blame] | 15 | module = import_util.import_('pkg.module') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 16 | self.assertTrue('pkg' in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 17 | |
| 18 | def test_bad_parent(self): |
Brett Cannon | bcb26c5 | 2009-02-01 04:00:05 +0000 | [diff] [blame] | 19 | with util.mock_modules('pkg.module') as mock: |
| 20 | with util.import_state(meta_path=[mock]): |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 21 | with self.assertRaises(ImportError): |
| 22 | import_util.import_('pkg.module') |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 23 | |
Brett Cannon | 1c1dcbf | 2009-08-30 20:22:21 +0000 | [diff] [blame] | 24 | def test_module_not_package(self): |
| 25 | # Try to import a submodule from a non-package should raise ImportError. |
| 26 | assert not hasattr(sys, '__path__') |
| 27 | with self.assertRaises(ImportError): |
| 28 | import_util.import_('sys.no_submodules_here') |
| 29 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 30 | |
| 31 | def test_main(): |
| 32 | from test.support import run_unittest |
| 33 | run_unittest(ParentModuleTests) |
| 34 | |
| 35 | |
| 36 | if __name__ == '__main__': |
| 37 | test_main() |