From 50af309c80c6513b07f569ce71bd5318cd0c6f2b Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Wed, 11 Aug 2021 01:51:23 +0300 Subject: [PATCH] add docstrings for every fixture and test methods also add tests for missing components --- .github/workflows/run-tests.yml | 2 +- tests/ahriman/application/conftest.py | 25 +++++++ .../application/handlers/test_handler_add.py | 5 ++ .../handlers/test_handler_clean.py | 5 ++ .../handlers/test_handler_key_import.py | 5 ++ .../handlers/test_handler_rebuild.py | 5 ++ .../handlers/test_handler_remove.py | 5 ++ .../handlers/test_handler_report.py | 5 ++ .../handlers/test_handler_search.py | 5 ++ .../handlers/test_handler_setup.py | 5 ++ .../application/handlers/test_handler_sign.py | 5 ++ .../handlers/test_handler_status.py | 5 ++ .../handlers/test_handler_status_update.py | 5 ++ .../application/handlers/test_handler_sync.py | 5 ++ .../handlers/test_handler_update.py | 5 ++ tests/ahriman/conftest.py | 73 +++++++++++++++++++ tests/ahriman/core/conftest.py | 28 +++++++ tests/ahriman/core/repository/conftest.py | 29 ++++++++ tests/ahriman/core/repository/test_cleaner.py | 7 ++ tests/ahriman/core/sign/conftest.py | 10 +++ tests/ahriman/core/status/conftest.py | 18 +++++ tests/ahriman/core/upload/conftest.py | 9 +++ tests/ahriman/models/conftest.py | 32 ++++++++ tests/ahriman/web/conftest.py | 6 ++ tests/ahriman/web/middlewares/conftest.py | 15 ++++ .../web/middlewares/test_exception_handler.py | 48 ++++++++++++ tests/ahriman/web/test_routes.py | 11 +++ tests/ahriman/web/views/conftest.py | 8 ++ 28 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 tests/ahriman/web/middlewares/conftest.py diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 81f79bde..6dc89e5d 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,4 +1,4 @@ -name: tests +name: tests on: push: diff --git a/tests/ahriman/application/conftest.py b/tests/ahriman/application/conftest.py index 8b266ee0..2dc50ec4 100644 --- a/tests/ahriman/application/conftest.py +++ b/tests/ahriman/application/conftest.py @@ -13,17 +13,32 @@ from ahriman.models.package import Package @pytest.fixture def application(configuration: Configuration, mocker: MockerFixture) -> Application: + """ + fixture for application + :param configuration: configuration fixture + :param mocker: mocker object + :return: application test instance + """ mocker.patch("pathlib.Path.mkdir") return Application("x86_64", configuration) @pytest.fixture def args() -> argparse.Namespace: + """ + fixture for command line arguments + :return: command line arguments test instance + """ return argparse.Namespace(lock=None, force=False, unsafe=False, no_report=True) @pytest.fixture def aur_package_ahriman(package_ahriman: Package) -> aur.Package: + """ + fixture for AUR package + :param package_ahriman: package fixture + :return: AUR package test instance + """ return aur.Package( num_votes=None, description=package_ahriman.packages[package_ahriman.base].description, @@ -44,9 +59,19 @@ def aur_package_ahriman(package_ahriman: Package) -> aur.Package: @pytest.fixture def lock(args: argparse.Namespace, configuration: Configuration) -> Lock: + """ + fixture for file lock + :param args: command line arguments fixture + :param configuration: configuration fixture + :return: file lock test instance + """ return Lock(args, "x86_64", configuration) @pytest.fixture def parser() -> argparse.ArgumentParser: + """ + fixture for command line arguments parser + :return: command line arguments parser test instance + """ return _parser() diff --git a/tests/ahriman/application/handlers/test_handler_add.py b/tests/ahriman/application/handlers/test_handler_add.py index 66a1c19c..25ab8d64 100644 --- a/tests/ahriman/application/handlers/test_handler_add.py +++ b/tests/ahriman/application/handlers/test_handler_add.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.package = [] args.now = False args.without_dependencies = False diff --git a/tests/ahriman/application/handlers/test_handler_clean.py b/tests/ahriman/application/handlers/test_handler_clean.py index db2c0e06..ca1edf44 100644 --- a/tests/ahriman/application/handlers/test_handler_clean.py +++ b/tests/ahriman/application/handlers/test_handler_clean.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.no_build = False args.no_cache = False args.no_chroot = False diff --git a/tests/ahriman/application/handlers/test_handler_key_import.py b/tests/ahriman/application/handlers/test_handler_key_import.py index 18afdd33..702ddf88 100644 --- a/tests/ahriman/application/handlers/test_handler_key_import.py +++ b/tests/ahriman/application/handlers/test_handler_key_import.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.key = "0xE989490C" args.key_server = "keys.gnupg.net" return args diff --git a/tests/ahriman/application/handlers/test_handler_rebuild.py b/tests/ahriman/application/handlers/test_handler_rebuild.py index a3d3bbab..32dfcc55 100644 --- a/tests/ahriman/application/handlers/test_handler_rebuild.py +++ b/tests/ahriman/application/handlers/test_handler_rebuild.py @@ -8,6 +8,11 @@ from ahriman.models.package import Package def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.depends_on = [] return args diff --git a/tests/ahriman/application/handlers/test_handler_remove.py b/tests/ahriman/application/handlers/test_handler_remove.py index df990cbc..b64f9ccf 100644 --- a/tests/ahriman/application/handlers/test_handler_remove.py +++ b/tests/ahriman/application/handlers/test_handler_remove.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.package = [] return args diff --git a/tests/ahriman/application/handlers/test_handler_report.py b/tests/ahriman/application/handlers/test_handler_report.py index 7deaa60d..5f366038 100644 --- a/tests/ahriman/application/handlers/test_handler_report.py +++ b/tests/ahriman/application/handlers/test_handler_report.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.target = [] return args diff --git a/tests/ahriman/application/handlers/test_handler_search.py b/tests/ahriman/application/handlers/test_handler_search.py index d9723f4f..68c432f7 100644 --- a/tests/ahriman/application/handlers/test_handler_search.py +++ b/tests/ahriman/application/handlers/test_handler_search.py @@ -8,6 +8,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.search = ["ahriman"] return args diff --git a/tests/ahriman/application/handlers/test_handler_setup.py b/tests/ahriman/application/handlers/test_handler_setup.py index 365952a2..dc0c6e00 100644 --- a/tests/ahriman/application/handlers/test_handler_setup.py +++ b/tests/ahriman/application/handlers/test_handler_setup.py @@ -11,6 +11,11 @@ from ahriman.models.sign_settings import SignSettings def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.build_command = "ahriman" args.from_configuration = Path("/usr/share/devtools/pacman-extra.conf") args.no_multilib = False diff --git a/tests/ahriman/application/handlers/test_handler_sign.py b/tests/ahriman/application/handlers/test_handler_sign.py index 0b16f01a..bd8e73b6 100644 --- a/tests/ahriman/application/handlers/test_handler_sign.py +++ b/tests/ahriman/application/handlers/test_handler_sign.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.package = [] return args diff --git a/tests/ahriman/application/handlers/test_handler_status.py b/tests/ahriman/application/handlers/test_handler_status.py index be3240d7..143028ff 100644 --- a/tests/ahriman/application/handlers/test_handler_status.py +++ b/tests/ahriman/application/handlers/test_handler_status.py @@ -9,6 +9,11 @@ from ahriman.models.package import Package def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.ahriman = True args.package = [] return args diff --git a/tests/ahriman/application/handlers/test_handler_status_update.py b/tests/ahriman/application/handlers/test_handler_status_update.py index 7dce4efd..4b4cc399 100644 --- a/tests/ahriman/application/handlers/test_handler_status_update.py +++ b/tests/ahriman/application/handlers/test_handler_status_update.py @@ -9,6 +9,11 @@ from ahriman.models.package import Package def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.status = BuildStatusEnum.Success args.package = None args.remove = False diff --git a/tests/ahriman/application/handlers/test_handler_sync.py b/tests/ahriman/application/handlers/test_handler_sync.py index 28dac940..660d71fb 100644 --- a/tests/ahriman/application/handlers/test_handler_sync.py +++ b/tests/ahriman/application/handlers/test_handler_sync.py @@ -7,6 +7,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.target = [] return args diff --git a/tests/ahriman/application/handlers/test_handler_update.py b/tests/ahriman/application/handlers/test_handler_update.py index fd3a9094..6861f034 100644 --- a/tests/ahriman/application/handlers/test_handler_update.py +++ b/tests/ahriman/application/handlers/test_handler_update.py @@ -8,6 +8,11 @@ from ahriman.core.configuration import Configuration def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ args.package = [] args.dry_run = False args.no_aur = False diff --git a/tests/ahriman/conftest.py b/tests/ahriman/conftest.py index bed81b50..309a9e77 100644 --- a/tests/ahriman/conftest.py +++ b/tests/ahriman/conftest.py @@ -1,3 +1,5 @@ +from unittest.mock import MagicMock + import pytest from pathlib import Path @@ -10,6 +12,7 @@ from ahriman.models.package import Package from ahriman.models.package_description import PackageDescription from ahriman.models.repository_paths import RepositoryPaths + T = TypeVar("T") @@ -17,21 +20,63 @@ T = TypeVar("T") # https://stackoverflow.com/a/21611963 @pytest.helpers.register def anyvar(cls: Type[T], strict: bool = False) -> T: + """ + any value helper for mocker calls check + :param cls: type class + :param strict: if True then check type of supplied argument + :return: any wrapper + """ class AnyVar(cls): + """ + any value wrapper + """ + def __eq__(self, other: Any) -> bool: + """ + compare object to other + :param other: other object to compare + :return: True in case if objects are equal + """ return not strict or isinstance(other, cls) + return AnyVar() +@pytest.helpers.register +class AsyncMock(MagicMock): + """ + async magic mock object + """ + + async def __call__(self, *args: Any, **kwargs: Any) -> Any: + """ + async call function + :param args: + :param kwargs: + :return: + """ + return MagicMock.__call__(self, *args, **kwargs) + + # generic fixtures @pytest.fixture def configuration(resource_path_root: Path) -> Configuration: + """ + configuration fixture + :param resource_path_root: resource path root directory + :return: configuration test instance + """ path = resource_path_root / "core" / "ahriman.ini" return Configuration.from_path(path=path, architecture="x86_64", logfile=False) @pytest.fixture def package_ahriman(package_description_ahriman: PackageDescription) -> Package: + """ + package fixture + :param package_description_ahriman: description fixture + :return: package test instance + """ packages = {"ahriman": package_description_ahriman} return Package( base="ahriman", @@ -44,6 +89,12 @@ def package_ahriman(package_description_ahriman: PackageDescription) -> Package: def package_python_schedule( package_description_python_schedule: PackageDescription, package_description_python2_schedule: PackageDescription) -> Package: + """ + multi package fixture + :param package_description_python_schedule: description fixture + :param package_description_python2_schedule: description fixture + :return: multi package test instance + """ packages = { "python-schedule": package_description_python_schedule, "python2-schedule": package_description_python2_schedule @@ -57,6 +108,10 @@ def package_python_schedule( @pytest.fixture def package_description_ahriman() -> PackageDescription: + """ + package description fixture + :return: package description test instance + """ return PackageDescription( architecture="x86_64", archive_size=4200, @@ -72,6 +127,10 @@ def package_description_ahriman() -> PackageDescription: @pytest.fixture def package_description_python_schedule() -> PackageDescription: + """ + package description fixture + :return: package description test instance + """ return PackageDescription( architecture="x86_64", archive_size=4201, @@ -87,6 +146,10 @@ def package_description_python_schedule() -> PackageDescription: @pytest.fixture def package_description_python2_schedule() -> PackageDescription: + """ + package description fixture + :return: package description test instance + """ return PackageDescription( architecture="x86_64", archive_size=4202, @@ -102,6 +165,10 @@ def package_description_python2_schedule() -> PackageDescription: @pytest.fixture def repository_paths(configuration: Configuration) -> RepositoryPaths: + """ + repository paths fixture + :return: repository paths test instance + """ return RepositoryPaths( architecture="x86_64", root=configuration.getpath("repository", "root")) @@ -109,5 +176,11 @@ def repository_paths(configuration: Configuration) -> RepositoryPaths: @pytest.fixture def watcher(configuration: Configuration, mocker: MockerFixture) -> Watcher: + """ + package status watcher fixture + :param configuration: configuration fixture + :param mocker: mocker object + :return: package status watcher test instance + """ mocker.patch("pathlib.Path.mkdir") return Watcher("x86_64", configuration) diff --git a/tests/ahriman/core/conftest.py b/tests/ahriman/core/conftest.py index 6458232d..3c522c86 100644 --- a/tests/ahriman/core/conftest.py +++ b/tests/ahriman/core/conftest.py @@ -11,24 +11,52 @@ from ahriman.models.repository_paths import RepositoryPaths @pytest.fixture def leaf_ahriman(package_ahriman: Package) -> Leaf: + """ + fixture for tree leaf with package + :param package_ahriman: package fixture + :return: tree leaf test instance + """ return Leaf(package_ahriman, set()) @pytest.fixture def leaf_python_schedule(package_python_schedule: Package) -> Leaf: + """ + fixture for tree leaf with package + :param package_python_schedule: package fixture + :return: tree leaf test instance + """ return Leaf(package_python_schedule, set()) @pytest.fixture def pacman(configuration: Configuration) -> Pacman: + """ + fixture for pacman wrapper + :param configuration: configuration fixture + :return: pacman wrapper test instance + """ return Pacman(configuration) @pytest.fixture def repo(configuration: Configuration, repository_paths: RepositoryPaths) -> Repo: + """ + fixture for repository wrapper + :param configuration: configuration fixture + :param repository_paths: repository paths fixture + :return: repository wrapper test instance + """ return Repo(configuration.get("repository", "name"), repository_paths, []) @pytest.fixture def task_ahriman(package_ahriman: Package, configuration: Configuration, repository_paths: RepositoryPaths) -> Task: + """ + fixture for built task + :param package_ahriman: package fixture + :param configuration: configuration fixture + :param repository_paths: repository paths fixture + :return: built task test instance + """ return Task(package_ahriman, configuration, repository_paths) diff --git a/tests/ahriman/core/repository/conftest.py b/tests/ahriman/core/repository/conftest.py index 4a8e8e22..2b8986d6 100644 --- a/tests/ahriman/core/repository/conftest.py +++ b/tests/ahriman/core/repository/conftest.py @@ -12,12 +12,24 @@ from ahriman.core.repository.update_handler import UpdateHandler @pytest.fixture def cleaner(configuration: Configuration, mocker: MockerFixture) -> Cleaner: + """ + fixture for cleaner + :param configuration: configuration fixture + :param mocker: mocker object + :return: cleaner test instance + """ mocker.patch("pathlib.Path.mkdir") return Cleaner("x86_64", configuration) @pytest.fixture def executor(configuration: Configuration, mocker: MockerFixture) -> Executor: + """ + fixture for executor + :param configuration: configuration fixture + :param mocker: mocker object + :return: executor test instance + """ mocker.patch("pathlib.Path.mkdir") mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_build") mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_cache") @@ -29,17 +41,34 @@ def executor(configuration: Configuration, mocker: MockerFixture) -> Executor: @pytest.fixture def repository(configuration: Configuration, mocker: MockerFixture) -> Repository: + """ + fixture for repository + :param configuration: configuration fixture + :param mocker: mocker object + :return: repository test instance + """ mocker.patch("pathlib.Path.mkdir") return Repository("x86_64", configuration) @pytest.fixture def properties(configuration: Configuration) -> Properties: + """ + fixture for properties + :param configuration: configuration fixture + :return: properties test instance + """ return Properties("x86_64", configuration) @pytest.fixture def update_handler(configuration: Configuration, mocker: MockerFixture) -> UpdateHandler: + """ + fixture for update handler + :param configuration: configuration fixture + :param mocker: mocker object + :return: update handler test instance + """ mocker.patch("pathlib.Path.mkdir") mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_build") mocker.patch("ahriman.core.repository.cleaner.Cleaner.clear_cache") diff --git a/tests/ahriman/core/repository/test_cleaner.py b/tests/ahriman/core/repository/test_cleaner.py index f848fb8b..0cae5cb5 100644 --- a/tests/ahriman/core/repository/test_cleaner.py +++ b/tests/ahriman/core/repository/test_cleaner.py @@ -9,11 +9,18 @@ from ahriman.core.repository.cleaner import Cleaner def _mock_clear(mocker: MockerFixture) -> None: + """ + mocker helper for clear function + :param mocker: mocker object + """ mocker.patch("pathlib.Path.iterdir", return_value=[Path("a"), Path("b"), Path("c")]) mocker.patch("shutil.rmtree") def _mock_clear_check() -> None: + """ + mocker helper for clear tests + """ shutil.rmtree.assert_has_calls([ mock.call(Path("a")), mock.call(Path("b")), diff --git a/tests/ahriman/core/sign/conftest.py b/tests/ahriman/core/sign/conftest.py index 8bd3801b..6fb578ab 100644 --- a/tests/ahriman/core/sign/conftest.py +++ b/tests/ahriman/core/sign/conftest.py @@ -6,10 +6,20 @@ from ahriman.core.sign.gpg import GPG @pytest.fixture def gpg(configuration: Configuration) -> GPG: + """ + fixture for empty GPG + :param configuration: configuration fixture + :return: GPG test instance + """ return GPG("x86_64", configuration) @pytest.fixture def gpg_with_key(gpg: GPG) -> GPG: + """ + fixture for correct GPG + :param gpg: empty GPG fixture + :return: GPG test instance + """ gpg.default_key = "key" return gpg diff --git a/tests/ahriman/core/status/conftest.py b/tests/ahriman/core/status/conftest.py index a3bc3937..e12c2365 100644 --- a/tests/ahriman/core/status/conftest.py +++ b/tests/ahriman/core/status/conftest.py @@ -11,20 +11,38 @@ from ahriman.models.package import Package # helpers @pytest.helpers.register def get_package_status(package: Package) -> Dict[str, Any]: + """ + helper to extract package status from package + :param package: package object + :return: simplified package status map (with only status and view) + """ return {"status": BuildStatusEnum.Unknown.value, "package": package.view()} @pytest.helpers.register def get_package_status_extended(package: Package) -> Dict[str, Any]: + """ + helper to extract package status from package + :param package: package object + :return: full package status map (with timestamped build status and view) + """ return {"status": BuildStatus().view(), "package": package.view()} # fixtures @pytest.fixture def client() -> Client: + """ + fixture for dummy client + :return: dummy client test instance + """ return Client() @pytest.fixture def web_client() -> WebClient: + """ + fixture for web client + :return: web client test instance + """ return WebClient("localhost", 8080) diff --git a/tests/ahriman/core/upload/conftest.py b/tests/ahriman/core/upload/conftest.py index 0e490d8c..4fa9605a 100644 --- a/tests/ahriman/core/upload/conftest.py +++ b/tests/ahriman/core/upload/conftest.py @@ -13,10 +13,19 @@ _s3_object = namedtuple("s3_object", ["key", "e_tag", "delete"]) @pytest.fixture def s3(configuration: Configuration) -> S3: + """ + fixture for S3 synchronization + :param configuration: configuration fixture + :return: S3 test instance + """ return S3("x86_64", configuration) @pytest.fixture def s3_remote_objects() -> List[_s3_object]: + """ + fixture for boto3 like S3 objects + :return: boto3 like S3 objects test instance + """ delete_mock = MagicMock() return list(map(lambda item: _s3_object(f"x86_64/{item}", f"\"{item}\"", delete_mock), ["a", "b", "c"])) diff --git a/tests/ahriman/models/conftest.py b/tests/ahriman/models/conftest.py index 2d268858..8bd3e490 100644 --- a/tests/ahriman/models/conftest.py +++ b/tests/ahriman/models/conftest.py @@ -12,11 +12,19 @@ from ahriman.models.package_description import PackageDescription @pytest.fixture def build_status_failed() -> BuildStatus: + """ + build result fixture with failed status + :return: failed build status test instance + """ return BuildStatus(BuildStatusEnum.Failed, 42) @pytest.fixture def counters() -> Counters: + """ + counters fixture + :return: counters test instance + """ return Counters(total=10, unknown=1, pending=2, @@ -27,6 +35,11 @@ def counters() -> Counters: @pytest.fixture def internal_status(counters: Counters) -> InternalStatus: + """ + internal status fixture + :param counters: counters fixture + :return: internal status test instance + """ return InternalStatus(architecture="x86_64", packages=counters, version=version.__version__, @@ -35,6 +48,10 @@ def internal_status(counters: Counters) -> InternalStatus: @pytest.fixture def package_tpacpi_bat_git() -> Package: + """ + git package fixture + :return: git package test instance + """ return Package( base="tpacpi-bat-git", version="3.1.r12.g4959b52-1", @@ -44,6 +61,11 @@ def package_tpacpi_bat_git() -> Package: @pytest.fixture def pyalpm_handle(pyalpm_package_ahriman: MagicMock) -> MagicMock: + """ + mock object for pyalpm + :param pyalpm_package_ahriman: mock object for pyalpm package + :return: pyalpm mock + """ mock = MagicMock() mock.handle.load_pkg.return_value = pyalpm_package_ahriman return mock @@ -51,6 +73,11 @@ def pyalpm_handle(pyalpm_package_ahriman: MagicMock) -> MagicMock: @pytest.fixture def pyalpm_package_ahriman(package_ahriman: Package) -> MagicMock: + """ + mock object for pyalpm package + :param package_ahriman: package fixture + :return: pyalpm package mock + """ mock = MagicMock() type(mock).base = PropertyMock(return_value=package_ahriman.base) type(mock).name = PropertyMock(return_value=package_ahriman.base) @@ -60,6 +87,11 @@ def pyalpm_package_ahriman(package_ahriman: Package) -> MagicMock: @pytest.fixture def pyalpm_package_description_ahriman(package_description_ahriman: PackageDescription) -> MagicMock: + """ + mock object for pyalpm package description + :param package_description_ahriman: package description fixture + :return: pyalpm package description mock + """ mock = MagicMock() type(mock).arch = PropertyMock(return_value=package_description_ahriman.architecture) type(mock).builddate = PropertyMock(return_value=package_description_ahriman.build_date) diff --git a/tests/ahriman/web/conftest.py b/tests/ahriman/web/conftest.py index 2dc774d7..3e711022 100644 --- a/tests/ahriman/web/conftest.py +++ b/tests/ahriman/web/conftest.py @@ -9,5 +9,11 @@ from ahriman.web.web import setup_service @pytest.fixture def application(configuration: Configuration, mocker: MockerFixture) -> web.Application: + """ + application fixture + :param configuration: configuration fixture + :param mocker: mocker object + :return: application test instance + """ mocker.patch("pathlib.Path.mkdir") return setup_service("x86_64", configuration) diff --git a/tests/ahriman/web/middlewares/conftest.py b/tests/ahriman/web/middlewares/conftest.py new file mode 100644 index 00000000..ca968d97 --- /dev/null +++ b/tests/ahriman/web/middlewares/conftest.py @@ -0,0 +1,15 @@ +import pytest + +from collections import namedtuple + + +_request = namedtuple("_request", ["path"]) + + +@pytest.fixture +def aiohttp_request() -> _request: + """ + fixture for aiohttp like object + :return: aiohttp like request test instance + """ + return _request("path") diff --git a/tests/ahriman/web/middlewares/test_exception_handler.py b/tests/ahriman/web/middlewares/test_exception_handler.py index e69de29b..22e9aa3d 100644 --- a/tests/ahriman/web/middlewares/test_exception_handler.py +++ b/tests/ahriman/web/middlewares/test_exception_handler.py @@ -0,0 +1,48 @@ +import logging +import pytest + +from aiohttp.web_exceptions import HTTPBadRequest +from pytest_mock import MockerFixture +from typing import Any + +from ahriman.web.middlewares.exception_handler import exception_handler + + +async def test_exception_handler(aiohttp_request: Any, mocker: MockerFixture) -> None: + """ + must pass success response + """ + request_handler = pytest.helpers.AsyncMock() + logging_mock = mocker.patch("logging.Logger.exception") + + handler = exception_handler(logging.getLogger()) + await handler(aiohttp_request, request_handler) + logging_mock.assert_not_called() + + +async def test_exception_handler_client_error(aiohttp_request: Any, mocker: MockerFixture) -> None: + """ + must pass client exception + """ + request_handler = pytest.helpers.AsyncMock() + request_handler.side_effect = HTTPBadRequest() + logging_mock = mocker.patch("logging.Logger.exception") + + handler = exception_handler(logging.getLogger()) + with pytest.raises(HTTPBadRequest): + await handler(aiohttp_request, request_handler) + logging_mock.assert_not_called() + + +async def test_exception_handler_server_error(aiohttp_request: Any, mocker: MockerFixture) -> None: + """ + must log server exception and re-raise it + """ + request_handler = pytest.helpers.AsyncMock() + request_handler.side_effect = Exception() + logging_mock = mocker.patch("logging.Logger.exception") + + handler = exception_handler(logging.getLogger()) + with pytest.raises(Exception): + await handler(aiohttp_request, request_handler) + logging_mock.assert_called_once() diff --git a/tests/ahriman/web/test_routes.py b/tests/ahriman/web/test_routes.py index e69de29b..009344c4 100644 --- a/tests/ahriman/web/test_routes.py +++ b/tests/ahriman/web/test_routes.py @@ -0,0 +1,11 @@ +from aiohttp import web + +from ahriman.web.routes import setup_routes + + +def test_setup_routes(application: web.Application) -> None: + """ + must generate non empty list of routes + """ + setup_routes(application) + assert application.router.routes() diff --git a/tests/ahriman/web/views/conftest.py b/tests/ahriman/web/views/conftest.py index 31f3da77..29ec18d0 100644 --- a/tests/ahriman/web/views/conftest.py +++ b/tests/ahriman/web/views/conftest.py @@ -10,5 +10,13 @@ from typing import Any @pytest.fixture def client(application: web.Application, loop: BaseEventLoop, aiohttp_client: Any, mocker: MockerFixture) -> TestClient: + """ + web client fixture + :param application: application fixture + :param loop: context event loop + :param aiohttp_client: aiohttp client fixture + :param mocker: mocker object + :return: web client test instance + """ mocker.patch("pathlib.Path.iterdir", return_value=[]) return loop.run_until_complete(aiohttp_client(application))