mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-11-24 09:23:41 +00:00
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
import pytest
|
|
|
|
from importlib.machinery import ModuleSpec
|
|
from pathlib import Path
|
|
from pytest_mock import MockerFixture
|
|
from types import ModuleType
|
|
|
|
from ahriman.core.module_loader import _module, _modules, implementations
|
|
from ahriman.web.views.base import BaseView
|
|
|
|
|
|
def test_implementations(resource_path_root: Path) -> None:
|
|
"""
|
|
must load implementations from the package
|
|
"""
|
|
views_root = resource_path_root / ".." / ".." / "src" / "ahriman" / "web" / "views"
|
|
routes = list(implementations(views_root, "ahriman.web.views", BaseView))
|
|
assert routes
|
|
assert all(isinstance(view, type) for view in routes)
|
|
assert all(issubclass(view, BaseView) for view in routes)
|
|
|
|
|
|
def test_module(mocker: MockerFixture) -> None:
|
|
"""
|
|
must load module
|
|
"""
|
|
exec_mock = mocker.patch("importlib.machinery.SourceFileLoader.exec_module")
|
|
module_info = next(_modules(Path(__file__).parent, "ahriman.web.views"))
|
|
|
|
module = _module(module_info)
|
|
assert isinstance(module, ModuleType)
|
|
exec_mock.assert_called_once_with(pytest.helpers.anyvar(int))
|
|
|
|
|
|
def test_module_no_spec(mocker: MockerFixture) -> None:
|
|
"""
|
|
must raise ValueError if spec is not available
|
|
"""
|
|
mocker.patch("importlib.machinery.FileFinder.find_spec", return_value=None)
|
|
module_info = next(_modules(Path(__file__).parent, "ahriman.web.views"))
|
|
|
|
with pytest.raises(ValueError):
|
|
_module(module_info)
|
|
|
|
|
|
def test_module_no_loader(mocker: MockerFixture) -> None:
|
|
"""
|
|
must raise ValueError if loader is not available
|
|
"""
|
|
mocker.patch("importlib.machinery.FileFinder.find_spec", return_value=ModuleSpec("name", None))
|
|
module_info = next(_modules(Path(__file__).parent, "ahriman.web.views"))
|
|
|
|
with pytest.raises(ValueError):
|
|
_module(module_info)
|
|
|
|
|
|
def test_modules() -> None:
|
|
"""
|
|
must load modules
|
|
"""
|
|
modules = list(_modules(Path(__file__).parent.parent, "ahriman.web.views"))
|
|
assert modules
|
|
assert all(not module.ispkg for module in modules)
|