[Python-checkins] r52941 - sandbox/trunk/import_in_py/test_importer.py

brett.cannon python-checkins at python.org
Wed Dec 6 23:39:31 CET 2006


Author: brett.cannon
Date: Wed Dec  6 23:39:31 2006
New Revision: 52941

Modified:
   sandbox/trunk/import_in_py/test_importer.py
Log:
Refactor ImportMiscTests.


Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py	(original)
+++ sandbox/trunk/import_in_py/test_importer.py	Wed Dec  6 23:39:31 2006
@@ -760,14 +760,9 @@
                 pass
 
 
-class ImportMiscTests(ImportHelper):
+class ImportHelper2(ImportHelper):
 
-    """Test miscellaneous parts of the Import class.
-    
-    Tests of the default values for __init__ are handled in the integration
-    tests.
-
-    """
+    """Create mock modules."""
 
     def setUp(self):
         ImportHelper.setUp(self)
@@ -778,69 +773,10 @@
         self.child_module = mock_importer.MockModule(self.full_child_name)
         setattr(self.parent_module, self.child_name, self.child_module)
 
-    def test_sys_module_return(self):
-        # A module found in sys.modules should be returned immediately.
-        sys.modules[self.parent_name] = self.parent_module
-        module = self.importer.import_module(self.parent_name)
-        self.failUnless(module is self.parent_module)
-        
-    def test_sys_module_None(self):
-        # If sys.modules contains None for a module name, then raise ImportError.
-        module_name = '<module>'
-        sys.modules[module_name] = None
-        self.failUnlessRaises(ImportError, self.importer.import_module,
-                                module_name)
-        
-    def test_parent_missing(self):
-        # An import should fail if a parent module cannot be found.
-        sys.modules[self.full_child_name] = self.child_module
-        self.failUnlessRaises(ImportError, self.importer, self.full_child_name)
-        
-    def test_parent_imported(self):
-        # If a parent module is already imported, importing a child module
-        # should work (along with setting the attribute on the parent for the
-        # child module).
-        delattr(self.parent_module, self.child_name)
-        succeed_importer = mock_importer.SucceedImporter()
-        sys.meta_path.append(succeed_importer)
-        sys.modules[self.parent_name] = self.parent_module
-        self.importer(self.full_child_name)
-        self.failUnless(hasattr(self.parent_module, self.child_name))
-        self.failUnlessEqual(getattr(self.parent_module, self.child_name),
-                                sys.modules[self.full_child_name])
-
-    def test_parent_not_imported(self):
-        # If a parent module is not imported yet, it should be imported.
-        # The attribute on the parent module for the child module should also
-        # be set.
-        delattr(self.parent_module, self.child_name)
-        succeed_importer = mock_importer.SucceedImporter()
-        sys.meta_path.append(succeed_importer)
-        self.importer(self.full_child_name)
-        self.failUnless(self.parent_name in sys.modules)
-        self.failUnless(self.full_child_name in sys.modules)
-        self.failUnless(hasattr(sys.modules[self.parent_name], self.child_name))
-        self.failUnlessEqual(getattr(sys.modules[self.parent_name],
-                                        self.child_name),
-                                sys.modules[self.full_child_name])
-
-    def test_empty_fromlist(self):
-        # An empty fromlist means that the root module is returned.
-        sys.modules[self.parent_name] = self.parent_module
-        sys.modules[self.full_child_name] = self.child_module
-        module = self.importer.return_module(self.full_child_name, '', [])
-        self.failUnless(module is self.parent_module)
-
-    def test_nonempty_fromlist(self):
-        # A fromlist with values should return the specified module.
-        sys.modules[self.parent_name] = self.parent_module
-        sys.modules[self.full_child_name] = self.child_module
-        test_attr_name = 'test_attr'
-        setattr(self.child_module, test_attr_name, None)
-        module = self.importer.return_module(self.full_child_name, '',
-                                                [test_attr_name])
-        self.failUnless(module is self.child_module)
+class ImportNameResolutionTests(ImportHelper2):
 
+    """Test the absolute name resolution for relative imports."""
+    
     def test_classic_relative_import_in_package(self):
         # Importing from within a package's __init__ file should lead to a
         # resolved import name of the package name tacked on to the name of the
@@ -917,17 +853,11 @@
         imported = self.importer(module_name, importing_globals, level=-1)
         self.failUnless(imported is expected_module)
 
