mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 15:27:17 +00:00
add docstrings for every fixture and test methods
also add tests for missing components
This commit is contained in:
parent
581401d60f
commit
50af309c80
@ -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()
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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")
|
||||
|
@ -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")),
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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"]))
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
15
tests/ahriman/web/middlewares/conftest.py
Normal file
15
tests/ahriman/web/middlewares/conftest.py
Normal file
@ -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")
|
@ -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()
|
@ -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()
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user