mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-16 07:19:57 +00:00
add models tests (#1)
also replace single quote to double one to confort PEP docstring + move _check_output to class properties to make it available for mocking
This commit is contained in:
79
tests/ahriman/models/conftest.py
Normal file
79
tests/ahriman/models/conftest.py
Normal file
@ -0,0 +1,79 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.package_desciption import PackageDescription
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def build_status_failed() -> BuildStatus:
|
||||
return BuildStatus(BuildStatusEnum.Failed, 42)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_ahriman(package_description_ahriman: PackageDescription) -> Package:
|
||||
packages = {"ahriman": package_description_ahriman}
|
||||
return Package(
|
||||
base="ahriman",
|
||||
version="0.12.1-1",
|
||||
aur_url="https://aur.archlinux.org",
|
||||
packages=packages)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_python_schedule(
|
||||
package_description_python_schedule: PackageDescription,
|
||||
package_description_python2_schedule: PackageDescription) -> Package:
|
||||
packages = {
|
||||
"python-schedule": package_description_python_schedule,
|
||||
"python2-schedule": package_description_python2_schedule
|
||||
}
|
||||
return Package(
|
||||
base="python-schedule",
|
||||
version="1.0.0-2",
|
||||
aur_url="https://aur.archlinux.org",
|
||||
packages=packages)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_tpacpi_bat_git() -> Package:
|
||||
return Package(
|
||||
base="tpacpi-bat-git",
|
||||
version="3.1.r12.g4959b52-1",
|
||||
aur_url="https://aur.archlinux.org",
|
||||
packages={"tpacpi-bat-git": PackageDescription()})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_description_ahriman() -> PackageDescription:
|
||||
return PackageDescription(
|
||||
archive_size=4200,
|
||||
build_date=42,
|
||||
filename="ahriman-0.12.1-1-any.pkg.tar.zst",
|
||||
installed_size=4200000)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_description_python_schedule() -> PackageDescription:
|
||||
return PackageDescription(
|
||||
archive_size=4201,
|
||||
build_date=421,
|
||||
filename="python-schedule-1.0.0-2-any.pkg.tar.zst",
|
||||
installed_size=4200001)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_description_python2_schedule() -> PackageDescription:
|
||||
return PackageDescription(
|
||||
archive_size=4202,
|
||||
build_date=422,
|
||||
filename="python2-schedule-1.0.0-2-any.pkg.tar.zst",
|
||||
installed_size=4200002)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def repository_paths() -> RepositoryPaths:
|
||||
return RepositoryPaths(
|
||||
architecture="x86_64",
|
||||
root="/var/lib/ahriman")
|
38
tests/ahriman/models/test_build_status.py
Normal file
38
tests/ahriman/models/test_build_status.py
Normal file
@ -0,0 +1,38 @@
|
||||
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
|
||||
|
||||
|
||||
def test_build_status_enum_badges_color() -> None:
|
||||
"""
|
||||
status color must be one of shields.io supported
|
||||
"""
|
||||
SUPPORTED_COLORS = [
|
||||
"brightgreen", "green", "yellowgreen", "yellow", "orange", "red", "blue", "lightgrey",
|
||||
"success", "important", "critical", "informational", "inactive", "blueviolet"
|
||||
]
|
||||
|
||||
for status in BuildStatusEnum:
|
||||
assert status.badges_color() in SUPPORTED_COLORS
|
||||
|
||||
|
||||
def test_build_status_init_1() -> None:
|
||||
"""
|
||||
must construct status object from None
|
||||
"""
|
||||
status = BuildStatus()
|
||||
assert status.status == BuildStatusEnum.Unknown
|
||||
assert status.timestamp > 0
|
||||
|
||||
|
||||
def test_build_status_init_2(build_status_failed: BuildStatus) -> None:
|
||||
"""
|
||||
must construct status object from objects
|
||||
"""
|
||||
status = BuildStatus(BuildStatusEnum.Failed, 42)
|
||||
assert status == build_status_failed
|
||||
|
||||
|
||||
def test_build_status_from_json_view(build_status_failed: BuildStatus) -> None:
|
||||
"""
|
||||
must construct same object from json
|
||||
"""
|
||||
assert BuildStatus.from_json(build_status_failed.view()) == build_status_failed
|
109
tests/ahriman/models/test_package.py
Normal file
109
tests/ahriman/models/test_package.py
Normal file
@ -0,0 +1,109 @@
|
||||
from pathlib import Path
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
|
||||
def test_git_url(package_ahriman: Package) -> None:
|
||||
"""
|
||||
must generate valid git url
|
||||
"""
|
||||
assert package_ahriman.git_url.endswith(".git")
|
||||
assert package_ahriman.git_url.startswith(package_ahriman.aur_url)
|
||||
assert package_ahriman.base in package_ahriman.git_url
|
||||
|
||||
|
||||
def test_is_single_package_false(package_python_schedule: Package) -> None:
|
||||
"""
|
||||
python-schedule must not be single package
|
||||
"""
|
||||
assert not package_python_schedule.is_single_package
|
||||
|
||||
|
||||
def test_is_single_package_true(package_ahriman: Package) -> None:
|
||||
"""
|
||||
ahriman must be single package
|
||||
"""
|
||||
assert package_ahriman.is_single_package
|
||||
|
||||
|
||||
def test_is_vcs_false(package_ahriman: Package) -> None:
|
||||
"""
|
||||
ahriman must not be VCS package
|
||||
"""
|
||||
assert not package_ahriman.is_vcs
|
||||
|
||||
|
||||
def test_is_vcs_true(package_tpacpi_bat_git: Package) -> None:
|
||||
"""
|
||||
tpacpi-bat-git must be VCS package
|
||||
"""
|
||||
assert package_tpacpi_bat_git.is_vcs
|
||||
|
||||
|
||||
def test_web_url(package_ahriman: Package) -> None:
|
||||
"""
|
||||
must generate valid web url
|
||||
"""
|
||||
assert package_ahriman.web_url.startswith(package_ahriman.aur_url)
|
||||
assert package_ahriman.base in package_ahriman.web_url
|
||||
|
||||
|
||||
def test_from_json_view_1(package_ahriman: Package) -> None:
|
||||
"""
|
||||
must construct same object from json
|
||||
"""
|
||||
assert Package.from_json(package_ahriman.view()) == package_ahriman
|
||||
|
||||
|
||||
def test_from_json_view_2(package_python_schedule: Package) -> None:
|
||||
"""
|
||||
must construct same object from json
|
||||
"""
|
||||
assert Package.from_json(package_python_schedule.view()) == package_python_schedule
|
||||
|
||||
|
||||
def test_from_json_view_3(package_tpacpi_bat_git: Package) -> None:
|
||||
"""
|
||||
must construct same object from json
|
||||
"""
|
||||
assert Package.from_json(package_tpacpi_bat_git.view()) == package_tpacpi_bat_git
|
||||
|
||||
|
||||
def test_actual_version(package_ahriman: Package, repository_paths: RepositoryPaths) -> None:
|
||||
"""
|
||||
must return same actual_version as version is
|
||||
"""
|
||||
assert package_ahriman.actual_version(repository_paths) == package_ahriman.version
|
||||
|
||||
|
||||
def test_actual_version_vcs(package_tpacpi_bat_git: Package, repository_paths: RepositoryPaths,
|
||||
mocker: MockerFixture, resource_path_root: Path) -> None:
|
||||
"""
|
||||
must return valid actual_version for VCS package
|
||||
"""
|
||||
srcinfo = (resource_path_root / "models" / "package_tpacpi-bat-git_srcinfo").read_text()
|
||||
|
||||
mocker.patch("ahriman.models.package.Package._check_output", return_value=srcinfo)
|
||||
mocker.patch("ahriman.core.build_tools.task.Task.fetch", return_value=None)
|
||||
|
||||
assert package_tpacpi_bat_git.actual_version(repository_paths) == "3.1.r13.g4959b52-1"
|
||||
|
||||
|
||||
def test_is_outdated_false(package_ahriman: Package, repository_paths: RepositoryPaths) -> None:
|
||||
"""
|
||||
must be not outdated for the same package
|
||||
"""
|
||||
assert not package_ahriman.is_outdated(package_ahriman, repository_paths)
|
||||
|
||||
|
||||
def test_is_outdated_true(package_ahriman: Package, repository_paths: RepositoryPaths) -> None:
|
||||
"""
|
||||
must be outdated for the new version
|
||||
"""
|
||||
other = Package.from_json(package_ahriman.view())
|
||||
other.version = other.version.replace("-1", "-2")
|
||||
|
||||
assert package_ahriman.is_outdated(other, repository_paths)
|
20
tests/ahriman/models/test_report_settings.py
Normal file
20
tests/ahriman/models/test_report_settings.py
Normal file
@ -0,0 +1,20 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.exceptions import InvalidOption
|
||||
from ahriman.models.report_settings import ReportSettings
|
||||
|
||||
|
||||
def test_from_option_invalid() -> None:
|
||||
"""
|
||||
must raise exception on invalid option
|
||||
"""
|
||||
with pytest.raises(InvalidOption, match=".* `invalid`$"):
|
||||
ReportSettings.from_option("invalid")
|
||||
|
||||
|
||||
def test_from_option_valid() -> None:
|
||||
"""
|
||||
must return value from valid options
|
||||
"""
|
||||
assert ReportSettings.from_option("html") == ReportSettings.HTML
|
||||
assert ReportSettings.from_option("HTML") == ReportSettings.HTML
|
25
tests/ahriman/models/test_repository_paths.py
Normal file
25
tests/ahriman/models/test_repository_paths.py
Normal file
@ -0,0 +1,25 @@
|
||||
import os
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest import mock
|
||||
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
|
||||
def test_create_tree(repository_paths: RepositoryPaths, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must create whole tree
|
||||
"""
|
||||
paths = {
|
||||
property
|
||||
for property in dir(repository_paths)
|
||||
if not property.startswith("_") and property not in ("architecture", "create_tree", "root")
|
||||
}
|
||||
mocker.patch("os.makedirs")
|
||||
|
||||
repository_paths.create_tree()
|
||||
os.makedirs.assert_has_calls(
|
||||
[
|
||||
mock.call(getattr(repository_paths, path), mode=0o755, exist_ok=True)
|
||||
for path in paths
|
||||
], any_order=True)
|
26
tests/ahriman/models/test_sign_settings.py
Normal file
26
tests/ahriman/models/test_sign_settings.py
Normal file
@ -0,0 +1,26 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.exceptions import InvalidOption
|
||||
from ahriman.models.sign_settings import SignSettings
|
||||
|
||||
|
||||
def test_from_option_invalid() -> None:
|
||||
"""
|
||||
must raise exception on invalid option
|
||||
"""
|
||||
with pytest.raises(InvalidOption, match=".* `invalid`$"):
|
||||
SignSettings.from_option("invalid")
|
||||
|
||||
|
||||
def test_from_option_valid() -> None:
|
||||
"""
|
||||
must return value from valid options
|
||||
"""
|
||||
assert SignSettings.from_option("package") == SignSettings.SignPackages
|
||||
assert SignSettings.from_option("PACKAGE") == SignSettings.SignPackages
|
||||
assert SignSettings.from_option("packages") == SignSettings.SignPackages
|
||||
assert SignSettings.from_option("sign-package") == SignSettings.SignPackages
|
||||
|
||||
assert SignSettings.from_option("repository") == SignSettings.SignRepository
|
||||
assert SignSettings.from_option("REPOSITORY") == SignSettings.SignRepository
|
||||
assert SignSettings.from_option("sign-repository") == SignSettings.SignRepository
|
23
tests/ahriman/models/test_upload_settings.py
Normal file
23
tests/ahriman/models/test_upload_settings.py
Normal file
@ -0,0 +1,23 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.exceptions import InvalidOption
|
||||
from ahriman.models.upload_settings import UploadSettings
|
||||
|
||||
|
||||
def test_from_option_invalid() -> None:
|
||||
"""
|
||||
must raise exception on invalid option
|
||||
"""
|
||||
with pytest.raises(InvalidOption, match=".* `invalid`$"):
|
||||
UploadSettings.from_option("invalid")
|
||||
|
||||
|
||||
def test_from_option_valid() -> None:
|
||||
"""
|
||||
must return value from valid options
|
||||
"""
|
||||
assert UploadSettings.from_option("rsync") == UploadSettings.Rsync
|
||||
assert UploadSettings.from_option("RSYNC") == UploadSettings.Rsync
|
||||
|
||||
assert UploadSettings.from_option("s3") == UploadSettings.S3
|
||||
assert UploadSettings.from_option("S3") == UploadSettings.S3
|
0
tests/conftest.py
Normal file
0
tests/conftest.py
Normal file
17
tests/testresources/models/package_tpacpi-bat-git_srcinfo
Normal file
17
tests/testresources/models/package_tpacpi-bat-git_srcinfo
Normal file
@ -0,0 +1,17 @@
|
||||
pkgbase = tpacpi-bat-git
|
||||
pkgdesc = A Perl script with ACPI calls for recent ThinkPads which are not supported by tp_smapi
|
||||
pkgver = 3.1.r13.g4959b52
|
||||
pkgrel = 1
|
||||
url = https://github.com/teleshoes/tpacpi-bat
|
||||
arch = any
|
||||
license = GPL3
|
||||
makedepends = git
|
||||
depends = perl
|
||||
depends = acpi_call
|
||||
provides = tpacpi-bat
|
||||
conflicts = tpacpi-bat
|
||||
backup = etc/conf.d/tpacpi
|
||||
source = git+https://github.com/teleshoes/tpacpi-bat.git
|
||||
b2sums = SKIP
|
||||
|
||||
pkgname = tpacpi-bat-git
|
Reference in New Issue
Block a user