forked from ChelseaKR/qfer-preflight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
214 lines (165 loc) · 6.48 KB
/
Copy pathtest_cli.py
File metadata and controls
214 lines (165 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""CLI behaviour, exit codes and report rendering."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from qfer_preflight.cli import EXIT_FINDINGS, EXIT_OK, EXIT_USAGE, main
from qfer_preflight.model import (
Citation,
Finding,
Report,
Rule,
Severity,
Status,
)
from qfer_preflight.report import to_text
FIXTURES = Path(__file__).parent / "fixtures"
def test_check_clean_file_exits_zero(capsys: pytest.CaptureFixture[str]) -> None:
code = main(["check", str(FIXTURES / "1306a_s1_clean.csv"), "--profile", "CEC-1306A-S1"])
out = capsys.readouterr().out
assert code == EXIT_OK
assert "UNVALIDATED" in out
assert "NOT reported as clean" in out
def test_check_dirty_file_exits_one(capsys: pytest.CaptureFixture[str]) -> None:
code = main(["check", str(FIXTURES / "1306a_s1_dirty.csv"), "--profile", "CEC-1306A-S1"])
assert code == EXIT_FINDINGS
assert "ERROR" in capsys.readouterr().out
def test_empty_file_exits_one(capsys: pytest.CaptureFixture[str]) -> None:
code = main(["check", str(FIXTURES / "empty.csv"), "--profile", "CEC-1306A-S1"])
capsys.readouterr()
assert code == EXIT_FINDINGS
def test_strict_turns_unvalidated_into_a_failure(
capsys: pytest.CaptureFixture[str],
) -> None:
args = ["check", str(FIXTURES / "1306a_s1_clean.csv"), "--profile", "CEC-1306A-S1"]
assert main(args) == EXIT_OK
capsys.readouterr()
assert main([*args, "--strict"]) == EXIT_FINDINGS
capsys.readouterr()
def test_json_output_is_valid_json(capsys: pytest.CaptureFixture[str]) -> None:
main(
[
"check",
str(FIXTURES / "1306a_s1_clean.csv"),
"--profile",
"CEC-1306A-S1",
"--format",
"json",
]
)
payload = json.loads(capsys.readouterr().out)
assert payload["status"] == "unvalidated"
assert payload["profile"]["id"] == "CEC-1306A-S1"
assert payload["input"]["sha256"]
assert payload["rules_not_evaluated"]
def test_unknown_profile_exits_two(capsys: pytest.CaptureFixture[str]) -> None:
code = main(["check", str(FIXTURES / "empty.csv"), "--profile", "NOPE"])
assert code == EXIT_USAGE
assert "unknown profile" in capsys.readouterr().err
def test_missing_file_exits_two(capsys: pytest.CaptureFixture[str]) -> None:
code = main(["check", "/nonexistent/nope.csv", "--profile", "CEC-1306A-S1"])
assert code == EXIT_USAGE
assert "could not read" in capsys.readouterr().err
def test_rules_command_lists_citations(capsys: pytest.CaptureFixture[str]) -> None:
assert main(["rules"]) == EXIT_OK
out = capsys.readouterr().out
assert "QP001" in out
assert "energy.ca.gov" in out
assert "NOT IMPLEMENTED" in out
def test_rules_command_json(capsys: pytest.CaptureFixture[str]) -> None:
assert main(["rules", "--format", "json", "--profile", "CEC-1308C"]) == EXIT_OK
payload = json.loads(capsys.readouterr().out)
assert all("citation" in rule for rule in payload)
def test_rules_command_rejects_bad_profile(
capsys: pytest.CaptureFixture[str],
) -> None:
assert main(["rules", "--profile", "NOPE"]) == EXIT_USAGE
capsys.readouterr()
def test_profiles_command(capsys: pytest.CaptureFixture[str]) -> None:
assert main(["profiles"]) == EXIT_OK
out = capsys.readouterr().out
for pid in ("CEC-1306A-S1", "CEC-1306B", "CEC-1308B-S1", "CEC-1308C"):
assert pid in out
# The published header irregularities must be visible, not silently fixed.
assert "RetailRatClass" in out
assert "NumberofCustomers" in out
def test_text_report_renders_findings() -> None:
report = Report(
tool="qfer-preflight",
tool_version="0.0.0",
profile_id="X",
profile_title="X",
input_name="x.csv",
input_sha256="deadbeef",
)
report.findings.append(Finding("QP010", Severity.ERROR, "bad year", row=2, column="Year"))
report.findings.append(Finding("QP030", Severity.WARNING, "spans quarters"))
text = to_text(report)
assert "row 2, Year" in text
assert "[ERROR] QP010" in text
assert "[WARN] QP030" in text
def test_text_report_handles_no_findings() -> None:
report = Report(
tool="t",
tool_version="0",
profile_id="X",
profile_title="X",
input_name="x",
input_sha256="h",
)
assert "Findings: none" in to_text(report)
assert report.status is Status.PASS
# ---------------------------------------------------------------------------
# Model guardrails
# ---------------------------------------------------------------------------
def _citation() -> Citation:
return Citation(source="s", url="https://example.invalid", locator="l")
def test_citation_rejects_empty_fields() -> None:
for kwargs in (
{"source": "", "url": "u", "locator": "l"},
{"source": "s", "url": "", "locator": "l"},
{"source": "s", "url": "u", "locator": ""},
):
with pytest.raises(ValueError):
Citation(**kwargs) # type: ignore[arg-type]
def test_rule_requires_a_reason_when_unimplemented() -> None:
with pytest.raises(ValueError):
Rule(
id="QP900",
title="t",
severity=Severity.ERROR,
citation=_citation(),
implemented=False,
)
def test_implemented_rule_may_not_carry_an_unimplemented_reason() -> None:
with pytest.raises(ValueError):
Rule(
id="QP901",
title="t",
severity=Severity.ERROR,
citation=_citation(),
unimplemented_reason="because",
)
def test_implemented_rule_may_not_have_unvalidated_severity() -> None:
with pytest.raises(ValueError):
Rule(
id="QP902",
title="t",
severity=Severity.UNVALIDATED,
citation=_citation(),
)
def test_rule_requires_id_and_title() -> None:
with pytest.raises(ValueError):
Rule(id="", title="t", severity=Severity.ERROR, citation=_citation())
with pytest.raises(ValueError):
Rule(id="QP903", title=" ", severity=Severity.ERROR, citation=_citation())
def test_severity_and_status_stringify() -> None:
assert str(Severity.ERROR) == "error"
assert str(Status.PASS) == "pass"
def test_citation_render_includes_authority() -> None:
cited = Citation(
source="doc", url="https://example.invalid", locator="p1", authority="Title 20"
)
rendered = cited.render()
assert "Title 20" in rendered
assert "doc" in rendered