Test both case where class and/or module does not exist for get_class

This commit is contained in:
Thomas Adamcik 2010-08-13 22:29:41 +02:00
parent e4bdacbb61
commit ec67d43fc9
2 changed files with 7 additions and 3 deletions

View File

@ -26,9 +26,9 @@ def get_class(name):
logger.debug('Loading: %s', name)
try:
module = import_module(module_name)
except ImportError:
class_object = getattr(module, class_name)
except (ImportError, AttributeError):
raise ImportError("Couldn't load: %s" % name)
class_object = getattr(module, class_name)
return class_object
def get_or_create_folder(folder):

View File

@ -12,10 +12,14 @@ from mopidy.models import Track, Artist, Album
from tests import SkipTest, data_folder
class GetClassTest(unittest.TestCase):
def test_loading_class_that_does_not_exist(self):
def test_loading_module_that_does_not_exist(self):
test = lambda: get_class('foo.bar.Baz')
self.assertRaises(ImportError, test)
def test_loading_class_that_does_not_exist(self):
test = lambda: get_class('unittest.FooBarBaz')
self.assertRaises(ImportError, test)
def test_import_error_message_contains_complete_class_path(self):
try:
get_class('foo.bar.Baz')