tests: napraw flaky asercję pokrycia w heartbeacie #26
@@ -60,25 +60,49 @@ def test_current_search_registration_round_trip():
|
|||||||
assert not lib._current_search
|
assert not lib._current_search
|
||||||
|
|
||||||
|
|
||||||
def test_search_fills_progress_with_live_positions_and_total(tmp_path, monkeypatch):
|
def _write_two_chunks(tmp_path):
|
||||||
# End to end against the real scan: total_bytes matches the chunk files on
|
|
||||||
# disk, and once finished the recorded offsets cover them.
|
|
||||||
monkeypatch.setattr(search_bot, "DATABASE_PATH", str(tmp_path) + "/")
|
|
||||||
(tmp_path / "0_chunk.txt").write_text("10.1/a\n10.1/b\n", encoding="utf-8")
|
(tmp_path / "0_chunk.txt").write_text("10.1/a\n10.1/b\n", encoding="utf-8")
|
||||||
(tmp_path / "1_chunk.txt").write_text("10.1/c\n", encoding="utf-8")
|
(tmp_path / "1_chunk.txt").write_text("10.1/c\n", encoding="utf-8")
|
||||||
expected_total = sum(
|
return sum(
|
||||||
(tmp_path / name).stat().st_size for name in ("0_chunk.txt", "1_chunk.txt")
|
(tmp_path / name).stat().st_size for name in ("0_chunk.txt", "1_chunk.txt")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_search_fills_progress_and_reaches_full_coverage(tmp_path, monkeypatch):
|
||||||
|
# Coverage must be measured on a search that CANNOT stop early. Once every
|
||||||
|
# queried DOI is found the consumer signals TERM and the producers stop
|
||||||
|
# mid-file, so a search for a DOI that exists reaches an arbitrary offset -
|
||||||
|
# asserting 100% there is a race (it failed roughly one run in two).
|
||||||
|
# An absent DOI forces the whole database to be read.
|
||||||
|
monkeypatch.setattr(search_bot, "DATABASE_PATH", str(tmp_path) + "/")
|
||||||
|
expected_total = _write_two_chunks(tmp_path)
|
||||||
|
|
||||||
|
progress = {}
|
||||||
|
search_bot.search_for_doi([("10.9/absent", "DATA")], [], _LOG, progress=progress)
|
||||||
|
|
||||||
|
assert progress["total_bytes"] == expected_total
|
||||||
|
assert progress["chunk_files"] == 2
|
||||||
|
done, total, percent = lib._progress_summary(progress)
|
||||||
|
assert total == expected_total
|
||||||
|
assert done == expected_total # nothing stopped it: whole DB scanned
|
||||||
|
assert percent == pytest.approx(100.0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_progress_is_populated_for_a_search_that_finds_its_target(tmp_path, monkeypatch):
|
||||||
|
# The early-termination case: the target is found, so coverage is whatever
|
||||||
|
# the producers reached. Assert what IS deterministic - the total is known,
|
||||||
|
# progress is bounded and sane, and the hit is reported.
|
||||||
|
monkeypatch.setattr(search_bot, "DATABASE_PATH", str(tmp_path) + "/")
|
||||||
|
expected_total = _write_two_chunks(tmp_path)
|
||||||
|
|
||||||
progress = {}
|
progress = {}
|
||||||
result, _positions, _interrupted = search_bot.search_for_doi(
|
result, _positions, _interrupted = search_bot.search_for_doi(
|
||||||
[("10.1/c", "DATA")], [], _LOG, progress=progress
|
[("10.1/c", "DATA")], [], _LOG, progress=progress
|
||||||
)
|
)
|
||||||
|
|
||||||
assert progress["total_bytes"] == expected_total
|
assert progress["total_bytes"] == expected_total
|
||||||
assert progress["chunk_files"] == 2
|
|
||||||
done, total, percent = lib._progress_summary(progress)
|
done, total, percent = lib._progress_summary(progress)
|
||||||
assert total == expected_total
|
assert total == expected_total
|
||||||
assert done == expected_total # whole DB scanned
|
assert 0 <= done <= total # bounded, never nonsense
|
||||||
assert percent == pytest.approx(100.0)
|
assert 0.0 <= percent <= 100.0
|
||||||
assert [r for r in result if r["DOI"] == "10.1/c" and r["exists"]]
|
assert [r for r in result if r["DOI"] == "10.1/c" and r["exists"]]
|
||||||
|
|||||||
Reference in New Issue
Block a user