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

Code before:
        with patch('celery.loaders.base.find_related_module') as frm:
            base.autodiscover_tasks(['foo'])
            frm.assert_called()

    # Happy - get something back
    def test_find_related_module__when_existent_package_alone(self):
        with patch('importlib.import_module') as imp:
            imp.return_value = Mock()
            imp.return_value.__path__ = 'foo'
            assert base.find_related_module('foo', None).__path__ == 'foo'
            imp.assert_called_once_with('foo')

    def test_find_related_module__when_existent_package_and_related_name(self):
        with patch('importlib.import_module') as imp:
            first_import = Mock()
            first_import.__path__ = 'foo'
            second_import = Mock()
            second_import.__path__ = 'foo/tasks'
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo', 'tasks').__path__ == 'foo/tasks'
            imp.assert_any_call('foo')
            imp.assert_any_call('foo.tasks')

    def test_find_related_module__when_existent_package_parent_and_related_name(self):
        with patch('importlib.import_module') as imp:
            first_import = ModuleNotFoundError(name='foo.BarApp')  # Ref issue #2248
            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')

Review comment (bug):
It seems like this behavior was added to match some edge case w/ how Django apps present themselves for auto-discovery (see #2248).

Code after:
        with patch('celery.loaders.base.find_related_module') as frm:
            base.autodiscover_tasks(['foo'])
            frm.assert_called()

    # Happy - get something back
    def test_find_related_module__when_existent_package_alone(self):
        with patch('importlib.import_module') as imp:
            imp.return_value = Mock()
            imp.return_value.__path__ = 'foo'
            assert base.find_related_module('foo', None).__path__ == 'foo'
            imp.assert_called_once_with('foo')

    def test_find_related_module__when_existent_package_and_related_name(self):
        with patch('importlib.import_module') as imp:
            first_import = Mock()
            first_import.__path__ = 'foo'
            second_import = Mock()
            second_import.__path__ = 'foo/tasks'
            imp.side_effect = [first_import, second_import]
            assert base.find_related_module('foo', 'tasks').__path__ == 'foo/tasks'
            imp.assert_any_call('foo')
            imp.assert_any_call('foo.tasks')

    def test_find_related_module__when_existent_package_parent_and_related_name(self):
        with patch('importlib.import_module') as imp:
            first_import = ModuleNotFoundError(name='foo.BarApp')  # Ref issue #2248
            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')