Repository: celery/celery
File: t/unit/app/test_loaders.py (Python)

Code before:
            second_import = Mock()
            second_import.__path__ = 'foo/tasks'
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo.BarApp', 'tasks').__path__ == 'foo/tasks'
            imp.assert_any_call('foo.BarApp')
            imp.assert_any_call('foo.tasks')

    # Sad - nothing returned
    def test_find_related_module__when_package_exists_but_related_name_does_not(self):
        with patch('importlib.import_module') as imp:
            first_import = Mock()
            first_import.__path__ = 'foo'
            second_import = ModuleNotFoundError(name='foo.tasks')
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo', 'tasks') is None
            imp.assert_any_call('foo')
            imp.assert_any_call('foo.tasks')

    def test_find_related_module__when_existent_package_parent_but_not_related_name(self):
        with patch('importlib.import_module') as imp:
            first_import = ModuleNotFoundError(name='foo.bar')
            second_import = ModuleNotFoundError(name='foo.tasks')
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo.bar', 'tasks') is None
            imp.assert_any_call('foo.bar')
            imp.assert_any_call('foo.tasks')

    # Sad - errors
    def test_find_related_module__when_no_package_parent(self):
        with patch('importlib.import_module') as imp:
            non_existent_import = ModuleNotFoundError(name='foo')
            imp.side_effect = non_existent_import
            with pytest.raises(ImportError) as exc:
                base.find_related_module('foo', 'tasks')

            assert exc.value.name == 'foo'
            imp.assert_called_once_with('foo')

    def test_find_related_module__when_nested_import_missing(self):
        expected_error = 'nested import error - e.g. missing library'
        with patch('importlib.import_module') as imp:
            first_import = Mock()
            first_import.__path__ = 'foo'
            second_import = ImportError(expected_error)
            imp.side_effect = [first_import, second_import]
            with pytest.raises(ImportError) as exc:
                base.find_related_module('foo', 'tasks')

            assert exc.value.msg == expected_error


Review comment (suggestion):
This is a weird case -- I'm not sure if `find_related_module` is actually meant to handle this or not, so can remove it if not.

Code after:
            second_import = Mock()
            second_import.__path__ = 'foo/tasks'
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo.BarApp', 'tasks').__path__ == 'foo/tasks'
            imp.assert_any_call('foo.BarApp')
            imp.assert_any_call('foo.tasks')

    # Sad - nothing returned
    def test_find_related_module__when_package_exists_but_related_name_does_not(self):
        with patch('importlib.import_module') as imp:
            first_import = Mock()
            first_import.__path__ = 'foo'
            second_import = ModuleNotFoundError(name='foo.tasks')
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo', 'tasks') is None
            imp.assert_any_call('foo')
            imp.assert_any_call('foo.tasks')

    def test_find_related_module__when_existent_package_parent_but_no_related_name(self):
        with patch('importlib.import_module') as imp:
            first_import = ModuleNotFoundError(name='foo.bar')
            second_import = ModuleNotFoundError(name='foo.tasks')
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo.bar', 'tasks') is None
            imp.assert_any_call('foo.bar')
            imp.assert_any_call('foo.tasks')

    # Sad - errors
    def test_find_related_module__when_no_package_parent(self):
        with patch('importlib.import_module') as imp:
            non_existent_import = ModuleNotFoundError(name='foo')
            imp.side_effect = non_existent_import
            with pytest.raises(ModuleNotFoundError) as exc:
                base.find_related_module('foo', 'tasks')

            assert exc.value.name == 'foo'
            imp.assert_called_once_with('foo')

    def test_find_related_module__when_nested_import_missing(self):
        expected_error = 'dummy import error - e.g. missing nested package'
        with patch('importlib.import_module') as imp:
            first_import = Mock()
            first_import.__path__ = 'foo'
            second_import = ModuleNotFoundError(expected_error)
            imp.side_effect = [first_import, second_import]
            with pytest.raises(ModuleNotFoundError) as exc:
                base.find_related_module('foo', 'tasks')

            assert exc.value.msg == expected_error