forked from ChelseaKR/ca-tariff-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
267 lines (219 loc) · 10.1 KB
/
Copy pathtest_cli.py
File metadata and controls
267 lines (219 loc) · 10.1 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Command line behaviour, including the coverage gate."""
from __future__ import annotations
import hashlib
import json
import os
from pathlib import Path
import pytest
from ca_tariff_parse.cli import EXIT_COVERAGE, EXIT_ERROR, EXIT_OK, main
def test_parse_writes_json_to_stdout(
complete_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
assert main(["parse", str(complete_fixture)]) == EXIT_OK
payload = json.loads(capsys.readouterr().out)
assert payload["schema"] == "ca-tariff-parse/parsed-schedule/v1"
assert payload["charges"]
assert "not a calculation of what any customer owes" in payload["disclaimer"]
def test_parse_writes_json_to_a_file(complete_fixture: Path, tmp_path: Path) -> None:
out = tmp_path / "parsed.json"
assert main(["parse", str(complete_fixture), "-o", str(out)]) == EXIT_OK
payload = json.loads(out.read_text(encoding="utf-8"))
assert payload["coverage"]["fully_recognized"] is True
def test_parse_still_emits_when_coverage_is_short(
unknown_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""The gate must not hide the output that explains why it failed."""
code = main(["parse", str(unknown_fixture), "--min-coverage", "0.99"])
captured = capsys.readouterr()
assert code == EXIT_COVERAGE
assert json.loads(captured.out)["unparsed"]
assert "below the required" in captured.err
def test_parse_passes_a_satisfiable_coverage_gate(unknown_fixture: Path) -> None:
assert main(["parse", str(unknown_fixture), "--min-coverage", "0.5"]) == EXIT_OK
def test_coverage_report_lists_unparsed_sections(
unknown_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
assert main(["coverage", str(unknown_fixture)]) == EXIT_OK
out = capsys.readouterr().out
assert "unparsed:" in out
assert "SYNTHETIC" in out
assert "not rate advice" in out
def test_coverage_gate_returns_its_own_exit_code(unknown_fixture: Path) -> None:
assert main(["coverage", str(unknown_fixture), "--min-coverage", "1.0"]) == EXIT_COVERAGE
def test_sources_lists_the_manifest(capsys: pytest.CaptureFixture[str]) -> None:
assert main(["sources"]) == EXIT_OK
out = capsys.readouterr().out
assert "smud-r-tod" in out
assert "Sacramento Municipal Utility District" in out
def test_sources_reports_an_empty_manifest(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
manifest = tmp_path / "sources.toml"
manifest.write_text("", encoding="utf-8")
assert main(["sources", "--manifest", str(manifest)]) == EXIT_OK
assert "no documents registered" in capsys.readouterr().out
def test_sources_distinguishes_missing_present_and_mismatched(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A file at the manifest's filename is only `present` if it matches the
pinned SHA-256; a corrupted or revised download must read `mismatched`."""
good_bytes = b"rate schedule bytes"
manifest = tmp_path / "sources.toml"
manifest.write_text(
"[[document]]\n"
'id = "doc"\n'
'schedule = "R"\n'
'title = "Residential"\n'
'publisher = "Test Utility"\n'
'url = "https://example.com/doc.pdf"\n'
'filename = "doc.pdf"\n'
f'sha256 = "{hashlib.sha256(good_bytes).hexdigest()}"\n'
'retrieved_at = "2026-01-01"\n'
"pages = 1\n"
"bytes = 19\n",
encoding="utf-8",
)
directory = tmp_path / "sources"
directory.mkdir()
# Not fetched: no file at all.
assert main(["sources", "--manifest", str(manifest), "--dir", str(directory)]) == EXIT_OK
assert "not fetched" in capsys.readouterr().out
# Present and matching.
target = directory / "doc.pdf"
target.write_bytes(good_bytes)
assert main(["sources", "--manifest", str(manifest), "--dir", str(directory)]) == EXIT_OK
out = capsys.readouterr().out
assert "present" in out and "mismatched" not in out
# Present but mismatched (truncated / revised / hand-edited bytes).
target.write_bytes(b"some other bytes")
assert main(["sources", "--manifest", str(manifest), "--dir", str(directory)]) == EXIT_OK
out = capsys.readouterr().out
assert "mismatched" in out and "not fetched" not in out
def test_sources_reports_mismatched_case_insensitively(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""digest()/hexdigest() always return lowercase hex; a hand-edited
manifest entry with an uppercase sha256 must not be reported as
`mismatched` for an otherwise byte-identical file."""
good_bytes = b"rate schedule bytes"
manifest = tmp_path / "sources.toml"
manifest.write_text(
"[[document]]\n"
'id = "doc"\n'
'schedule = "R"\n'
'title = "Residential"\n'
'publisher = "Test Utility"\n'
'url = "https://example.com/doc.pdf"\n'
'filename = "doc.pdf"\n'
f'sha256 = "{hashlib.sha256(good_bytes).hexdigest().upper()}"\n'
'retrieved_at = "2026-01-01"\n'
"pages = 1\n"
"bytes = 19\n",
encoding="utf-8",
)
directory = tmp_path / "sources"
directory.mkdir()
(directory / "doc.pdf").write_bytes(good_bytes)
assert main(["sources", "--manifest", str(manifest), "--dir", str(directory)]) == EXIT_OK
out = capsys.readouterr().out
assert "present" in out and "mismatched" not in out
def test_sources_reports_a_directory_as_mismatched_not_a_crash(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A directory sitting at the manifest's filename is not the document
the parser was audited against, and is not safe to open as a file — it
must read as `mismatched`, not raise IsADirectoryError."""
manifest = tmp_path / "sources.toml"
manifest.write_text(
"[[document]]\n"
'id = "doc"\n'
'schedule = "R"\n'
'title = "Residential"\n'
'publisher = "Test Utility"\n'
'url = "https://example.com/doc.pdf"\n'
'filename = "doc.pdf"\n'
f'sha256 = "{hashlib.sha256(b"anything").hexdigest()}"\n'
'retrieved_at = "2026-01-01"\n'
"pages = 1\n"
"bytes = 19\n",
encoding="utf-8",
)
directory = tmp_path / "sources"
directory.mkdir()
(directory / "doc.pdf").mkdir() # a directory, not a document
assert main(["sources", "--manifest", str(manifest), "--dir", str(directory)]) == EXIT_OK
out = capsys.readouterr().out
assert "mismatched" in out
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="FIFOs are not available on this platform")
def test_sources_reports_a_fifo_as_mismatched_without_hanging(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A FIFO with nothing writing to it must never be read: read_bytes()
on one blocks forever. It must read as `mismatched`, not hang."""
manifest = tmp_path / "sources.toml"
manifest.write_text(
"[[document]]\n"
'id = "doc"\n'
'schedule = "R"\n'
'title = "Residential"\n'
'publisher = "Test Utility"\n'
'url = "https://example.com/doc.pdf"\n'
'filename = "doc.pdf"\n'
f'sha256 = "{hashlib.sha256(b"anything").hexdigest()}"\n'
'retrieved_at = "2026-01-01"\n'
"pages = 1\n"
"bytes = 19\n",
encoding="utf-8",
)
directory = tmp_path / "sources"
directory.mkdir()
os.mkfifo(directory / "doc.pdf") # a FIFO, not a document; nothing writes to it
assert main(["sources", "--manifest", str(manifest), "--dir", str(directory)]) == EXIT_OK
out = capsys.readouterr().out
assert "mismatched" in out
def test_a_missing_manifest_is_an_error(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
assert main(["sources", "--manifest", str(tmp_path / "nope.toml")]) == EXIT_ERROR
assert "not found" in capsys.readouterr().err
def test_verify_source_reports_a_missing_document(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
code = main(["verify-source", "--id", "smud-r-tod", "--dir", str(tmp_path)])
assert code == EXIT_ERROR
assert "ca-tariff-parse fetch" in capsys.readouterr().err
def test_an_unknown_document_id_is_an_error(
complete_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
code = main(["parse", str(complete_fixture), "--id", "not-a-real-id"])
assert code == EXIT_ERROR
assert "unknown document id" in capsys.readouterr().err
def test_parsing_with_an_id_rejects_a_document_whose_bytes_do_not_match(
complete_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A manifest id is a claim about which bytes were read."""
code = main(["parse", str(complete_fixture), "--id", "smud-r-tod"])
assert code == EXIT_ERROR
assert "does not match the manifest" in capsys.readouterr().err
def test_parse_reads_an_unregistered_document_with_a_named_profile(
keyword_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""A profile can be named on the command line for a file not in the manifest."""
assert main(["parse", str(keyword_fixture), "--profile", "pge-tariff-book"]) == EXIT_OK
payload = json.loads(capsys.readouterr().out)
sections = {charge["price"]["amount"]["provenance"]["section"] for charge in payload["charges"]}
assert sections == {"RATES", "SPECIALCONDITIONS"}
assert any(charge["price"]["amount"]["value"].startswith("-") for charge in payload["charges"])
def test_parse_without_a_profile_refuses_what_that_profile_would_have_read(
keyword_fixture: Path, capsys: pytest.CaptureFixture[str]
) -> None:
assert main(["parse", str(keyword_fixture)]) == EXIT_OK
payload = json.loads(capsys.readouterr().out)
assert not any(
charge["price"]["amount"]["value"].startswith("-") for charge in payload["charges"]
)
assert {
charge["price"]["amount"]["provenance"]["section"] for charge in payload["charges"]
} == {"preamble"}
def test_an_unknown_profile_is_rejected_by_the_command_line(keyword_fixture: Path) -> None:
"""A misspelled profile is an error, never a silent fall back to the default."""
with pytest.raises(SystemExit):
main(["parse", str(keyword_fixture), "--profile", "no-such-publisher"])