From ec67d43fc960d5537fef3fdf5e6fc46c6a354e0f Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Fri, 13 Aug 2010 22:29:41 +0200 Subject: [PATCH] Test both case where class and/or module does not exist for get_class --- mopidy/utils.py | 4 ++-- tests/utils_test.py | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mopidy/utils.py b/mopidy/utils.py index b8aa574c..bdc0b632 100644 --- a/mopidy/utils.py +++ b/mopidy/utils.py @@ -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): diff --git a/tests/utils_test.py b/tests/utils_test.py index d5beade2..ca44de45 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -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')