Files
meshcore-stats/tests/reports/test_aggregation_helpers.py
T
Jorijn Schrijvershof a9f6926104 test: add comprehensive pytest test suite with 95% coverage (#29)
* test: add comprehensive pytest test suite with 95% coverage

Add full unit and integration test coverage for the meshcore-stats project:

- 1020 tests covering all modules (db, charts, html, reports, client, etc.)
- 95.95% code coverage with pytest-cov (95% threshold enforced)
- GitHub Actions CI workflow for automated testing on push/PR
- Proper mocking of external dependencies (meshcore, serial, filesystem)
- SVG snapshot infrastructure for chart regression testing
- Integration tests for collection and rendering pipelines

Test organization:
- tests/charts/: Chart rendering and statistics
- tests/client/: MeshCore client and connection handling
- tests/config/: Environment and configuration parsing
- tests/database/: SQLite operations and migrations
- tests/html/: HTML generation and Jinja templates
- tests/reports/: Report generation and formatting
- tests/retry/: Circuit breaker and retry logic
- tests/unit/: Pure unit tests for utilities
- tests/integration/: End-to-end pipeline tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: add test-engineer agent configuration

Add project-local test-engineer agent for pytest test development,
coverage analysis, and test review tasks.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: comprehensive test suite review with 956 tests analyzed

Conducted thorough review of all 956 test cases across 47 test files:

- Unit Tests: 338 tests (battery, metrics, log, telemetry, env, charts, html, reports, formatters)
- Config Tests: 53 tests (env loading, config file parsing)
- Database Tests: 115 tests (init, insert, queries, migrations, maintenance, validation)
- Retry Tests: 59 tests (circuit breaker, async retries, factory)
- Charts Tests: 76 tests (transforms, statistics, timeseries, rendering, I/O)
- HTML Tests: 81 tests (site generation, Jinja2, metrics builders, reports index)
- Reports Tests: 149 tests (location, JSON/TXT formatting, aggregation, counter totals)
- Client Tests: 63 tests (contacts, connection, meshcore availability, commands)
- Integration Tests: 22 tests (reports, collection, rendering pipelines)

Results:
- Overall Pass Rate: 99.7% (953/956)
- 3 tests marked for improvement (empty test bodies in client tests)
- 0 tests requiring fixes

Key findings documented in test_review/tests.md including quality
observations, F.I.R.S.T. principle adherence, and recommendations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: implement snapshot testing for charts and reports

Add comprehensive snapshot testing infrastructure:

SVG Chart Snapshots:
- Deterministic fixtures with fixed timestamps (2024-01-15 12:00:00)
- Tests for gauge/counter metrics in light/dark themes
- Empty chart and single-point edge cases
- Extended normalize_svg_for_snapshot_full() for reproducible comparisons

TXT Report Snapshots:
- Monthly/yearly report snapshots for repeater and companion
- Empty report handling tests
- Tests in tests/reports/test_snapshots.py

Infrastructure:
- tests/snapshots/conftest.py with shared fixtures
- UPDATE_SNAPSHOTS=1 environment variable for regeneration
- scripts/generate_snapshots.py for batch snapshot generation

Run `UPDATE_SNAPSHOTS=1 pytest tests/charts/test_chart_render.py::TestSvgSnapshots`
to generate initial snapshots.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: fix SVG normalization and generate initial snapshots

Fix normalize_svg_for_snapshot() to handle:
- clipPath IDs like id="p47c77a2a6e"
- url(#p...) references
- xlink:href="#p..." references
- <dc:date> timestamps

Generated initial snapshot files:
- 7 SVG chart snapshots (gauge, counter, empty, single-point in light/dark)
- 6 TXT report snapshots (monthly/yearly for repeater/companion + empty)

All 13 snapshot tests now pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: fix SVG normalization to preserve axis rendering

The SVG normalization was replacing all matplotlib-generated IDs with
the same value, causing duplicate IDs that broke SVG rendering:
- Font glyphs, clipPaths, and tick marks all got id="normalized"
- References couldn't resolve to the correct elements
- X and Y axes failed to render in normalized snapshots

Fix uses type-specific prefixes with sequential numbering:
- glyph_N for font glyphs (DejaVuSans-XX patterns)
- clip_N for clipPath definitions (p[0-9a-f]{8,} patterns)
- tick_N for tick marks (m[0-9a-f]{8,} patterns)

This ensures all IDs remain unique while still being deterministic
for snapshot comparison.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: add coverage and pytest artifacts to gitignore

Add .coverage, .coverage.*, htmlcov/, and .pytest_cache/ to prevent
test artifacts from being committed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* style: fix all ruff lint errors across codebase

- Sort and organize imports (I001)
- Use modern type annotations (X | Y instead of Union, collections.abc)
- Remove unused imports (F401)
- Combine nested if statements (SIM102)
- Use ternary operators where appropriate (SIM108)
- Combine nested with statements (SIM117)
- Use contextlib.suppress instead of try-except-pass (SIM105)
- Add noqa comments for intentional SIM115 violations (file locks)
- Add TYPE_CHECKING import for forward references
- Fix exception chaining (B904)

All 1033 tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: add TDD workflow and pre-commit requirements to CLAUDE.md

- Add mandatory test-driven development workflow (write tests first)
- Add pre-commit requirements (must run lint and tests before committing)
- Document test organization and running commands
- Document 95% coverage requirement

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: resolve mypy type checking errors with proper structural fixes

- charts.py: Create PeriodConfig dataclass for type-safe period configuration,
  use mdates.date2num() for matplotlib datetime handling, fix x-axis limits
  for single-point charts
- db.py: Add explicit int() conversion with None handling for SQLite returns
- env.py: Add class-level type annotations to Config class
- html.py: Add MetricDisplay TypedDict, fix import order, add proper type
  annotations for table data functions
- meshcore_client.py: Add return type annotation

Update tests to use new dataclass attribute access and regenerate SVG
snapshots. Add mypy step to CLAUDE.md pre-commit requirements.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: cast Jinja2 template.render() to str for mypy

Jinja2's type stubs declare render() as returning Any, but it actually
returns str. Wrap with str() to satisfy mypy's no-any-return check.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* ci: improve workflow security and reliability

- test.yml: Pin all actions by SHA, add concurrency control to cancel
  in-progress runs on rapid pushes
- release-please.yml: Pin action by SHA, add 10-minute timeout
- conftest.py: Fix snapshot_base_time to use explicit UTC timezone for
  consistent behavior across CI and local environments

Regenerate SVG snapshots with UTC-aware timestamps.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: add mypy command to permissions in settings.local.json

* test: add comprehensive script tests with coroutine warning fixes

- Add tests/scripts/ with tests for collect_companion, collect_repeater,
  and render scripts (1135 tests total, 96% coverage)
- Fix unawaited coroutine warnings by using AsyncMock properly for async
  functions and async_context_manager_factory fixture for context managers
- Add --cov=scripts to CI workflow and pyproject.toml coverage config
- Omit scripts/generate_snapshots.py from coverage (dev utility)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: migrate claude setup to codex skills

* feat: migrate dependencies to uv (#31)

* fix: run tests through uv

* test: fix ruff lint issues in tests

Consolidate patch context managers and clean unused imports/variables

Use datetime.UTC in snapshot fixtures

* test: avoid unawaited async mocks in entrypoint tests

* ci: replace codecov with github coverage artifacts

Add junit XML output and coverage summary in job output

Upload HTML and XML coverage artifacts (3.12 only) on every run

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 17:16:53 +01:00

389 lines
14 KiB
Python

"""Tests for report aggregation helper functions."""
from datetime import date, datetime
import pytest
from meshmon.reports import (
DailyAggregate,
MetricStats,
MonthlyAggregate,
_aggregate_daily_counter_to_summary,
_aggregate_daily_gauge_to_summary,
_aggregate_monthly_counter_to_summary,
_aggregate_monthly_gauge_to_summary,
_compute_counter_stats,
_compute_gauge_stats,
)
class TestComputeGaugeStats:
"""Tests for _compute_gauge_stats function."""
def test_returns_metric_stats(self):
"""Returns a MetricStats dataclass."""
values = [
(datetime(2024, 1, 1, 0, 0), 3.8),
(datetime(2024, 1, 1, 1, 0), 3.9),
(datetime(2024, 1, 1, 2, 0), 4.0),
]
result = _compute_gauge_stats(values)
assert isinstance(result, MetricStats)
def test_computes_min_max_mean(self):
"""Computes correct min, max, and mean."""
values = [
(datetime(2024, 1, 1, 0, 0), 3.8),
(datetime(2024, 1, 1, 1, 0), 3.9),
(datetime(2024, 1, 1, 2, 0), 4.0),
]
result = _compute_gauge_stats(values)
assert result.min_value == 3.8
assert result.max_value == 4.0
assert result.mean == pytest.approx(3.9)
def test_handles_single_value(self):
"""Handles single value correctly."""
values = [(datetime(2024, 1, 1, 0, 0), 3.85)]
result = _compute_gauge_stats(values)
assert result.min_value == 3.85
assert result.max_value == 3.85
assert result.mean == 3.85
def test_handles_empty_list(self):
"""Handles empty list gracefully."""
result = _compute_gauge_stats([])
assert result.min_value is None
assert result.max_value is None
assert result.mean is None
def test_tracks_count(self):
"""Tracks the number of values."""
values = [
(datetime(2024, 1, 1, i, 0), 3.8 + i * 0.01)
for i in range(10)
]
result = _compute_gauge_stats(values)
assert result.count == 10
def test_tracks_min_time(self):
"""Tracks timestamp of minimum value."""
values = [
(datetime(2024, 1, 1, 0, 0), 3.9),
(datetime(2024, 1, 1, 1, 0), 3.7), # Min
(datetime(2024, 1, 1, 2, 0), 3.8),
]
result = _compute_gauge_stats(values)
assert result.min_time == datetime(2024, 1, 1, 1, 0)
def test_tracks_max_time(self):
"""Tracks timestamp of maximum value."""
values = [
(datetime(2024, 1, 1, 0, 0), 3.9),
(datetime(2024, 1, 1, 1, 0), 4.1), # Max
(datetime(2024, 1, 1, 2, 0), 3.8),
]
result = _compute_gauge_stats(values)
assert result.max_time == datetime(2024, 1, 1, 1, 0)
class TestComputeCounterStats:
"""Tests for _compute_counter_stats function."""
def test_returns_metric_stats(self):
"""Returns a MetricStats dataclass."""
values = [
(datetime(2024, 1, 1, 0, 0), 100),
(datetime(2024, 1, 1, 1, 0), 150),
(datetime(2024, 1, 1, 2, 0), 200),
]
result = _compute_counter_stats(values)
assert isinstance(result, MetricStats)
def test_computes_total_delta(self):
"""Computes total delta from counter values."""
values = [
(datetime(2024, 1, 1, 0, 0), 100),
(datetime(2024, 1, 1, 1, 0), 150), # +50
(datetime(2024, 1, 1, 2, 0), 200), # +50
]
result = _compute_counter_stats(values)
# Total should be 100 (50 + 50)
assert result.total == 100
def test_handles_counter_reboot(self):
"""Handles counter reboot (value decrease)."""
values = [
(datetime(2024, 1, 1, 0, 0), 100),
(datetime(2024, 1, 1, 1, 0), 150), # +50
(datetime(2024, 1, 1, 2, 0), 20), # Reboot - counts from 0
(datetime(2024, 1, 1, 3, 0), 50), # +30
]
result = _compute_counter_stats(values)
# Total: 50 + 20 + 30 = 100
assert result.total == 100
def test_tracks_reboot_count(self):
"""Tracks number of reboots."""
values = [
(datetime(2024, 1, 1, 0, 0), 100),
(datetime(2024, 1, 1, 1, 0), 150),
(datetime(2024, 1, 1, 2, 0), 20), # Reboot 1
(datetime(2024, 1, 1, 3, 0), 50),
(datetime(2024, 1, 1, 4, 0), 10), # Reboot 2
]
result = _compute_counter_stats(values)
assert result.reboot_count == 2
def test_handles_empty_list(self):
"""Handles empty list gracefully."""
result = _compute_counter_stats([])
assert result.total is None
def test_handles_single_value(self):
"""Handles single value (no delta possible)."""
values = [(datetime(2024, 1, 1, 0, 0), 100)]
result = _compute_counter_stats(values)
# Single value means no delta can be computed
assert result.total is None
class TestAggregateDailyGaugeToSummary:
"""Tests for _aggregate_daily_gauge_to_summary function."""
@pytest.fixture
def daily_gauge_data(self):
"""Sample daily gauge aggregates."""
return [
DailyAggregate(
date=date(2024, 1, 1),
metrics={
"battery": MetricStats(
min_value=3.7, min_time=datetime(2024, 1, 1, 3, 0),
max_value=3.9, max_time=datetime(2024, 1, 1, 15, 0),
mean=3.8, count=96
)
}
),
DailyAggregate(
date=date(2024, 1, 2),
metrics={
"battery": MetricStats(
min_value=3.6, min_time=datetime(2024, 1, 2, 4, 0),
max_value=4.0, max_time=datetime(2024, 1, 2, 12, 0),
mean=3.85, count=96
)
}
),
DailyAggregate(
date=date(2024, 1, 3),
metrics={
"battery": MetricStats(
min_value=3.8, min_time=datetime(2024, 1, 3, 2, 0),
max_value=4.1, max_time=datetime(2024, 1, 3, 18, 0),
mean=3.95, count=96
)
}
),
]
def test_returns_metric_stats(self, daily_gauge_data):
"""Returns a MetricStats object."""
result = _aggregate_daily_gauge_to_summary(daily_gauge_data, "battery")
assert isinstance(result, MetricStats)
def test_finds_overall_min(self, daily_gauge_data):
"""Finds minimum across all days."""
result = _aggregate_daily_gauge_to_summary(daily_gauge_data, "battery")
assert result.min_value == 3.6
def test_finds_overall_max(self, daily_gauge_data):
"""Finds maximum across all days."""
result = _aggregate_daily_gauge_to_summary(daily_gauge_data, "battery")
assert result.max_value == 4.1
def test_computes_weighted_mean(self, daily_gauge_data):
"""Computes weighted mean based on count."""
result = _aggregate_daily_gauge_to_summary(daily_gauge_data, "battery")
# All have same count, so simple average: (3.8 + 3.85 + 3.95) / 3 = 3.8667
assert result.mean == pytest.approx(3.8667, rel=0.01)
def test_handles_empty_list(self):
"""Handles empty daily list."""
result = _aggregate_daily_gauge_to_summary([], "battery")
assert result.min_value is None
assert result.max_value is None
assert result.mean is None
def test_handles_missing_metric(self, daily_gauge_data):
"""Handles when metric doesn't exist in daily data."""
result = _aggregate_daily_gauge_to_summary(daily_gauge_data, "nonexistent")
assert result.min_value is None
assert result.max_value is None
class TestAggregateDailyCounterToSummary:
"""Tests for _aggregate_daily_counter_to_summary function."""
@pytest.fixture
def daily_counter_data(self):
"""Sample daily counter aggregates."""
return [
DailyAggregate(
date=date(2024, 1, 1),
metrics={
"packets_rx": MetricStats(total=1000, reboot_count=0, count=96)
}
),
DailyAggregate(
date=date(2024, 1, 2),
metrics={
"packets_rx": MetricStats(total=1500, reboot_count=1, count=96)
}
),
DailyAggregate(
date=date(2024, 1, 3),
metrics={
"packets_rx": MetricStats(total=800, reboot_count=0, count=96)
}
),
]
def test_returns_metric_stats(self, daily_counter_data):
"""Returns a MetricStats object."""
result = _aggregate_daily_counter_to_summary(daily_counter_data, "packets_rx")
assert isinstance(result, MetricStats)
def test_sums_totals(self, daily_counter_data):
"""Sums totals across all days."""
result = _aggregate_daily_counter_to_summary(daily_counter_data, "packets_rx")
assert result.total == 3300 # 1000 + 1500 + 800
def test_sums_reboots(self, daily_counter_data):
"""Sums reboot counts across all days."""
result = _aggregate_daily_counter_to_summary(daily_counter_data, "packets_rx")
assert result.reboot_count == 1
def test_handles_empty_list(self):
"""Handles empty daily list."""
result = _aggregate_daily_counter_to_summary([], "packets_rx")
assert result.total is None
def test_handles_missing_metric(self, daily_counter_data):
"""Handles when metric doesn't exist in daily data."""
result = _aggregate_daily_counter_to_summary(daily_counter_data, "nonexistent")
assert result.total is None
class TestAggregateMonthlyGaugeToSummary:
"""Tests for _aggregate_monthly_gauge_to_summary function."""
@pytest.fixture
def monthly_gauge_data(self):
"""Sample monthly gauge aggregates."""
return [
MonthlyAggregate(
year=2024,
month=1,
role="companion",
summary={
"battery": MetricStats(
min_value=3.6, min_time=datetime(2024, 1, 15, 4, 0),
max_value=4.0, max_time=datetime(2024, 1, 20, 14, 0),
mean=3.8, count=2976
)
}
),
MonthlyAggregate(
year=2024,
month=2,
role="companion",
summary={
"battery": MetricStats(
min_value=3.5, min_time=datetime(2024, 2, 10, 5, 0),
max_value=4.1, max_time=datetime(2024, 2, 25, 16, 0),
mean=3.9, count=2784
)
}
),
]
def test_returns_metric_stats(self, monthly_gauge_data):
"""Returns a MetricStats object."""
result = _aggregate_monthly_gauge_to_summary(monthly_gauge_data, "battery")
assert isinstance(result, MetricStats)
def test_finds_overall_min(self, monthly_gauge_data):
"""Finds minimum across all months."""
result = _aggregate_monthly_gauge_to_summary(monthly_gauge_data, "battery")
assert result.min_value == 3.5
def test_finds_overall_max(self, monthly_gauge_data):
"""Finds maximum across all months."""
result = _aggregate_monthly_gauge_to_summary(monthly_gauge_data, "battery")
assert result.max_value == 4.1
def test_computes_weighted_mean(self, monthly_gauge_data):
"""Computes weighted mean based on count."""
result = _aggregate_monthly_gauge_to_summary(monthly_gauge_data, "battery")
# Weighted: (3.8 * 2976 + 3.9 * 2784) / (2976 + 2784)
expected = (3.8 * 2976 + 3.9 * 2784) / (2976 + 2784)
assert result.mean == pytest.approx(expected, rel=0.01)
def test_handles_empty_list(self):
"""Handles empty monthly list."""
result = _aggregate_monthly_gauge_to_summary([], "battery")
assert result.min_value is None
assert result.max_value is None
class TestAggregateMonthlyCounterToSummary:
"""Tests for _aggregate_monthly_counter_to_summary function."""
@pytest.fixture
def monthly_counter_data(self):
"""Sample monthly counter aggregates."""
return [
MonthlyAggregate(
year=2024,
month=1,
role="companion",
summary={
"packets_rx": MetricStats(total=50000, reboot_count=2, count=2976)
}
),
MonthlyAggregate(
year=2024,
month=2,
role="companion",
summary={
"packets_rx": MetricStats(total=45000, reboot_count=1, count=2784)
}
),
]
def test_returns_metric_stats(self, monthly_counter_data):
"""Returns a MetricStats object."""
result = _aggregate_monthly_counter_to_summary(monthly_counter_data, "packets_rx")
assert isinstance(result, MetricStats)
def test_sums_totals(self, monthly_counter_data):
"""Sums totals across all months."""
result = _aggregate_monthly_counter_to_summary(monthly_counter_data, "packets_rx")
assert result.total == 95000
def test_sums_reboots(self, monthly_counter_data):
"""Sums reboot counts across all months."""
result = _aggregate_monthly_counter_to_summary(monthly_counter_data, "packets_rx")
assert result.reboot_count == 3
def test_handles_empty_list(self):
"""Handles empty monthly list."""
result = _aggregate_monthly_counter_to_summary([], "packets_rx")
assert result.total is None
def test_handles_missing_metric(self, monthly_counter_data):
"""Handles when metric doesn't exist in monthly data."""
result = _aggregate_monthly_counter_to_summary(monthly_counter_data, "nonexistent")
assert result.total is None