audit-labs/audit-report

Turn audit-tools evidence packages into control-mapped, auditor-ready reports. audit compliance evidence reporting https://audit-labs.dev/audit-report/

tests/test_periods.py

v1.1.0
audit-report/tests/test_periods.py history · blame · raw

62 lines · 2285 bytes

 1"""Row-level period checks (ruleset schema 2)."""
 2
 3from pathlib import Path
 4
 5import pytest
 6
 7from audit_report import periods
 8from audit_report.rules import load_ruleset
 9
10ROWS = [{"at": "2026-07-01T10:00:00Z"}, {"at": "2026-07-03"}, {"at": "2026-07-20"}, {"at": "2026-06-30"}, {"at": "soon"}]
11
12
13def test_outside_undated_and_gaps():
14    res = periods.check(ROWS, "at", "2026-07-01", "2026-07-31", max_gap_days=7)
15    assert res.rows_in_period == 3
16    assert res.outside == [{"row": 3, "date": "2026-06-30"}]
17    assert res.undated == [4]
18    assert res.first == "2026-07-01" and res.last == "2026-07-20"
19    assert res.gaps == [
20        {"from": "2026-07-04", "to": "2026-07-19", "days": 16},
21        {"from": "2026-07-21", "to": "2026-07-31", "days": 11},
22    ]
23    assert not res.ok
24
25
26def test_no_gap_limit_and_clean_period():
27    rows = [{"at": f"2026-07-{d:02d}"} for d in range(1, 32, 3)]
28    assert periods.check(rows, "at", "2026-07-01", "2026-07-31", max_gap_days=2).ok
29    assert periods.check(rows, "at", "2026-07-01", "2026-07-31").gaps == []
30    empty = periods.check([], "at", "2026-07-01", "2026-07-10", max_gap_days=3)
31    assert empty.gaps == [{"from": "2026-07-01", "to": "2026-07-10", "days": 10}]
32
33
34def _ruleset(tmp_path: Path, body: str) -> Path:
35    path = tmp_path / "r.yaml"
36    path.write_text(body)
37    return path
38
39
40RULE = """
41rules:
42  - id: x.logs
43    title: Logs present
44    table: events
45    check: {type: fail_if_any_rows, when: {column: level, op: equals, value: error}}
46    timestamp: at
47    max_gap_days: 3
48"""
49
50
51def test_schema_2_fields(tmp_path):
52    rs = load_ruleset(_ruleset(tmp_path, "schema: 2\nplatform: x\n" + RULE))
53    assert rs.schema == 2 and rs.rules[0].timestamp == "at" and rs.rules[0].max_gap_days == 3
54
55
56def test_schema_1_refuses_period_fields(tmp_path):
57    with pytest.raises(ValueError, match="need 'schema: 2'"):
58        load_ruleset(_ruleset(tmp_path, "platform: x\n" + RULE))
59    with pytest.raises(ValueError, match="unsupported ruleset schema"):
60        load_ruleset(_ruleset(tmp_path, "schema: 3\nplatform: x\nrules: []\n"))
61    with pytest.raises(ValueError, match="positive integer with a timestamp"):
62        load_ruleset(_ruleset(tmp_path, "schema: 2\nplatform: x\n" + RULE.replace("max_gap_days: 3", "max_gap_days: 0")))