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 | |
| 24 | |
| 25 | def test_main(): |
| 26 | from test.support import run_unittest |
| 27 | run_unittest(ParentModuleTests) |
| 28 | |
| 29 | |
| 30 | if __name__ == '__main__': |
| 31 | test_main() |