mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
feat: implement stats subcommand (#132)
This commit is contained in:
199
tests/ahriman/application/handlers/test_handler_statistics.py
Normal file
199
tests/ahriman/application/handlers/test_handler_statistics.py
Normal file
@ -0,0 +1,199 @@
|
||||
import argparse
|
||||
import pytest
|
||||
|
||||
from pathlib import Path
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import call as MockCall
|
||||
|
||||
from ahriman.application.handlers import Statistics
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.repository import Repository
|
||||
from ahriman.core.utils import pretty_datetime, utcnow
|
||||
from ahriman.models.event import Event, EventType
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
def _default_args(args: argparse.Namespace) -> argparse.Namespace:
|
||||
"""
|
||||
default arguments for these test cases
|
||||
|
||||
Args:
|
||||
args(argparse.Namespace): command line arguments fixture
|
||||
|
||||
Returns:
|
||||
argparse.Namespace: generated arguments for these test cases
|
||||
"""
|
||||
args.chart = None
|
||||
args.event = EventType.PackageUpdated
|
||||
args.from_date = None
|
||||
args.limit = -1
|
||||
args.offset = 0
|
||||
args.package = None
|
||||
args.to_date = None
|
||||
return args
|
||||
|
||||
|
||||
def test_run(args: argparse.Namespace, configuration: Configuration, repository: Repository,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command
|
||||
"""
|
||||
args = _default_args(args)
|
||||
events = [Event("1", "1"), Event("2", "2")]
|
||||
mocker.patch("ahriman.core.repository.Repository.load", return_value=repository)
|
||||
events_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.event_get", return_value=events)
|
||||
application_mock = mocker.patch("ahriman.application.handlers.Statistics.stats_per_package")
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
Statistics.run(args, repository_id, configuration, report=False)
|
||||
events_mock.assert_called_once_with(args.event, args.package, None, None, args.limit, args.offset)
|
||||
application_mock.assert_called_once_with(args.event, events, args.chart)
|
||||
|
||||
|
||||
def test_run_for_package(args: argparse.Namespace, configuration: Configuration, repository: Repository,
|
||||
package_ahriman: Package, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must run command for specific package
|
||||
"""
|
||||
args = _default_args(args)
|
||||
args.package = package_ahriman.base
|
||||
events = [Event("1", "1"), Event("2", "2")]
|
||||
mocker.patch("ahriman.core.repository.Repository.load", return_value=repository)
|
||||
events_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.event_get", return_value=events)
|
||||
application_mock = mocker.patch("ahriman.application.handlers.Statistics.stats_for_package")
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
Statistics.run(args, repository_id, configuration, report=False)
|
||||
events_mock.assert_called_once_with(args.event, args.package, None, None, args.limit, args.offset)
|
||||
application_mock.assert_called_once_with(args.event, events, args.chart)
|
||||
|
||||
|
||||
def test_run_convert_from_date(args: argparse.Namespace, configuration: Configuration, repository: Repository,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must convert from date
|
||||
"""
|
||||
args = _default_args(args)
|
||||
date = utcnow()
|
||||
args.from_date = date.isoformat()
|
||||
mocker.patch("ahriman.core.repository.Repository.load", return_value=repository)
|
||||
mocker.patch("ahriman.application.handlers.Statistics.stats_per_package")
|
||||
events_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.event_get", return_value=[])
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
Statistics.run(args, repository_id, configuration, report=False)
|
||||
events_mock.assert_called_once_with(args.event, args.package, date.timestamp(), None, args.limit, args.offset)
|
||||
|
||||
|
||||
def test_run_convert_to_date(args: argparse.Namespace, configuration: Configuration, repository: Repository,
|
||||
mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must convert to date
|
||||
"""
|
||||
args = _default_args(args)
|
||||
date = utcnow()
|
||||
args.to_date = date.isoformat()
|
||||
mocker.patch("ahriman.core.repository.Repository.load", return_value=repository)
|
||||
mocker.patch("ahriman.application.handlers.Statistics.stats_per_package")
|
||||
events_mock = mocker.patch("ahriman.core.status.local_client.LocalClient.event_get", return_value=[])
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
Statistics.run(args, repository_id, configuration, report=False)
|
||||
events_mock.assert_called_once_with(args.event, args.package, None, date.timestamp(), args.limit, args.offset)
|
||||
|
||||
|
||||
def test_event_stats(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must print event stats
|
||||
"""
|
||||
print_mock = mocker.patch("ahriman.core.formatters.Printer.print")
|
||||
events = [Event("event", "1"), Event("event", "2", took=42.0)]
|
||||
|
||||
Statistics.event_stats("event", events)
|
||||
print_mock.assert_called_once_with(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": ")
|
||||
|
||||
|
||||
def test_plot_packages(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must plot chart for packages
|
||||
"""
|
||||
plot_mock = mocker.patch("matplotlib.pyplot.bar")
|
||||
save_mock = mocker.patch("matplotlib.pyplot.savefig")
|
||||
local = Path("local")
|
||||
|
||||
Statistics.plot_packages("event", {"1": 1, "2": 2}, local)
|
||||
plot_mock.assert_called_once_with(["1", "2"], [1, 2])
|
||||
save_mock.assert_called_once_with(local)
|
||||
|
||||
|
||||
def test_plot_times(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must plot chart for durations
|
||||
"""
|
||||
plot_mock = mocker.patch("matplotlib.pyplot.plot")
|
||||
save_mock = mocker.patch("matplotlib.pyplot.savefig")
|
||||
local = Path("local")
|
||||
|
||||
Statistics.plot_times("event", [
|
||||
Event("", "", created=1, took=2),
|
||||
Event("", "", created=3, took=4),
|
||||
], local)
|
||||
plot_mock.assert_called_once_with((pretty_datetime(1), pretty_datetime(3)), (2, 4))
|
||||
save_mock.assert_called_once_with(local)
|
||||
|
||||
|
||||
def test_stats_for_package(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must print statistics for the package
|
||||
"""
|
||||
events = [Event("event", "1"), Event("event", "1")]
|
||||
events_mock = mocker.patch("ahriman.application.handlers.Statistics.event_stats")
|
||||
chart_plot = mocker.patch("ahriman.application.handlers.Statistics.plot_times")
|
||||
|
||||
Statistics.stats_for_package("event", events, None)
|
||||
events_mock.assert_called_once_with("event", events)
|
||||
chart_plot.assert_not_called()
|
||||
|
||||
|
||||
def test_stats_for_package_with_chart(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must generate chart for package stats
|
||||
"""
|
||||
local = Path("local")
|
||||
events = [Event("event", "1"), Event("event", "1")]
|
||||
mocker.patch("ahriman.application.handlers.Statistics.event_stats")
|
||||
chart_plot = mocker.patch("ahriman.application.handlers.Statistics.plot_times")
|
||||
|
||||
Statistics.stats_for_package("event", events, local)
|
||||
chart_plot.assert_called_once_with("event", events, local)
|
||||
|
||||
|
||||
def test_stats_per_package(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must print statistics per package
|
||||
"""
|
||||
events = [Event("event", "1"), Event("event", "2"), Event("event", "1")]
|
||||
print_mock = mocker.patch("ahriman.core.formatters.Printer.print")
|
||||
events_mock = mocker.patch("ahriman.application.handlers.Statistics.event_stats")
|
||||
chart_plot = mocker.patch("ahriman.application.handlers.Statistics.plot_packages")
|
||||
|
||||
Statistics.stats_per_package("event", events, None)
|
||||
print_mock.assert_has_calls([
|
||||
MockCall(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": "),
|
||||
MockCall(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": "),
|
||||
])
|
||||
events_mock.assert_called_once_with("event", events)
|
||||
chart_plot.assert_not_called()
|
||||
|
||||
|
||||
def test_stats_per_package_with_chart(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must print statistics per package with chart
|
||||
"""
|
||||
local = Path("local")
|
||||
events = [Event("event", "1"), Event("event", "2"), Event("event", "1")]
|
||||
mocker.patch("ahriman.application.handlers.Statistics.event_stats")
|
||||
chart_plot = mocker.patch("ahriman.application.handlers.Statistics.plot_packages")
|
||||
|
||||
Statistics.stats_per_package("event", events, local)
|
||||
chart_plot.assert_called_once_with("event", {"1": 2, "2": 1}, local)
|
@ -9,6 +9,7 @@ from ahriman.application.handlers import Handler
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.models.action import Action
|
||||
from ahriman.models.build_status import BuildStatusEnum
|
||||
from ahriman.models.event import EventType
|
||||
from ahriman.models.log_handler import LogHandler
|
||||
from ahriman.models.sign_settings import SignSettings
|
||||
from ahriman.models.user_access import UserAccess
|
||||
@ -931,11 +932,73 @@ def test_subparsers_repo_sign_option_repository(parser: argparse.ArgumentParser)
|
||||
assert args.repository == "repo"
|
||||
|
||||
|
||||
def test_subparsers_repo_statistics(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
repo-statistics command must imply lock, quiet, report and unsafe
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics"])
|
||||
assert args.architecture == "x86_64"
|
||||
assert args.lock is None
|
||||
assert args.quiet
|
||||
assert not args.report
|
||||
assert args.repository == "repo"
|
||||
assert args.unsafe
|
||||
|
||||
|
||||
def test_subparsers_repo_statistics_option_event(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
repo-statistics command must convert event option to EventType instance
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics"])
|
||||
assert isinstance(args.event, EventType)
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics", "--event", "package-removed"])
|
||||
assert isinstance(args.event, EventType)
|
||||
|
||||
|
||||
def test_subparsers_repo_statistics_option_package(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
repo-statistics command must parse optional package argument
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics"])
|
||||
assert args.package is None
|
||||
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics", "package"])
|
||||
assert args.package == "package"
|
||||
|
||||
|
||||
def test_subparsers_repo_statistics_option_chart(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
repo-statistics command must convert chart option to Path instance
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics", "--chart", "path"])
|
||||
assert isinstance(args.chart, Path)
|
||||
|
||||
|
||||
def test_subparsers_repo_statistics_option_limit(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
repo-statistics command must convert chart option to Path instance
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics"])
|
||||
assert isinstance(args.limit, int)
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics", "--limit", "42"])
|
||||
assert isinstance(args.limit, int)
|
||||
|
||||
|
||||
def test_subparsers_repo_statistics_option_offset(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
repo-statistics command must convert chart option to Path instance
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics"])
|
||||
assert isinstance(args.offset, int)
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-statistics", "--offset", "42"])
|
||||
assert isinstance(args.offset, int)
|
||||
|
||||
|
||||
def test_subparsers_repo_status_update(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
re[p-status-update command must imply action, lock, quiet, report, package and unsafe
|
||||
repo-status-update command must imply action, lock, quiet, report, package and unsafe
|
||||
"""
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "package-status-update"])
|
||||
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "repo-status-update"])
|
||||
assert args.architecture == "x86_64"
|
||||
assert args.action == Action.Update
|
||||
assert args.lock is None
|
||||
|
@ -2,9 +2,23 @@ import pytest
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from ahriman.core.formatters import AurPrinter, ChangesPrinter, ConfigurationPathsPrinter, ConfigurationPrinter, \
|
||||
PackagePrinter, PatchPrinter, RepositoryPrinter, StatusPrinter, StringPrinter, TreePrinter, UpdatePrinter, \
|
||||
UserPrinter, ValidationPrinter, VersionPrinter
|
||||
from ahriman.core.formatters import \
|
||||
AurPrinter, \
|
||||
ChangesPrinter, \
|
||||
ConfigurationPathsPrinter, \
|
||||
ConfigurationPrinter, \
|
||||
EventStatsPrinter, \
|
||||
PackagePrinter, \
|
||||
PackageStatsPrinter, \
|
||||
PatchPrinter, \
|
||||
RepositoryPrinter, \
|
||||
StatusPrinter, \
|
||||
StringPrinter, \
|
||||
TreePrinter, \
|
||||
UpdatePrinter, \
|
||||
UserPrinter, \
|
||||
ValidationPrinter, \
|
||||
VersionPrinter
|
||||
from ahriman.models.aur_package import AURPackage
|
||||
from ahriman.models.build_status import BuildStatus
|
||||
from ahriman.models.changes import Changes
|
||||
@ -61,6 +75,17 @@ def configuration_printer() -> ConfigurationPrinter:
|
||||
return ConfigurationPrinter("section", {"key_one": "value_one", "key_two": "value_two"})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def event_stats_printer() -> EventStatsPrinter:
|
||||
"""
|
||||
fixture for event stats printer
|
||||
|
||||
Returns:
|
||||
EventStatsPrinter: event stats printer test instance
|
||||
"""
|
||||
return EventStatsPrinter("event", [5, 2, 7, 9, 8, 0, 4, 1, 6, 3])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_ahriman_printer(package_ahriman: Package) -> PackagePrinter:
|
||||
"""
|
||||
@ -75,6 +100,21 @@ def package_ahriman_printer(package_ahriman: Package) -> PackagePrinter:
|
||||
return PackagePrinter(package_ahriman, BuildStatus())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package_stats_printer(package_ahriman: Package, package_python_schedule: Package) -> PackageStatsPrinter:
|
||||
"""
|
||||
fixture for package stats printer
|
||||
|
||||
Args:
|
||||
package_ahriman(Package): package fixture
|
||||
package_python_schedule(Package): schedule package fixture
|
||||
|
||||
Returns:
|
||||
PackageStatsPrinter: package stats printer test instance
|
||||
"""
|
||||
return PackageStatsPrinter({package_ahriman.base: 4, package_python_schedule.base: 5})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patch_printer(package_ahriman: Package) -> PatchPrinter:
|
||||
"""
|
||||
|
29
tests/ahriman/core/formatters/test_event_stats_printer.py
Normal file
29
tests/ahriman/core/formatters/test_event_stats_printer.py
Normal file
@ -0,0 +1,29 @@
|
||||
from ahriman.core.formatters import EventStatsPrinter
|
||||
|
||||
|
||||
def test_properties(event_stats_printer: EventStatsPrinter) -> None:
|
||||
"""
|
||||
must return empty properties list
|
||||
"""
|
||||
assert event_stats_printer.properties()
|
||||
|
||||
|
||||
def test_properties_empty() -> None:
|
||||
"""
|
||||
must correctly generate properties for empty events list
|
||||
"""
|
||||
assert EventStatsPrinter("event", []).properties()
|
||||
|
||||
|
||||
def test_properties_single() -> None:
|
||||
"""
|
||||
must skip calculation of the standard deviation for single event
|
||||
"""
|
||||
assert EventStatsPrinter("event", [1]).properties()
|
||||
|
||||
|
||||
def test_title(event_stats_printer: EventStatsPrinter) -> None:
|
||||
"""
|
||||
must return non-empty title
|
||||
"""
|
||||
assert event_stats_printer.title() is not None
|
30
tests/ahriman/core/formatters/test_package_stats_printer.py
Normal file
30
tests/ahriman/core/formatters/test_package_stats_printer.py
Normal file
@ -0,0 +1,30 @@
|
||||
from ahriman.core.formatters import PackageStatsPrinter
|
||||
|
||||
|
||||
def test_properties(package_stats_printer: PackageStatsPrinter) -> None:
|
||||
"""
|
||||
must return non-empty properties list
|
||||
"""
|
||||
assert package_stats_printer.properties()
|
||||
|
||||
|
||||
def test_properties_sorted(package_stats_printer: PackageStatsPrinter) -> None:
|
||||
"""
|
||||
properties list must be sorted in descending order
|
||||
"""
|
||||
prop1, prop2 = package_stats_printer.properties()
|
||||
assert prop1.value > prop2.value
|
||||
|
||||
|
||||
def test_properties_empty() -> None:
|
||||
"""
|
||||
must return empty properties list for the empty events list
|
||||
"""
|
||||
assert not PackageStatsPrinter({}).properties()
|
||||
|
||||
|
||||
def test_title(package_stats_printer: PackageStatsPrinter) -> None:
|
||||
"""
|
||||
must return non-empty title
|
||||
"""
|
||||
assert package_stats_printer.title() is not None
|
Reference in New Issue
Block a user