-    def test_None_not_set_for_import_failure(self):
-        # If an import that is tried both relative and absolute fails there
-        # should not be an entry of None for the resolved relative name.
-        module_name = '<should fail>'
-        pkg_name = '<non-existent package>'
-        resolved_name = module_name + '.' + pkg_name
-        importing_globals = {'__name__':pkg_name, '__path__':['path']}
-        self.failUnlessRaises(ImportError, self.importer, module_name,
-                                importing_globals, {}, [], -1)
-        self.failUnless(resolved_name not in sys.modules)
 
+class ImportFromListTests(ImportHelper2):
+
+    """Test conditions based on fromlist."""
+    
     def test_fromlist_relative_import(self):
         # Any items specified in fromlist while importing a package needs to be
         # checked as to whether it is a pre-existing attribute or should be
@@ -1000,6 +930,90 @@
         relative_module = getattr(module, module_name)
         self.failUnlessEqual(relative_module.__name__, full_module_name)
         
+    def test_empty_fromlist(self):
+        # An empty fromlist means that the root module is returned.
+        sys.modules[self.parent_name] = self.parent_module
+        sys.modules[self.full_child_name] = self.child_module
+        module = self.importer.return_module(self.full_child_name, '', [])
+        self.failUnless(module is self.parent_module)
+
+    def test_nonempty_fromlist(self):
+        # A fromlist with values should return the specified module.
+        sys.modules[self.parent_name] = self.parent_module
+        sys.modules[self.full_child_name] = self.child_module
+        test_attr_name = 'test_attr'
+        setattr(self.child_module, test_attr_name, None)
+        module = self.importer.return_module(self.full_child_name, '',
+                                                [test_attr_name])
+        self.failUnless(module is self.child_module)
+
+
+class ImportMiscTests(ImportHelper2):
+
+    """Test miscellaneous parts of the Import class.
+    
+    Tests of the default values for __init__ are handled in the integration
+    tests.
+
+    """
+
+    def test_sys_module_return(self):
+        # A module found in sys.modules should be returned immediately.
+        sys.modules[self.parent_name] = self.parent_module
+        module = self.importer.import_module(self.parent_name)
+        self.failUnless(module is self.parent_module)
+        
+    def test_sys_module_None(self):
+        # If sys.modules contains None for a module name, then raise ImportError.
+        module_name = '<module>'
+        sys.modules[module_name] = None
+        self.failUnlessRaises(ImportError, self.importer.import_module,
+                                module_name)
+        
+    def test_parent_missing(self):
+        # An import should fail if a parent module cannot be found.
+        sys.modules[self.full_child_name] = self.child_module
+        self.failUnlessRaises(ImportError, self.importer, self.full_child_name)
+        
+    def test_parent_imported(self):
+        # If a parent module is already imported, importing a child module
+        # should work (along with setting the attribute on the parent for the
+        # child module).
+        delattr(self.parent_module, self.child_name)
+        succeed_importer = mock_importer.SucceedImporter()
+        sys.meta_path.append(succeed_importer)
+        sys.modules[self.parent_name] = self.parent_module
+        self.importer(self.full_child_name)
+        self.failUnless(hasattr(self.parent_module, self.child_name))
+        self.failUnlessEqual(getattr(self.parent_module, self.child_name),
+                                sys.modules[self.full_child_name])
+
+    def test_parent_not_imported(self):
+        # If a parent module is not imported yet, it should be imported.
+        # The attribute on the parent module for the child module should also
+        # be set.
+        delattr(self.parent_module, self.child_name)
+        succeed_importer = mock_importer.SucceedImporter()
+        sys.meta_path.append(succeed_importer)
+        self.importer(self.full_child_name)
+        self.failUnless(self.parent_name in sys.modules)
+        self.failUnless(self.full_child_name in sys.modules)
+        self.failUnless(hasattr(sys.modules[self.parent_name], self.child_name))
+        self.failUnlessEqual(getattr(sys.modules[self.parent_name],
+                                        self.child_name),
+                                sys.modules[self.full_child_name])
+
+    def test_None_not_set_for_import_failure(self):
+        # If an import that is tried both relative and absolute fails there
+        # should not be an entry of None for the resolved relative name.
+        module_name = '<should fail>'
+        pkg_name = '<non-existent package>'
+        resolved_name = module_name + '.' + pkg_name
+        importing_globals = {'__name__':pkg_name, '__path__':['path']}
+        self.failUnlessRaises(ImportError, self.importer, module_name,
+                                importing_globals, {}, [], -1)
+        self.failUnless(resolved_name not in sys.modules)
+        
     def test_empty_string(self):
         # An empty string should raise ValueError.
         self.failUnlessRaises(ValueError, self.importer, '')


More information about the Python-checkins mailing list