mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 23:37:18 +00:00
base web tests
This commit is contained in:
parent
a5716fa8a8
commit
2c90efc339
1
setup.py
1
setup.py
@ -36,6 +36,7 @@ setup(
|
|||||||
],
|
],
|
||||||
tests_require=[
|
tests_require=[
|
||||||
"pytest",
|
"pytest",
|
||||||
|
"pytest-asyncio",
|
||||||
"pytest-cov",
|
"pytest-cov",
|
||||||
"pytest-helpers-namespace",
|
"pytest-helpers-namespace",
|
||||||
"pytest-mock",
|
"pytest-mock",
|
||||||
|
@ -40,4 +40,4 @@ class Web(Handler):
|
|||||||
"""
|
"""
|
||||||
from ahriman.web.web import run_server, setup_service
|
from ahriman.web.web import run_server, setup_service
|
||||||
application = setup_service(architecture, config)
|
application = setup_service(architecture, config)
|
||||||
run_server(application, architecture)
|
run_server(application)
|
||||||
|
@ -51,15 +51,14 @@ async def on_startup(application: web.Application) -> None:
|
|||||||
raise InitializeException()
|
raise InitializeException()
|
||||||
|
|
||||||
|
|
||||||
def run_server(application: web.Application, architecture: str) -> None:
|
def run_server(application: web.Application) -> None:
|
||||||
"""
|
"""
|
||||||
run web application
|
run web application
|
||||||
:param application: web application instance
|
:param application: web application instance
|
||||||
:param architecture: repository architecture
|
|
||||||
"""
|
"""
|
||||||
application.logger.info("start server")
|
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")
|
host = application["config"].get(section, "host")
|
||||||
port = application["config"].getint(section, "port")
|
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.logger.info("setup configuration")
|
||||||
application["config"] = config
|
application["config"] = config
|
||||||
|
application["architecture"] = architecture
|
||||||
|
|
||||||
application.logger.info("setup watcher")
|
application.logger.info("setup watcher")
|
||||||
application["watcher"] = Watcher(architecture, config)
|
application["watcher"] = Watcher(architecture, config)
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from pytest_mock import MockerFixture
|
||||||
from typing import Any, Type, TypeVar
|
from typing import Any, Type, TypeVar
|
||||||
|
|
||||||
from ahriman.core.configuration import Configuration
|
from ahriman.core.configuration import Configuration
|
||||||
|
from ahriman.core.status.watcher import Watcher
|
||||||
from ahriman.models.package import Package
|
from ahriman.models.package import Package
|
||||||
from ahriman.models.package_desciption import PackageDescription
|
from ahriman.models.package_desciption import PackageDescription
|
||||||
from ahriman.models.repository_paths import RepositoryPaths
|
from ahriman.models.repository_paths import RepositoryPaths
|
||||||
@ -85,3 +87,9 @@ def repository_paths() -> RepositoryPaths:
|
|||||||
return RepositoryPaths(
|
return RepositoryPaths(
|
||||||
architecture="x86_64",
|
architecture="x86_64",
|
||||||
root=Path("/var/lib/ahriman"))
|
root=Path("/var/lib/ahriman"))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def watcher(configuration: Configuration, mocker: MockerFixture) -> Watcher:
|
||||||
|
mocker.patch("pathlib.Path.mkdir")
|
||||||
|
return Watcher("x86_64", configuration)
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pytest_mock import MockerFixture
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from ahriman.core.configuration import Configuration
|
|
||||||
from ahriman.core.status.client import Client
|
from ahriman.core.status.client import Client
|
||||||
from ahriman.core.status.watcher import Watcher
|
|
||||||
from ahriman.core.status.web_client import WebClient
|
from ahriman.core.status.web_client import WebClient
|
||||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||||
from ahriman.models.package import Package
|
from ahriman.models.package import Package
|
||||||
@ -28,12 +25,6 @@ def client() -> Client:
|
|||||||
return Client()
|
return Client()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def watcher(configuration: Configuration, mocker: MockerFixture) -> Watcher:
|
|
||||||
mocker.patch("pathlib.Path.mkdir")
|
|
||||||
return Watcher("x86_64", configuration)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def web_client() -> WebClient:
|
def web_client() -> WebClient:
|
||||||
return WebClient("localhost", 8080)
|
return WebClient("localhost", 8080)
|
||||||
|
13
tests/ahriman/web/conftest.py
Normal file
13
tests/ahriman/web/conftest.py
Normal file
@ -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)
|
0
tests/ahriman/web/test_routes.py
Normal file
0
tests/ahriman/web/test_routes.py
Normal file
47
tests/ahriman/web/test_web.py
Normal file
47
tests/ahriman/web/test_web.py
Normal file
@ -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))
|
Loading…
Reference in New Issue
Block a user