97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
"""Unit tests for analysis.data_audit."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from cerbero_bite.analysis.data_audit import ( # noqa: PLC2701
|
|
_detect_gaps,
|
|
_expected_ticks,
|
|
_max_zero_streak,
|
|
)
|
|
|
|
|
|
def test_expected_ticks_basic() -> None:
|
|
since = datetime(2026, 5, 12, 12, 0, tzinfo=UTC)
|
|
until = datetime(2026, 5, 12, 13, 0, tzinfo=UTC)
|
|
# 12:00, 12:15, 12:30, 12:45 → 4 ticks before 13:00
|
|
assert _expected_ticks(since, until) == 4
|
|
|
|
|
|
def test_expected_ticks_inclusive_left_exclusive_right() -> None:
|
|
since = datetime(2026, 5, 12, 12, 0, tzinfo=UTC)
|
|
until = datetime(2026, 5, 12, 12, 15, tzinfo=UTC)
|
|
# Only 12:00
|
|
assert _expected_ticks(since, until) == 1
|
|
|
|
|
|
def test_expected_ticks_unaligned_since_rounds_up() -> None:
|
|
since = datetime(2026, 5, 12, 12, 7, tzinfo=UTC)
|
|
until = datetime(2026, 5, 12, 12, 30, tzinfo=UTC)
|
|
# First aligned tick after 12:07 is 12:15, then 12:30 is excluded
|
|
assert _expected_ticks(since, until) == 1
|
|
|
|
|
|
def test_expected_ticks_non_multiple_span_uses_ceiling() -> None:
|
|
# Span = 16 min (12:00 → 12:16): ticks 12:00 AND 12:15 are < 12:16.
|
|
# floor(16/15) = 1 would under-count; the correct answer is 2.
|
|
since = datetime(2026, 5, 12, 12, 0, tzinfo=UTC)
|
|
until = datetime(2026, 5, 12, 12, 16, tzinfo=UTC)
|
|
assert _expected_ticks(since, until) == 2
|
|
|
|
|
|
def test_expected_ticks_exact_quarter_boundary_is_excluded() -> None:
|
|
# Until landing exactly on a */15 tick: that tick is NOT counted
|
|
# because the window is half-open on the right.
|
|
since = datetime(2026, 5, 12, 12, 0, tzinfo=UTC)
|
|
until = datetime(2026, 5, 12, 12, 45, tzinfo=UTC)
|
|
# Ticks at 12:00, 12:15, 12:30 → 3 (12:45 excluded)
|
|
assert _expected_ticks(since, until) == 3
|
|
|
|
|
|
def test_detect_gaps_returns_empty_when_no_gap() -> None:
|
|
ts = [
|
|
datetime(2026, 5, 12, 12, 0, tzinfo=UTC),
|
|
datetime(2026, 5, 12, 12, 15, tzinfo=UTC),
|
|
datetime(2026, 5, 12, 12, 30, tzinfo=UTC),
|
|
]
|
|
assert _detect_gaps(ts) == ()
|
|
|
|
|
|
def test_detect_gaps_flags_above_threshold() -> None:
|
|
ts = [
|
|
datetime(2026, 5, 12, 12, 0, tzinfo=UTC),
|
|
datetime(2026, 5, 12, 12, 45, tzinfo=UTC), # 45-min gap
|
|
datetime(2026, 5, 12, 13, 0, tzinfo=UTC),
|
|
]
|
|
gaps = _detect_gaps(ts)
|
|
assert len(gaps) == 1
|
|
assert gaps[0].gap_minutes == 45
|
|
assert gaps[0].prev_timestamp == ts[0]
|
|
assert gaps[0].next_timestamp == ts[1]
|
|
|
|
|
|
def test_detect_gaps_ignores_threshold_boundary() -> None:
|
|
# 20-min gap is exactly the threshold → NOT flagged (strict >)
|
|
ts = [
|
|
datetime(2026, 5, 12, 12, 0, tzinfo=UTC),
|
|
datetime(2026, 5, 12, 12, 20, tzinfo=UTC),
|
|
]
|
|
assert _detect_gaps(ts) == ()
|
|
|
|
|
|
def test_max_zero_streak_empty() -> None:
|
|
assert _max_zero_streak([]) == 0
|
|
|
|
|
|
def test_max_zero_streak_no_zeros() -> None:
|
|
assert _max_zero_streak([1, 1, 1, 1]) == 0
|
|
|
|
|
|
def test_max_zero_streak_single_zero_block() -> None:
|
|
assert _max_zero_streak([1, 0, 0, 0, 1, 0, 1]) == 3
|
|
|
|
|
|
def test_max_zero_streak_all_zeros() -> None:
|
|
assert _max_zero_streak([0, 0, 0]) == 3
|