forked from ChelseaKR/constituent-reconciler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_progress.py
More file actions
405 lines (332 loc) · 15.4 KB
/
Copy pathtest_progress.py
File metadata and controls
405 lines (332 loc) · 15.4 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""Tests for the UC-01 progress events and their CLI rendering.
The pipeline half injects a recording sink and asserts event ordering,
counts, and content-freedom over real runs of small fixtures; the renderer
half drives ``ConsoleProgressRenderer`` directly with fake TTY and non-TTY
streams, so no pty is needed. A final test proves the no-op default leaves
library callers' results untouched.
"""
from __future__ import annotations
import dataclasses
import inspect
import io
import json
from pathlib import Path
import pytest
from constituent_reconciler import matching, pipeline
from constituent_reconciler.cli import main
from constituent_reconciler.config import ExtractConfig, Recipe
from constituent_reconciler.progress import (
NULL_SINK,
STAGES,
ConsoleProgressRenderer,
NullProgressSink,
ProgressEvent,
)
EXAMPLES = Path(__file__).resolve().parents[1] / "examples" / "intake-demo"
class RecordingSink:
"""Sink that keeps every event for assertions."""
def __init__(self) -> None:
self.events: list[ProgressEvent] = []
def emit(self, event: ProgressEvent) -> None:
self.events.append(event)
class _Stream(io.StringIO):
"""A StringIO whose isatty() answer is chosen by the test."""
def __init__(self, *, tty: bool) -> None:
super().__init__()
self._tty = tty
def isatty(self) -> bool:
return self._tty
def _fixture_recipe(tmp_path: Path, *, with_text: bool) -> Recipe:
"""A two-file incoming directory: one CSV, and optionally one text intake."""
incoming = tmp_path / "incoming"
incoming.mkdir(parents=True)
(incoming / "a.csv").write_text("first,last\nAda,Lovelace\nGrace,Hopper\n", encoding="utf-8")
if with_text:
(incoming / "note.txt").write_text(
"First Name: Jean\nLast Name: Bartik\n", encoding="utf-8"
)
return Recipe(
incoming=incoming,
mapping={"first_name": "first", "last_name": "last"},
fields=("first_name", "last_name"),
extract=ExtractConfig(backend="pdfplumber"),
)
def _no_pairs(monkeypatch: pytest.MonkeyPatch) -> None:
"""Skip Splink so the fixture runs stay fast and deterministic."""
monkeypatch.setattr(matching, "score_pairs", lambda records, fields, prior: [])
# ---------------------------------------------------------------------------
# Event ordering and counts
# ---------------------------------------------------------------------------
def test_run_emits_ordered_events_with_counts(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_no_pairs(monkeypatch)
sink = RecordingSink()
pipeline.run(_fixture_recipe(tmp_path, with_text=True), progress=sink)
assert [(e.stage, e.status) for e in sink.events] == [
("ingest", "started"),
("extract", "started"),
("ingest", "advanced"), # a.csv
("extract", "advanced"), # note.txt
("ingest", "advanced"),
("extract", "finished"),
("ingest", "finished"),
("normalize", "started"),
("normalize", "advanced"),
("normalize", "advanced"),
("normalize", "advanced"),
("normalize", "finished"),
("score", "started"),
("score", "finished"),
]
by_key = {(e.stage, e.status): e for e in sink.events}
assert (by_key["ingest", "started"].completed, by_key["ingest", "started"].total) == (0, 2)
assert (by_key["ingest", "finished"].completed, by_key["ingest", "finished"].total) == (2, 2)
assert (by_key["extract", "finished"].completed, by_key["extract", "finished"].total) == (1, 1)
assert by_key["normalize", "finished"].completed == 3
assert by_key["normalize", "finished"].total == 3
# Durations ride on the finish of each stage the run summary times;
# extraction is interleaved with ingest and has no clock of its own.
assert by_key["ingest", "finished"].duration_seconds is not None
assert by_key["normalize", "finished"].duration_seconds is not None
assert by_key["score", "finished"].duration_seconds is not None
assert by_key["extract", "finished"].duration_seconds is None
# The score stage has no denominator before it runs, so no counts.
assert by_key["score", "started"].total is None
assert by_key["score", "finished"].completed is None
def test_run_without_documents_emits_no_extract_events(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_no_pairs(monkeypatch)
sink = RecordingSink()
pipeline.run(_fixture_recipe(tmp_path, with_text=False), progress=sink)
assert all(event.stage != "extract" for event in sink.events)
assert [(e.stage, e.status) for e in sink.events][:2] == [
("ingest", "started"),
("ingest", "advanced"),
]
def test_export_emits_write_then_review_artifact_events(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_no_pairs(monkeypatch)
result = pipeline.run(_fixture_recipe(tmp_path, with_text=True))
sink = RecordingSink()
pipeline.export(
result,
_fixture_recipe(tmp_path / "again", with_text=True),
out_dir=tmp_path / "out",
progress=sink,
)
assert [(e.stage, e.status) for e in sink.events] == [
("write", "started"),
("write", "finished"),
("review_artifact", "started"),
("review_artifact", "finished"),
]
write_finished = sink.events[1]
assert (write_finished.completed, write_finished.total) == (3, 3)
assert write_finished.duration_seconds is not None
review_finished = sink.events[3]
assert (review_finished.completed, review_finished.total) == (0, 0)
assert review_finished.duration_seconds is not None
def test_dry_run_export_emits_the_same_events_as_a_real_export(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Both export stages still execute under --dry-run (the connector
# classifies records without writing; the review queue is written either
# way), so the event stream matches a real export in everything but the
# measured durations.
_no_pairs(monkeypatch)
recipe = _fixture_recipe(tmp_path, with_text=True)
result = pipeline.run(recipe)
wet, dry = RecordingSink(), RecordingSink()
pipeline.export(result, recipe, out_dir=tmp_path / "wet", progress=wet)
pipeline.export(result, recipe, out_dir=tmp_path / "dry", dry_run=True, progress=dry)
def shape(sink: RecordingSink) -> list[tuple[str, str, int | None, int | None]]:
return [(e.stage, e.status, e.completed, e.total) for e in sink.events]
assert shape(dry) == shape(wet)
# ---------------------------------------------------------------------------
# Content-freedom
# ---------------------------------------------------------------------------
def test_events_are_content_free(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
_no_pairs(monkeypatch)
recipe = _fixture_recipe(tmp_path, with_text=True)
sink = RecordingSink()
result = pipeline.run(recipe, progress=sink)
pipeline.export(result, recipe, out_dir=tmp_path / "out", progress=sink)
leak_candidates = (
"Ada",
"Lovelace",
"Grace",
"Hopper",
"Jean",
"Bartik",
"a.csv",
"note.txt",
"incoming:",
str(tmp_path),
)
assert sink.events
for event in sink.events:
# Structural: the event has exactly the content-free fields, and the
# string-typed ones come from fixed vocabularies.
payload = dataclasses.asdict(event)
assert set(payload) == {"stage", "status", "completed", "total", "duration_seconds"}
assert event.stage in STAGES
assert event.status in ("started", "advanced", "finished")
assert event.completed is None or isinstance(event.completed, int)
assert event.total is None or isinstance(event.total, int)
assert event.duration_seconds is None or isinstance(event.duration_seconds, float)
serialized = json.dumps(payload)
for candidate in leak_candidates:
assert candidate not in serialized
# ---------------------------------------------------------------------------
# No-op default: zero behavior change for library callers
# ---------------------------------------------------------------------------
def test_default_sink_is_a_noop_and_results_are_unchanged(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
assert isinstance(
inspect.signature(pipeline.run).parameters["progress"].default, NullProgressSink
)
assert inspect.signature(pipeline.export).parameters["progress"].default is NULL_SINK
assert NULL_SINK.emit(ProgressEvent("score", "started")) is None
_no_pairs(monkeypatch)
silent = pipeline.run(_fixture_recipe(tmp_path / "a", with_text=True))
observed = pipeline.run(
_fixture_recipe(tmp_path / "b", with_text=True), progress=RecordingSink()
)
assert silent.records == observed.records
assert silent.pairs == observed.pairs
assert silent.golden == observed.golden
# ---------------------------------------------------------------------------
# Renderer: non-TTY
# ---------------------------------------------------------------------------
def test_non_tty_rendering_writes_stable_records_without_control_characters() -> None:
stream = _Stream(tty=False)
renderer = ConsoleProgressRenderer(stream, min_redraw_seconds=0.0)
renderer.emit(ProgressEvent("ingest", "started", completed=0, total=2))
renderer.emit(ProgressEvent("ingest", "advanced", completed=1, total=2))
renderer.emit(ProgressEvent("ingest", "finished", completed=2, total=2, duration_seconds=0.5))
renderer.emit(ProgressEvent("score", "started"))
renderer.emit(ProgressEvent("score", "finished", duration_seconds=1.25))
renderer.emit(ProgressEvent("review_artifact", "started", completed=0, total=4))
renderer.close() # a no-op off-TTY: no line is ever left open
out = stream.getvalue()
assert "\r" not in out
assert "\x1b" not in out
assert out.splitlines() == [
"progress: ingest 0/2 started",
"progress: ingest 2/2 done in 0.50s",
"progress: score started",
"progress: score done in 1.25s",
"progress: review artifact 0/4 started",
]
# ---------------------------------------------------------------------------
# Renderer: TTY
# ---------------------------------------------------------------------------
def test_tty_rendering_updates_one_line_in_place() -> None:
stream = _Stream(tty=True)
renderer = ConsoleProgressRenderer(stream, min_redraw_seconds=0.0)
renderer.emit(ProgressEvent("ingest", "started", completed=0, total=2))
renderer.emit(ProgressEvent("ingest", "advanced", completed=1, total=2))
renderer.emit(ProgressEvent("ingest", "finished", completed=2, total=2, duration_seconds=0.1))
out = stream.getvalue()
# One newline total: the stage finish. Every repaint rewrites in place.
assert out.count("\n") == 1
frames = [frame.rstrip() for frame in out.split("\r") if frame]
assert frames == [
"progress: ingest 0/2 started",
"progress: ingest 1/2",
"progress: ingest 2/2 done in 0.10s",
]
# The shorter middle frame is padded to erase the longer first one.
assert "\rprogress: ingest 1/2 " in out
def test_tty_advanced_redraws_are_throttled_but_start_and_finish_paint() -> None:
stream = _Stream(tty=True)
renderer = ConsoleProgressRenderer(stream, min_redraw_seconds=3600.0)
renderer.emit(ProgressEvent("normalize", "started", completed=0, total=9))
renderer.emit(ProgressEvent("normalize", "advanced", completed=1, total=9))
renderer.emit(ProgressEvent("normalize", "advanced", completed=2, total=9))
renderer.emit(ProgressEvent("normalize", "finished", completed=9, total=9))
out = stream.getvalue()
assert "0/9" in out
assert "1/9" in out # the first repaint after start always lands
assert "2/9" not in out # the next one is inside the redraw window
assert "9/9 done" in out
def test_close_terminates_an_open_tty_line_once() -> None:
stream = _Stream(tty=True)
renderer = ConsoleProgressRenderer(stream, min_redraw_seconds=0.0)
renderer.emit(ProgressEvent("ingest", "started", completed=0, total=3))
renderer.close()
renderer.close()
assert stream.getvalue() == "\rprogress: ingest 0/3 started\n"
# ---------------------------------------------------------------------------
# CLI wiring
# ---------------------------------------------------------------------------
def test_cli_run_renders_progress_records_on_stderr(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
# capsys replaces stderr with a non-TTY stream, so this covers the
# pipe/log rendering end to end: stable records, no control characters.
code = main(
[
"run",
"--config",
str(EXAMPLES / "recipe.toml"),
"--out",
str(tmp_path),
"--dry-run",
]
)
assert code == 0
err = capsys.readouterr().err
assert "\r" not in err
assert "progress: ingest 0/2 started" in err
assert "progress: ingest 2/2 done in " in err
assert "progress: normalize 27/27 done in " in err
assert "progress: score done in " in err
assert "progress: write 0/21 started" in err
assert "progress: write 21/21 done in " in err
assert "progress: review artifact" in err
# The demo recipe ingests CSVs only, so no extract stage is reported.
assert "progress: extract" not in err
class _FailingStream(io.StringIO):
"""A text stream whose writes start failing after a set number of calls."""
def __init__(self, fail_after: int = 0, tty: bool = False) -> None:
super().__init__()
self._fail_after = fail_after
self._tty = tty
self.writes = 0
def isatty(self) -> bool:
return self._tty
def write(self, text: str) -> int:
self.writes += 1
if self.writes > self._fail_after:
raise BrokenPipeError("stream went away")
return super().write(text)
def test_a_broken_stream_latches_the_renderer_off_instead_of_raising() -> None:
# The contract in the renderer docstring: rendering never raises into
# the pipeline, because an emit sits between a connector write and its
# provenance entry. A stream that fails on the first write, mid-run,
# or at close must be absorbed and the renderer must go quiet.
for fail_after in (0, 1, 2):
renderer = ConsoleProgressRenderer(_FailingStream(fail_after=fail_after))
for status in ("started", "advanced", "finished"):
renderer.emit(ProgressEvent(stage="write", status=status, completed=1, total=2))
renderer.close()
renderer.close()
tty = ConsoleProgressRenderer(_FailingStream(fail_after=1, tty=True))
tty.emit(ProgressEvent(stage="ingest", status="started", completed=0, total=2))
tty.emit(ProgressEvent(stage="ingest", status="finished", completed=2, total=2))
tty.close()
def test_apply_wires_the_renderer_exactly_like_run() -> None:
# Pins the CLI wiring for ``reconcile apply`` (the end-to-end stderr
# test above covers ``run``): both commands construct the stderr
# renderer inside try/finally and pass it to the pipeline.
from constituent_reconciler import cli
for command in (cli._cmd_run, cli._cmd_apply):
source = inspect.getsource(command)
assert "ConsoleProgressRenderer" in source
assert "progress=" in source
assert "finally:" in source and ".close()" in source