py60 lines2.1 KB
| 1 | import pytest |
| 2 | |
| 3 | from leads.feed_guard import (StageError, assert_produced, check_payload, count_rows, |
| 4 | fetch_to_cache) |
| 5 | |
| 6 | |
| 7 | def test_a_failed_pull_keeps_the_previous_cache(tmp_path): |
| 8 | cache = tmp_path / "ASH.jsonl" |
| 9 | cache.write_text('{"parcel":"A-1"}\n') |
| 10 | |
| 11 | def broken_source(): |
| 12 | yield {"parcel": "A-1"} |
| 13 | raise ConnectionError("source went away mid-export") |
| 14 | |
| 15 | with pytest.raises(ConnectionError): |
| 16 | fetch_to_cache("ASH", broken_source(), cache) |
| 17 | assert cache.read_text() == '{"parcel":"A-1"}\n' |
| 18 | assert not cache.with_suffix(".tmp").exists() |
| 19 | |
| 20 | |
| 21 | def test_an_empty_export_does_not_replace_good_data(tmp_path): |
| 22 | cache = tmp_path / "ASH.jsonl" |
| 23 | cache.write_text('{"parcel":"A-1"}\n') |
| 24 | with pytest.raises(StageError, match="0 rows"): |
| 25 | fetch_to_cache("ASH", iter([]), cache) |
| 26 | assert cache.read_text() == '{"parcel":"A-1"}\n' |
| 27 | |
| 28 | |
| 29 | def test_a_complete_pull_replaces_the_cache(tmp_path): |
| 30 | cache = tmp_path / "ASH.jsonl" |
| 31 | cache.write_text('{"parcel":"old"}\n') |
| 32 | assert fetch_to_cache("ASH", iter([{"parcel": "A-1"}, {"parcel": "A-2"}]), cache) == 2 |
| 33 | assert count_rows(cache) == 2 |
| 34 | |
| 35 | |
| 36 | def test_an_error_inside_a_200_response_is_a_failure(): |
| 37 | with pytest.raises(RuntimeError, match="server error"): |
| 38 | check_payload({"error": {"code": 400, "message": "Invalid query"}}) |
| 39 | assert check_payload({"features": []}) == {"features": []} |
| 40 | |
| 41 | |
| 42 | def test_a_header_only_csv_stops_the_stage(tmp_path): |
| 43 | out = tmp_path / "owners.csv" |
| 44 | out.write_text("name,tier\n") |
| 45 | with pytest.raises(StageError) as err: |
| 46 | assert_produced("portfolios", [out], resume="run --from portfolios") |
| 47 | assert "produced 0 rows" in str(err.value) |
| 48 | assert "resume with: run --from portfolios" in str(err.value) |
| 49 | |
| 50 | |
| 51 | def test_a_missing_output_stops_the_stage(tmp_path): |
| 52 | with pytest.raises(StageError, match="not created"): |
| 53 | assert_produced("portfolios", [tmp_path / "owners.csv"]) |
| 54 | |
| 55 | |
| 56 | def test_quoted_newlines_are_one_row(tmp_path): |
| 57 | out = tmp_path / "owners.csv" |
| 58 | out.write_text('name,note\n"KESTREL BAY HOLDINGS LLC","line one\nline two"\n') |
| 59 | assert count_rows(out) == 1 |
| 60 | assert_produced("portfolios", [out]) |