From 2c90efc3396c4f13b37aeefbcd386b1454342d7d Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Sun, 28 Mar 2021 06:45:16 +0300 Subject: [PATCH] base web tests --- setup.py | 1 + src/ahriman/application/handlers/web.py | 2 +- src/ahriman/web/web.py | 6 +-- tests/ahriman/conftest.py | 8 ++++ tests/ahriman/core/status/conftest.py | 9 ---- tests/ahriman/web/conftest.py | 13 +++++ .../web/middlewares/test_exception_handler.py | 0 tests/ahriman/web/test_routes.py | 0 tests/ahriman/web/test_web.py | 47 +++++++++++++++++++ 9 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 tests/ahriman/web/conftest.py create mode 100644 tests/ahriman/web/middlewares/test_exception_handler.py create mode 100644 tests/ahriman/web/test_routes.py create mode 100644 tests/ahriman/web/test_web.py diff --git a/setup.py b/setup.py index 7a067673..264cbe5c 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,7 @@ setup( ], tests_require=[ "pytest", + "pytest-asyncio", "pytest-cov", "pytest-helpers-namespace", "pytest-mock", diff --git a/src/ahriman/application/handlers/web.py b/src/ahriman/application/handlers/web.py index f9f24672..ef94098a 100644 --- a/src/ahriman/application/handlers/web.py +++ b/src/ahriman/application/handlers/web.py @@ -40,4 +40,4 @@ class Web(Handler): """ from ahriman.web.web import run_server, setup_service application = setup_service(architecture, config) - run_server(application, architecture) + run_server(application) diff --git a/src/ahriman/web/web.py b/src/ahriman/web/web.py index e88c5140..ab8a679e 100644 --- a/src/ahriman/web/web.py +++ b/src/ahriman/web/web.py @@ -51,15 +51,14 @@ async def on_startup(application: web.Application) -> None: raise InitializeException() -def run_server(application: web.Application, architecture: str) -> None: +def run_server(application: web.Application) -> None: """ run web application :param application: web application instance - :param architecture: repository architecture """ application.logger.info("start server") - section = application["config"].get_section_name("web", architecture) + section = application["config"].get_section_name("web", application["architecture"]) host = application["config"].get(section, "host") port = application["config"].getint(section, "port") @@ -88,6 +87,7 @@ def setup_service(architecture: str, config: Configuration) -> web.Application: application.logger.info("setup configuration") application["config"] = config + application["architecture"] = architecture application.logger.info("setup watcher") application["watcher"] = Watcher(architecture, config) diff --git a/tests/ahriman/conftest.py b/tests/ahriman/conftest.py index 87c1d768..7d5ebd84 100644 --- a/tests/ahriman/conftest.py +++ b/tests/ahriman/conftest.py @@ -1,9 +1,11 @@ import pytest from pathlib import Path +from pytest_mock import MockerFixture from typing import Any, Type, TypeVar from ahriman.core.configuration import Configuration +from ahriman.core.status.watcher import Watcher from ahriman.models.package import Package from ahriman.models.package_desciption import PackageDescription from ahriman.models.repository_paths import RepositoryPaths @@ -85,3 +87,9 @@ def repository_paths() -> RepositoryPaths: return RepositoryPaths( architecture="x86_64", root=Path("/var/lib/ahriman")) + + +@pytest.fixture +def watcher(configuration: Configuration, mocker: MockerFixture) -> Watcher: + mocker.patch("pathlib.Path.mkdir") + return Watcher("x86_64", configuration) diff --git a/tests/ahriman/core/status/conftest.py b/tests/ahriman/core/status/conftest.py index 6a31f6d6..a3bc3937 100644 --- a/tests/ahriman/core/status/conftest.py +++ b/tests/ahriman/core/status/conftest.py @@ -1,11 +1,8 @@ import pytest -from pytest_mock import MockerFixture from typing import Any, Dict -from ahriman.core.configuration import Configuration from ahriman.core.status.client import Client -from ahriman.core.status.watcher import Watcher from ahriman.core.status.web_client import WebClient from ahriman.models.build_status import BuildStatus, BuildStatusEnum from ahriman.models.package import Package @@ -28,12 +25,6 @@ def client() -> Client: return Client() -@pytest.fixture -def watcher(configuration: Configuration, mocker: MockerFixture) -> Watcher: - mocker.patch("pathlib.Path.mkdir") - return Watcher("x86_64", configuration) - - @pytest.fixture def web_client() -> WebClient: return WebClient("localhost", 8080) diff --git a/tests/ahriman/web/conftest.py b/tests/ahriman/web/conftest.py new file mode 100644 index 00000000..2dc774d7 --- /dev/null +++ b/tests/ahriman/web/conftest.py @@ -0,0 +1,13 @@ +import pytest + +from aiohttp import web +from pytest_mock import MockerFixture + +from ahriman.core.configuration import Configuration +from ahriman.web.web import setup_service + + +@pytest.fixture +def application(configuration: Configuration, mocker: MockerFixture) -> web.Application: + mocker.patch("pathlib.Path.mkdir") + return setup_service("x86_64", configuration) diff --git a/tests/ahriman/web/middlewares/test_exception_handler.py b/tests/ahriman/web/middlewares/test_exception_handler.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/ahriman/web/test_routes.py b/tests/ahriman/web/test_routes.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/ahriman/web/test_web.py b/tests/ahriman/web/test_web.py new file mode 100644 index 00000000..5e42af99 --- /dev/null +++ b/tests/ahriman/web/test_web.py @@ -0,0 +1,47 @@ +import pytest + +from aiohttp import web +from pytest_mock import MockerFixture + +from ahriman.core.exceptions import InitializeException +from ahriman.core.status.watcher import Watcher +from ahriman.web.web import on_startup, run_server + + +@pytest.mark.asyncio +async def test_on_startup(application: web.Application, watcher: Watcher, mocker: MockerFixture) -> None: + """ + must call load method + """ + mocker.patch("aiohttp.web.Application.__getitem__", return_value=watcher) + load_mock = mocker.patch("ahriman.core.status.watcher.Watcher.load") + + await on_startup(application) + load_mock.assert_called_once() + + +@pytest.mark.asyncio +async def test_on_startup_exception(application: web.Application, watcher: Watcher, mocker: MockerFixture) -> None: + """ + must throw exception on load error + """ + mocker.patch("aiohttp.web.Application.__getitem__", return_value=watcher) + mocker.patch("ahriman.core.status.watcher.Watcher.load", side_effect=Exception()) + + with pytest.raises(InitializeException): + await on_startup(application) + + +def test_run(application: web.Application, mocker: MockerFixture) -> None: + """ + must run application + """ + host = "localhost" + port = 8080 + application["config"].set("web", "host", host) + application["config"].set("web", "port", str(port)) + run_app_mock = mocker.patch("aiohttp.web.run_app") + + run_server(application) + run_app_mock.assert_called_with(application, host=host, port=port, + handle_signals=False, access_log=pytest.helpers.anyvar(int))