testsAndMisc/linux_configuration/tests/test_usage_report_segments.py
Krzysztof kuhy Rudnicki 57b9005a6b usage_report: delete the dead _compute_window shim
Documented as "kept for backwards import compatibility", it had zero
production callers - only its own two tests. Removed the function, those
two tests, and the pathlib.Path import that only its signature used.

_SEC_PER_DAY stays: two live callers remain at _usage_report_run.py:135
and :181, so only the function was dead, not the constant.

Verified: usage_report.py --help exits 0 (the unit tests stub subprocesses,
so only a real run proves the import graph), 202 tests pass, and
_usage_report_run.py holds 100% line+branch coverage after removing both
the code and the tests that covered it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 12:57:52 +02:00

85 lines
2.6 KiB
Python

"""Tests for usage_report's per-day segment aggregation and the
log-description strings.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import _usage_report_run as run
from _usage_report_types import _Progress, _Window
if TYPE_CHECKING:
from pathlib import Path
import pytest
def _progress() -> _Progress:
"""A progress reporter that never draws, so stderr stays clean."""
return _Progress(enabled=False, total_stages=1)
# --------------------------------------------------------------------------- #
# _aggregate_segments / _log_descriptions
# --------------------------------------------------------------------------- #
def test_aggregate_segments_skips_missing_atop_logs(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
"""A gap day contributes no atop data but its pmon log is still read."""
present = tmp_path / "atop-present"
present.write_text("x", encoding="utf-8")
segments = [
run._Segment(present, tmp_path / "pmon-a"),
run._Segment(tmp_path / "atop-absent", tmp_path / "pmon-b"),
]
monkeypatch.setattr(
run,
"aggregate_atop",
lambda *_a: ({}, _Window(distinct_samples=2, seconds=600)),
)
pmon_calls: list[Path] = []
def fake_pmon(log: Path, *_a: object) -> tuple[dict[str, object], int]:
pmon_calls.append(log)
return {}, 5
monkeypatch.setattr(run, "aggregate_pmon", fake_pmon)
aggs = run._aggregate_segments(segments, _progress())
assert aggs.days_with_data == 1
assert aggs.gpu_samples == 10
assert len(pmon_calls) == 2
def test_log_descriptions_names_only_existing_logs(tmp_path: Path) -> None:
"""Descriptions cover the logs that are actually on disk."""
atop = tmp_path / "atop"
atop.write_text("x", encoding="utf-8")
segments = [run._Segment(atop, tmp_path / "pmon-absent")]
atop_desc, pmon_desc = run._log_descriptions(segments)
assert str(atop) in atop_desc
assert "none" in pmon_desc.lower() or pmon_desc
def test_aggregate_segments_ignores_a_log_with_no_samples(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
"""A present atop log that yields no samples does not count as a data day."""
present = tmp_path / "atop-empty"
present.write_text("x", encoding="utf-8")
monkeypatch.setattr(run, "aggregate_atop", lambda *_a: ({}, _Window()))
monkeypatch.setattr(run, "aggregate_pmon", lambda *_a: ({}, 0))
aggs = run._aggregate_segments(
[run._Segment(present, tmp_path / "pmon")],
_progress(),
)
assert aggs.days_with_data == 0