mirror of
				https://github.com/arcan1s/ahriman.git
				synced 2025-11-04 07:43:42 +00:00 
			
		
		
		
	use own aur wrapper (#49)
This commit is contained in:
		@ -1,4 +1,3 @@
 | 
			
		||||
import aur
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from ahriman.application.formatters.aur_printer import AurPrinter
 | 
			
		||||
@ -7,12 +6,13 @@ from ahriman.application.formatters.package_printer import PackagePrinter
 | 
			
		||||
from ahriman.application.formatters.status_printer import StatusPrinter
 | 
			
		||||
from ahriman.application.formatters.string_printer import StringPrinter
 | 
			
		||||
from ahriman.application.formatters.update_printer import UpdatePrinter
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
from ahriman.models.build_status import BuildStatus
 | 
			
		||||
from ahriman.models.package import Package
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def aur_package_ahriman_printer(aur_package_ahriman: aur.Package) -> AurPrinter:
 | 
			
		||||
def aur_package_ahriman_printer(aur_package_ahriman: AURPackage) -> AurPrinter:
 | 
			
		||||
    """
 | 
			
		||||
    fixture for AUR package printer
 | 
			
		||||
    :param aur_package_ahriman: AUR package fixture
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,5 @@
 | 
			
		||||
import argparse
 | 
			
		||||
import aur
 | 
			
		||||
import dataclasses
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
@ -7,6 +7,7 @@ from pytest_mock import MockerFixture
 | 
			
		||||
from ahriman.application.handlers import Search
 | 
			
		||||
from ahriman.core.configuration import Configuration
 | 
			
		||||
from ahriman.core.exceptions import InvalidOption
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
 | 
			
		||||
@ -21,13 +22,13 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace:
 | 
			
		||||
    return args
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_run(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
 | 
			
		||||
def test_run(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: AURPackage,
 | 
			
		||||
             mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must run command
 | 
			
		||||
    """
 | 
			
		||||
    args = _default_args(args)
 | 
			
		||||
    search_mock = mocker.patch("ahriman.application.handlers.search.aur_search", return_value=[aur_package_ahriman])
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.multisearch", return_value=[aur_package_ahriman])
 | 
			
		||||
    print_mock = mocker.patch("ahriman.application.formatters.printer.Printer.print")
 | 
			
		||||
 | 
			
		||||
    Search.run(args, "x86_64", configuration, True)
 | 
			
		||||
@ -35,38 +36,38 @@ def test_run(args: argparse.Namespace, configuration: Configuration, aur_package
 | 
			
		||||
    print_mock.assert_called_once()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_run_sort(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
 | 
			
		||||
def test_run_sort(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: AURPackage,
 | 
			
		||||
                  mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must run command with sorting
 | 
			
		||||
    """
 | 
			
		||||
    args = _default_args(args)
 | 
			
		||||
    mocker.patch("ahriman.application.handlers.search.aur_search", return_value=[aur_package_ahriman])
 | 
			
		||||
    mocker.patch("ahriman.core.alpm.aur.AUR.multisearch", return_value=[aur_package_ahriman])
 | 
			
		||||
    sort_mock = mocker.patch("ahriman.application.handlers.search.Search.sort")
 | 
			
		||||
 | 
			
		||||
    Search.run(args, "x86_64", configuration, True)
 | 
			
		||||
    sort_mock.assert_called_once_with([aur_package_ahriman], "name")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_run_sort_by(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: aur.Package,
 | 
			
		||||
def test_run_sort_by(args: argparse.Namespace, configuration: Configuration, aur_package_ahriman: AURPackage,
 | 
			
		||||
                     mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must run command with sorting by specified field
 | 
			
		||||
    """
 | 
			
		||||
    args = _default_args(args)
 | 
			
		||||
    args.sort_by = "field"
 | 
			
		||||
    mocker.patch("ahriman.application.handlers.search.aur_search", return_value=[aur_package_ahriman])
 | 
			
		||||
    mocker.patch("ahriman.core.alpm.aur.AUR.multisearch", return_value=[aur_package_ahriman])
 | 
			
		||||
    sort_mock = mocker.patch("ahriman.application.handlers.search.Search.sort")
 | 
			
		||||
 | 
			
		||||
    Search.run(args, "x86_64", configuration, True)
 | 
			
		||||
    sort_mock.assert_called_once_with([aur_package_ahriman], "field")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_sort(aur_package_ahriman: aur.Package) -> None:
 | 
			
		||||
def test_sort(aur_package_ahriman: AURPackage) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must sort package list
 | 
			
		||||
    """
 | 
			
		||||
    another = aur_package_ahriman._replace(name="1", package_base="base")
 | 
			
		||||
    another = dataclasses.replace(aur_package_ahriman, name="1", package_base="base")
 | 
			
		||||
    # sort by name
 | 
			
		||||
    assert Search.sort([aur_package_ahriman, another], "name") == [another, aur_package_ahriman]
 | 
			
		||||
    # sort by another field
 | 
			
		||||
@ -75,7 +76,7 @@ def test_sort(aur_package_ahriman: aur.Package) -> None:
 | 
			
		||||
    assert Search.sort([aur_package_ahriman, another], "version") == [another, aur_package_ahriman]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_sort_exception(aur_package_ahriman: aur.Package) -> None:
 | 
			
		||||
def test_sort_exception(aur_package_ahriman: AURPackage) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must raise an exception on unknown sorting field
 | 
			
		||||
    """
 | 
			
		||||
@ -94,4 +95,5 @@ def test_sort_fields() -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must store valid field list which are allowed to be used for sorting
 | 
			
		||||
    """
 | 
			
		||||
    assert all(field in aur.Package._fields for field in Search.SORT_FIELDS)
 | 
			
		||||
    expected = {pair.name for pair in dataclasses.fields(AURPackage)}
 | 
			
		||||
    assert all(field in expected for field in Search.SORT_FIELDS)
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
import aur
 | 
			
		||||
import datetime
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
@ -10,6 +10,7 @@ from ahriman.core.auth.auth import Auth
 | 
			
		||||
from ahriman.core.configuration import Configuration
 | 
			
		||||
from ahriman.core.spawn import Spawn
 | 
			
		||||
from ahriman.core.status.watcher import Watcher
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
from ahriman.models.package import Package
 | 
			
		||||
from ahriman.models.package_description import PackageDescription
 | 
			
		||||
from ahriman.models.repository_paths import RepositoryPaths
 | 
			
		||||
@ -48,28 +49,56 @@ def anyvar(cls: Type[T], strict: bool = False) -> T:
 | 
			
		||||
 | 
			
		||||
# generic fixtures
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def aur_package_ahriman(package_ahriman: Package) -> aur.Package:
 | 
			
		||||
def aur_package_ahriman() -> AURPackage:
 | 
			
		||||
    """
 | 
			
		||||
    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,
 | 
			
		||||
        url_path=package_ahriman.web_url,
 | 
			
		||||
        last_modified=None,
 | 
			
		||||
        name=package_ahriman.base,
 | 
			
		||||
    return AURPackage(
 | 
			
		||||
        id=1009791,
 | 
			
		||||
        name="ahriman",
 | 
			
		||||
        package_base_id=165427,
 | 
			
		||||
        package_base="ahriman",
 | 
			
		||||
        version="1.7.0-1",
 | 
			
		||||
        description="ArcH Linux ReposItory MANager",
 | 
			
		||||
        num_votes=0,
 | 
			
		||||
        popularity=0,
 | 
			
		||||
        first_submitted=datetime.datetime(2021, 4, 9, 22, 44, 45),
 | 
			
		||||
        last_modified=datetime.datetime(2021, 12, 25, 23, 11, 11),
 | 
			
		||||
        url_path="/cgit/aur.git/snapshot/ahriman.tar.gz",
 | 
			
		||||
        url="https://github.com/arcan1s/ahriman",
 | 
			
		||||
        out_of_date=None,
 | 
			
		||||
        id=None,
 | 
			
		||||
        first_submitted=None,
 | 
			
		||||
        maintainer=None,
 | 
			
		||||
        version=package_ahriman.version,
 | 
			
		||||
        license=package_ahriman.packages[package_ahriman.base].licenses,
 | 
			
		||||
        url=None,
 | 
			
		||||
        package_base=package_ahriman.base,
 | 
			
		||||
        package_base_id=None,
 | 
			
		||||
        category_id=None)
 | 
			
		||||
        maintainer="arcanis",
 | 
			
		||||
        depends=[
 | 
			
		||||
            "devtools",
 | 
			
		||||
            "git",
 | 
			
		||||
            "pyalpm",
 | 
			
		||||
            "python-aur",
 | 
			
		||||
            "python-passlib",
 | 
			
		||||
            "python-srcinfo",
 | 
			
		||||
        ],
 | 
			
		||||
        make_depends=["python-pip"],
 | 
			
		||||
        opt_depends=[
 | 
			
		||||
            "breezy",
 | 
			
		||||
            "darcs",
 | 
			
		||||
            "mercurial",
 | 
			
		||||
            "python-aioauth-client",
 | 
			
		||||
            "python-aiohttp",
 | 
			
		||||
            "python-aiohttp-debugtoolbar",
 | 
			
		||||
            "python-aiohttp-jinja2",
 | 
			
		||||
            "python-aiohttp-security",
 | 
			
		||||
            "python-aiohttp-session",
 | 
			
		||||
            "python-boto3",
 | 
			
		||||
            "python-cryptography",
 | 
			
		||||
            "python-jinja",
 | 
			
		||||
            "rsync",
 | 
			
		||||
            "subversion",
 | 
			
		||||
        ],
 | 
			
		||||
        conflicts=[],
 | 
			
		||||
        provides=[],
 | 
			
		||||
        license=["GPL3"],
 | 
			
		||||
        keywords=[],
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
@ -103,7 +132,7 @@ def package_ahriman(package_description_ahriman: PackageDescription) -> Package:
 | 
			
		||||
    packages = {"ahriman": package_description_ahriman}
 | 
			
		||||
    return Package(
 | 
			
		||||
        base="ahriman",
 | 
			
		||||
        version="0.12.1-1",
 | 
			
		||||
        version="1.7.0-1",
 | 
			
		||||
        aur_url="https://aur.archlinux.org",
 | 
			
		||||
        packages=packages)
 | 
			
		||||
 | 
			
		||||
@ -139,9 +168,16 @@ def package_description_ahriman() -> PackageDescription:
 | 
			
		||||
        architecture="x86_64",
 | 
			
		||||
        archive_size=4200,
 | 
			
		||||
        build_date=42,
 | 
			
		||||
        depends=["devtools", "git", "pyalpm", "python-aur", "python-srcinfo"],
 | 
			
		||||
        depends=[
 | 
			
		||||
            "devtools",
 | 
			
		||||
            "git",
 | 
			
		||||
            "pyalpm",
 | 
			
		||||
            "python-aur",
 | 
			
		||||
            "python-passlib",
 | 
			
		||||
            "python-srcinfo",
 | 
			
		||||
        ],
 | 
			
		||||
        description="ArcH Linux ReposItory MANager",
 | 
			
		||||
        filename="ahriman-0.12.1-1-any.pkg.tar.zst",
 | 
			
		||||
        filename="ahriman-1.7.0-1-any.pkg.tar.zst",
 | 
			
		||||
        groups=[],
 | 
			
		||||
        installed_size=4200000,
 | 
			
		||||
        licenses=["GPL3"],
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								tests/ahriman/core/alpm/conftest.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								tests/ahriman/core/alpm/conftest.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from ahriman.core.alpm.aur import AUR
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def aur() -> AUR:
 | 
			
		||||
    """
 | 
			
		||||
    aur helper fixture
 | 
			
		||||
    :return: aur helper instance
 | 
			
		||||
    """
 | 
			
		||||
    return AUR()
 | 
			
		||||
							
								
								
									
										173
									
								
								tests/ahriman/core/alpm/test_aur.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										173
									
								
								tests/ahriman/core/alpm/test_aur.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,173 @@
 | 
			
		||||
import json
 | 
			
		||||
import pytest
 | 
			
		||||
import requests
 | 
			
		||||
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
from unittest import mock
 | 
			
		||||
from unittest.mock import MagicMock
 | 
			
		||||
 | 
			
		||||
from ahriman.core.alpm.aur import AUR
 | 
			
		||||
from ahriman.core.exceptions import InvalidPackageInfo
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _get_response(resource_path_root: Path) -> str:
 | 
			
		||||
    """
 | 
			
		||||
    load response from resource file
 | 
			
		||||
    :param resource_path_root: path to resource root
 | 
			
		||||
    :return: response text
 | 
			
		||||
    """
 | 
			
		||||
    return (resource_path_root / "models" / "package_ahriman_aur").read_text()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_info(mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must call info method
 | 
			
		||||
    """
 | 
			
		||||
    info_mock = mocker.patch("ahriman.core.alpm.aur.AUR.package_info")
 | 
			
		||||
    AUR.info("ahriman")
 | 
			
		||||
    info_mock.assert_called_once_with("ahriman")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_multisearch(aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must search in AUR with multiple words
 | 
			
		||||
    """
 | 
			
		||||
    terms = ["ahriman", "is", "cool"]
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.search", return_value=[aur_package_ahriman])
 | 
			
		||||
 | 
			
		||||
    assert AUR.multisearch(*terms) == [aur_package_ahriman]
 | 
			
		||||
    search_mock.assert_has_calls([mock.call("ahriman"), mock.call("cool")])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_multisearch_empty(mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must return empty list if no long terms supplied
 | 
			
		||||
    """
 | 
			
		||||
    terms = ["it", "is"]
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.search")
 | 
			
		||||
 | 
			
		||||
    assert AUR.multisearch(*terms) == []
 | 
			
		||||
    search_mock.assert_not_called()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_multisearch_single(aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must search in AUR with one word
 | 
			
		||||
    """
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.search", return_value=[aur_package_ahriman])
 | 
			
		||||
    assert AUR.multisearch("ahriman") == [aur_package_ahriman]
 | 
			
		||||
    search_mock.assert_called_once_with("ahriman")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_search(mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must call search method
 | 
			
		||||
    """
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.package_search")
 | 
			
		||||
    AUR.search("ahriman")
 | 
			
		||||
    search_mock.assert_called_once_with("ahriman")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_parse_response(aur_package_ahriman: AURPackage, resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must parse success response
 | 
			
		||||
    """
 | 
			
		||||
    response = _get_response(resource_path_root)
 | 
			
		||||
    assert AUR.parse_response(json.loads(response)) == [aur_package_ahriman]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_parse_response_error(resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must raise exception on invalid response
 | 
			
		||||
    """
 | 
			
		||||
    response = (resource_path_root / "models" / "aur_error").read_text()
 | 
			
		||||
    with pytest.raises(InvalidPackageInfo, match="Incorrect request type specified."):
 | 
			
		||||
        AUR.parse_response(json.loads(response))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_parse_response_unknown_error(resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must raise exception on invalid response with empty error message
 | 
			
		||||
    """
 | 
			
		||||
    with pytest.raises(InvalidPackageInfo, match="Unknown API error"):
 | 
			
		||||
        AUR.parse_response({"type": "error"})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_make_request(aur: AUR, aur_package_ahriman: AURPackage,
 | 
			
		||||
                      mocker: MockerFixture, resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must perform request to AUR
 | 
			
		||||
    """
 | 
			
		||||
    response_mock = MagicMock()
 | 
			
		||||
    response_mock.json.return_value = json.loads(_get_response(resource_path_root))
 | 
			
		||||
    request_mock = mocker.patch("requests.get", return_value=response_mock)
 | 
			
		||||
 | 
			
		||||
    assert aur.make_request("info", "ahriman") == [aur_package_ahriman]
 | 
			
		||||
    request_mock.assert_called_once_with(
 | 
			
		||||
        "https://aur.archlinux.org/rpc", params={"v": "5", "type": "info", "arg": ["ahriman"]})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_make_request_multi_arg(aur: AUR, aur_package_ahriman: AURPackage,
 | 
			
		||||
                                mocker: MockerFixture, resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must perform request to AUR with multiple args
 | 
			
		||||
    """
 | 
			
		||||
    response_mock = MagicMock()
 | 
			
		||||
    response_mock.json.return_value = json.loads(_get_response(resource_path_root))
 | 
			
		||||
    request_mock = mocker.patch("requests.get", return_value=response_mock)
 | 
			
		||||
 | 
			
		||||
    assert aur.make_request("search", "ahriman", "is", "cool") == [aur_package_ahriman]
 | 
			
		||||
    request_mock.assert_called_once_with(
 | 
			
		||||
        "https://aur.archlinux.org/rpc", params={"v": "5", "type": "search", "arg[]": ["ahriman", "is", "cool"]})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_make_request_with_kwargs(aur: AUR, aur_package_ahriman: AURPackage,
 | 
			
		||||
                                  mocker: MockerFixture, resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must perform request to AUR with named parameters
 | 
			
		||||
    """
 | 
			
		||||
    response_mock = MagicMock()
 | 
			
		||||
    response_mock.json.return_value = json.loads(_get_response(resource_path_root))
 | 
			
		||||
    request_mock = mocker.patch("requests.get", return_value=response_mock)
 | 
			
		||||
 | 
			
		||||
    assert aur.make_request("search", "ahriman", by="name") == [aur_package_ahriman]
 | 
			
		||||
    request_mock.assert_called_once_with(
 | 
			
		||||
        "https://aur.archlinux.org/rpc", params={"v": "5", "type": "search", "arg": ["ahriman"], "by": "name"})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_make_request_failed(aur: AUR, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must reraise generic exception
 | 
			
		||||
    """
 | 
			
		||||
    mocker.patch("requests.get", side_effect=Exception())
 | 
			
		||||
    with pytest.raises(Exception):
 | 
			
		||||
        aur.make_request("info", "ahriman")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_make_request_failed_http_error(aur: AUR, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must reraise http exception
 | 
			
		||||
    """
 | 
			
		||||
    mocker.patch("requests.get", side_effect=requests.exceptions.HTTPError())
 | 
			
		||||
    with pytest.raises(requests.exceptions.HTTPError):
 | 
			
		||||
        aur.make_request("info", "ahriman")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_package_info(aur: AUR, aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must make request for info
 | 
			
		||||
    """
 | 
			
		||||
    request_mock = mocker.patch("ahriman.core.alpm.aur.AUR.make_request", return_value=[aur_package_ahriman])
 | 
			
		||||
    assert aur.package_info(aur_package_ahriman.name) == aur_package_ahriman
 | 
			
		||||
    request_mock.assert_called_once_with("info", aur_package_ahriman.name)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_package_search(aur: AUR, aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must make request for search
 | 
			
		||||
    """
 | 
			
		||||
    request_mock = mocker.patch("ahriman.core.alpm.aur.AUR.make_request", return_value=[aur_package_ahriman])
 | 
			
		||||
    assert aur.package_search(aur_package_ahriman.name, by="name") == [aur_package_ahriman]
 | 
			
		||||
    request_mock.assert_called_once_with("search", aur_package_ahriman.name, by="name")
 | 
			
		||||
@ -1,4 +1,3 @@
 | 
			
		||||
import aur
 | 
			
		||||
import datetime
 | 
			
		||||
import logging
 | 
			
		||||
import pytest
 | 
			
		||||
@ -6,45 +5,12 @@ import subprocess
 | 
			
		||||
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
from unittest import mock
 | 
			
		||||
 | 
			
		||||
from ahriman.core.exceptions import InvalidOption, UnsafeRun
 | 
			
		||||
from ahriman.core.util import aur_search, check_output, check_user, filter_json, package_like, pretty_datetime, \
 | 
			
		||||
    pretty_size, walk
 | 
			
		||||
from ahriman.core.util import check_output, check_user, filter_json, package_like, pretty_datetime, pretty_size, walk
 | 
			
		||||
from ahriman.models.package import Package
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_aur_search(aur_package_ahriman: aur.Package, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must search in AUR with multiple words
 | 
			
		||||
    """
 | 
			
		||||
    terms = ["ahriman", "is", "cool"]
 | 
			
		||||
    search_mock = mocker.patch("aur.search", return_value=[aur_package_ahriman])
 | 
			
		||||
 | 
			
		||||
    assert aur_search(*terms) == [aur_package_ahriman]
 | 
			
		||||
    search_mock.assert_has_calls([mock.call("ahriman"), mock.call("cool")])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_aur_search_empty(mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must return empty list if no long terms supplied
 | 
			
		||||
    """
 | 
			
		||||
    terms = ["it", "is"]
 | 
			
		||||
    search_mock = mocker.patch("aur.search")
 | 
			
		||||
 | 
			
		||||
    assert aur_search(*terms) == []
 | 
			
		||||
    search_mock.assert_not_called()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_aur_search_single(aur_package_ahriman: aur.Package, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must search in AUR with one word
 | 
			
		||||
    """
 | 
			
		||||
    search_mock = mocker.patch("aur.search", return_value=[aur_package_ahriman])
 | 
			
		||||
    assert aur_search("ahriman") == [aur_package_ahriman]
 | 
			
		||||
    search_mock.assert_called_once_with("ahriman")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_check_output(mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must run command and log result
 | 
			
		||||
@ -127,7 +93,7 @@ def test_filter_json(package_ahriman: Package) -> None:
 | 
			
		||||
 | 
			
		||||
def test_filter_json_empty_value(package_ahriman: Package) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must return empty values from object
 | 
			
		||||
    must filter empty values from object
 | 
			
		||||
    """
 | 
			
		||||
    probe = package_ahriman.view()
 | 
			
		||||
    probe["base"] = None
 | 
			
		||||
@ -238,8 +204,10 @@ def test_walk(resource_path_root: Path) -> None:
 | 
			
		||||
    expected = sorted([
 | 
			
		||||
        resource_path_root / "core/ahriman.ini",
 | 
			
		||||
        resource_path_root / "core/logging.ini",
 | 
			
		||||
        resource_path_root / "models/aur_error",
 | 
			
		||||
        resource_path_root / "models/big_file_checksum",
 | 
			
		||||
        resource_path_root / "models/empty_file_checksum",
 | 
			
		||||
        resource_path_root / "models/package_ahriman_aur",
 | 
			
		||||
        resource_path_root / "models/package_ahriman_srcinfo",
 | 
			
		||||
        resource_path_root / "models/package_tpacpi-bat-git_srcinfo",
 | 
			
		||||
        resource_path_root / "models/package_yay_srcinfo",
 | 
			
		||||
 | 
			
		||||
@ -22,7 +22,7 @@ def test_calculate_hash_small(resource_path_root: Path) -> None:
 | 
			
		||||
    must calculate checksum for path which is single chunk
 | 
			
		||||
    """
 | 
			
		||||
    path = resource_path_root / "models" / "package_ahriman_srcinfo"
 | 
			
		||||
    assert HttpUpload.calculate_hash(path) == "a55f82198e56061295d405aeb58f4062"
 | 
			
		||||
    assert HttpUpload.calculate_hash(path) == "c0aaf6ebf95ca9206dc8ba1d8ff10af3"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_body_get_hashes() -> None:
 | 
			
		||||
 | 
			
		||||
@ -31,7 +31,7 @@ def test_calculate_etag_small(resource_path_root: Path) -> None:
 | 
			
		||||
    must calculate checksum for path which is single chunk
 | 
			
		||||
    """
 | 
			
		||||
    path = resource_path_root / "models" / "package_ahriman_srcinfo"
 | 
			
		||||
    assert S3.calculate_etag(path, _chunk_size) == "a55f82198e56061295d405aeb58f4062"
 | 
			
		||||
    assert S3.calculate_etag(path, _chunk_size) == "c0aaf6ebf95ca9206dc8ba1d8ff10af3"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_files_remove(s3_remote_objects: List[Any]) -> None:
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										47
									
								
								tests/ahriman/models/test_aur_package.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								tests/ahriman/models/test_aur_package.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
			
		||||
import datetime
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
from dataclasses import asdict, fields
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
from typing import Any, Dict
 | 
			
		||||
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _get_data(resource_path_root: Path) -> Dict[str, Any]:
 | 
			
		||||
    """
 | 
			
		||||
    load package description from resource file
 | 
			
		||||
    :param resource_path_root: path to resource root
 | 
			
		||||
    :return: json descriptor
 | 
			
		||||
    """
 | 
			
		||||
    response = (resource_path_root / "models" / "package_ahriman_aur").read_text()
 | 
			
		||||
    return json.loads(response)["results"][0]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_from_json(aur_package_ahriman: AURPackage, resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must load package from json
 | 
			
		||||
    """
 | 
			
		||||
    model = _get_data(resource_path_root)
 | 
			
		||||
    assert AURPackage.from_json(model) == aur_package_ahriman
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_from_json_2(aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must load the same package from json
 | 
			
		||||
    """
 | 
			
		||||
    mocker.patch("ahriman.models.aur_package.AURPackage.convert", side_effect=lambda v: v)
 | 
			
		||||
    assert AURPackage.from_json(asdict(aur_package_ahriman)) == aur_package_ahriman
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_convert(aur_package_ahriman: AURPackage, resource_path_root: Path) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must convert fields to snakecase and also apply converters
 | 
			
		||||
    """
 | 
			
		||||
    model = _get_data(resource_path_root)
 | 
			
		||||
    converted = AURPackage.convert(model)
 | 
			
		||||
    known_fields = [pair.name for pair in fields(AURPackage)]
 | 
			
		||||
    assert all(field in known_fields for field in converted)
 | 
			
		||||
    assert isinstance(converted.get("first_submitted"), datetime.datetime)
 | 
			
		||||
    assert isinstance(converted.get("last_modified"), datetime.datetime)
 | 
			
		||||
@ -2,9 +2,10 @@ import pytest
 | 
			
		||||
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
from unittest.mock import MagicMock, PropertyMock
 | 
			
		||||
from unittest.mock import MagicMock
 | 
			
		||||
 | 
			
		||||
from ahriman.core.exceptions import InvalidPackageInfo
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
from ahriman.models.package import Package
 | 
			
		||||
from ahriman.models.package_source import PackageSource
 | 
			
		||||
from ahriman.models.repository_paths import RepositoryPaths
 | 
			
		||||
@ -96,15 +97,11 @@ def test_from_archive(package_ahriman: Package, pyalpm_handle: MagicMock, mocker
 | 
			
		||||
    assert Package.from_archive(Path("path"), pyalpm_handle, package_ahriman.aur_url) == package_ahriman
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_from_aur(package_ahriman: Package, mocker: MockerFixture) -> None:
 | 
			
		||||
def test_from_aur(package_ahriman: Package, aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must construct package from aur
 | 
			
		||||
    """
 | 
			
		||||
    mock = MagicMock()
 | 
			
		||||
    type(mock).name = PropertyMock(return_value=package_ahriman.base)
 | 
			
		||||
    type(mock).package_base = PropertyMock(return_value=package_ahriman.base)
 | 
			
		||||
    type(mock).version = PropertyMock(return_value=package_ahriman.version)
 | 
			
		||||
    mocker.patch("aur.info", return_value=mock)
 | 
			
		||||
    mocker.patch("ahriman.core.alpm.aur.AUR.info", return_value=aur_package_ahriman)
 | 
			
		||||
 | 
			
		||||
    package = Package.from_aur(package_ahriman.base, package_ahriman.aur_url)
 | 
			
		||||
    assert package_ahriman.base == package.base
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,9 @@
 | 
			
		||||
import aur
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from aiohttp.test_utils import TestClient
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
 | 
			
		||||
from ahriman.models.aur_package import AURPackage
 | 
			
		||||
from ahriman.models.user_access import UserAccess
 | 
			
		||||
from ahriman.web.views.service.search import SearchView
 | 
			
		||||
 | 
			
		||||
@ -17,11 +17,11 @@ async def test_get_permission() -> None:
 | 
			
		||||
        assert await SearchView.get_permission(request) == UserAccess.Read
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def test_get(client: TestClient, aur_package_ahriman: aur.Package, mocker: MockerFixture) -> None:
 | 
			
		||||
async def test_get(client: TestClient, aur_package_ahriman: AURPackage, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must call get request correctly
 | 
			
		||||
    """
 | 
			
		||||
    mocker.patch("ahriman.web.views.service.search.aur_search", return_value=[aur_package_ahriman])
 | 
			
		||||
    mocker.patch("ahriman.core.alpm.aur.AUR.multisearch", return_value=[aur_package_ahriman])
 | 
			
		||||
    response = await client.get("/service-api/v1/search", params={"for": "ahriman"})
 | 
			
		||||
 | 
			
		||||
    assert response.ok
 | 
			
		||||
@ -33,7 +33,7 @@ async def test_get_exception(client: TestClient, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must raise 400 on empty search string
 | 
			
		||||
    """
 | 
			
		||||
    search_mock = mocker.patch("ahriman.web.views.service.search.aur_search", return_value=[])
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.multisearch", return_value=[])
 | 
			
		||||
    response = await client.get("/service-api/v1/search")
 | 
			
		||||
 | 
			
		||||
    assert response.status == 404
 | 
			
		||||
@ -44,7 +44,7 @@ async def test_get_join(client: TestClient, mocker: MockerFixture) -> None:
 | 
			
		||||
    """
 | 
			
		||||
    must join search args with space
 | 
			
		||||
    """
 | 
			
		||||
    search_mock = mocker.patch("ahriman.web.views.service.search.aur_search")
 | 
			
		||||
    search_mock = mocker.patch("ahriman.core.alpm.aur.AUR.multisearch")
 | 
			
		||||
    response = await client.get("/service-api/v1/search", params=[("for", "ahriman"), ("for", "maybe")])
 | 
			
		||||
 | 
			
		||||
    assert response.ok
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
from aiohttp.test_utils import TestClient
 | 
			
		||||
from pytest_mock import MockerFixture
 | 
			
		||||
from unittest.mock import MagicMock
 | 
			
		||||
@ -31,7 +32,7 @@ async def test_get_redirect_to_oauth(client_with_auth: TestClient) -> None:
 | 
			
		||||
    must redirect to OAuth service provider in case if no code is supplied
 | 
			
		||||
    """
 | 
			
		||||
    oauth = client_with_auth.app["validator"] = MagicMock(spec=OAuth)
 | 
			
		||||
    oauth.get_oauth_url.return_value = "https://example.com"
 | 
			
		||||
    oauth.get_oauth_url.return_value = "https://httpbin.org"
 | 
			
		||||
 | 
			
		||||
    get_response = await client_with_auth.get("/user-api/v1/login")
 | 
			
		||||
    assert get_response.ok
 | 
			
		||||
@ -43,7 +44,7 @@ async def test_get_redirect_to_oauth_empty_code(client_with_auth: TestClient) ->
 | 
			
		||||
    must redirect to OAuth service provider in case if empty code is supplied
 | 
			
		||||
    """
 | 
			
		||||
    oauth = client_with_auth.app["validator"] = MagicMock(spec=OAuth)
 | 
			
		||||
    oauth.get_oauth_url.return_value = "https://example.com"
 | 
			
		||||
    oauth.get_oauth_url.return_value = "https://httpbin.org"
 | 
			
		||||
 | 
			
		||||
    get_response = await client_with_auth.get("/user-api/v1/login", params={"code": ""})
 | 
			
		||||
    assert get_response.ok
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user