Guard every read that is not a sweep, treat a read that asks nothing as incomplete, and mark a filtered read that could not ask remote, so no board loses its roles to a changed answer or a filter it could not send
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e827665dd
commit
36bc639384
4 files changed
+332
-116
No files matched your search
+132
-2
@@ -26,7 +26,7 @@ if str(ROOT) not in sys.path:
|
||||
from plugins import ats, readers # noqa: E402
|
||||
from plugins.readers import _runner # noqa: E402
|
||||
from plugins.readers import lever # noqa: E402
|
||||
from plugins.readers._contract import Ask # noqa: E402
|
||||
from plugins.readers._contract import Ask, Filters, Reads # noqa: E402
|
||||
|
||||
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
|
||||
|
||||
@@ -253,7 +253,9 @@ def _fake_host(*, detail: Callable[..., Any] | None = None) -> types.ModuleType:
|
||||
module.slug_ok = lambda slug: slug.isalnum()
|
||||
module.allowed = lambda slug: lambda h: h == "jobs.fake.example"
|
||||
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}"
|
||||
module.feed_url = lambda slug: f"https://jobs.fake.example/{slug}/list"
|
||||
module.READS = Reads(scope="board", listing="whole", complete="whole", places="one",
|
||||
text="detail" if detail is not None else "listing", robots="test")
|
||||
module.feed_url = lambda slug, ask: f"https://jobs.fake.example/{slug}/list"
|
||||
module.parse = lambda body, slug: [
|
||||
{"source_id": i, "title": f"Role {i}", "location": "", "department": "",
|
||||
"url": f"https://jobs.fake.example/{slug}/{i}", "posted": None,
|
||||
@@ -590,3 +592,131 @@ def test_the_eu_api_is_refused_for_a_us_board_and_the_reverse() -> None:
|
||||
|
||||
with _client(handler) as client, pytest.raises(ats._Refused):
|
||||
ats._get(client, "https://api.eu.lever.co/v0/postings/x", "lever", "eu/x")
|
||||
|
||||
|
||||
# ------------------------------------------- the guard, asks, remote unasked
|
||||
|
||||
def _listing_host(**reads: Any) -> Any:
|
||||
"""A declared whole-listing board `fake` whose feed lists `module.ids`,
|
||||
whatever is asked; `reads` override its declaration."""
|
||||
module: Any = types.ModuleType("fake")
|
||||
module.NAME, module.LABEL, module.PATTERNS = "fake", "Fake", ()
|
||||
module.slug_ok = lambda slug: slug.isalnum()
|
||||
module.allowed = lambda slug: lambda h: h == "jobs.fake.example"
|
||||
module.careers_url = lambda slug: f"https://jobs.fake.example/{slug}"
|
||||
module.READS = Reads(**({"scope": "board", "listing": "whole", "text": "listing",
|
||||
"complete": "whole", "places": "one", "robots": "test"} | reads))
|
||||
module.ids = []
|
||||
module.feed_url = lambda slug, ask: f"https://jobs.fake.example/{slug}/list"
|
||||
module.parse = lambda body, slug: [
|
||||
{"source_id": i, "title": f"Role {i}", "url": f"https://jobs.fake.example/{i}",
|
||||
"places": [{"text": "Berlin", "remote": None}]} for i in json.loads(body)]
|
||||
return module
|
||||
|
||||
|
||||
def _listed(module: Any) -> Callable[..., bytes]:
|
||||
return lambda url, **kwargs: json.dumps(module.ids).encode()
|
||||
|
||||
|
||||
def _read_and_stamp(state: dict[str, str], module: Any,
|
||||
limits: _runner.Limits | None = None) -> _runner.Listing:
|
||||
listing = _runner.read(state, "ats", "fake:acme", module, "acme", _listed(module),
|
||||
limits=limits)
|
||||
_runner.stamp(state, "ats", "fake:acme", "r", listing, listing.ids)
|
||||
return listing
|
||||
|
||||
|
||||
def test_an_unfiltered_whole_read_of_nothing_after_forty_is_suspect() -> None:
|
||||
"""A Greenhouse-style board, trusted, unfiltered: a changed answer that
|
||||
parses to no rows is refused rather than read as a board that closed."""
|
||||
module = _listing_host()
|
||||
state: dict[str, str] = {}
|
||||
module.ids = [str(n) for n in range(40)]
|
||||
_read_and_stamp(state, module)
|
||||
module.ids = []
|
||||
with pytest.raises(_runner.Suspect, match="listed 0 roles, under 10% of the 40"):
|
||||
_runner.read(state, "ats", "fake:acme", module, "acme", _listed(module))
|
||||
|
||||
|
||||
def test_a_whole_read_under_suspect_min_is_not_held() -> None:
|
||||
module = _listing_host()
|
||||
state: dict[str, str] = {}
|
||||
module.ids = [str(n) for n in range(5)]
|
||||
_read_and_stamp(state, module)
|
||||
module.ids = []
|
||||
assert _read_and_stamp(state, module).ids == []
|
||||
assert json.loads(state["ats:count:fake:acme"])["count"] == 0
|
||||
|
||||
|
||||
def test_the_same_suspect_answer_is_accepted_after_stale_days() -> None:
|
||||
module = _listing_host()
|
||||
state: dict[str, str] = {}
|
||||
module.ids = [str(n) for n in range(40)]
|
||||
_read_and_stamp(state, module)
|
||||
module.ids = ["1"]
|
||||
failing = "ats:failing:fake:acme"
|
||||
recent = (datetime.now(UTC) - timedelta(days=20)).isoformat(timespec="seconds")
|
||||
state[failing] = json.dumps({"since": recent, "reason": "Suspect: listed 1 roles"})
|
||||
limits = _runner.Limits(page_delay=0, stale_days=21)
|
||||
with pytest.raises(_runner.Suspect):
|
||||
_runner.read(state, "ats", "fake:acme", module, "acme", _listed(module), limits=limits)
|
||||
|
||||
old = (datetime.now(UTC) - timedelta(days=22)).isoformat(timespec="seconds")
|
||||
state[failing] = json.dumps({"since": old, "reason": "Suspect: listed 1 roles"})
|
||||
# Outside a pull (no `stale_days`) the guard never gives way.
|
||||
with pytest.raises(_runner.Suspect):
|
||||
_runner.read(state, "ats", "fake:acme", module, "acme", _listed(module))
|
||||
assert _read_and_stamp(state, module, limits).ids == ["1"]
|
||||
assert json.loads(state["ats:count:fake:acme"])["count"] == 1
|
||||
|
||||
|
||||
def test_keep_size_resets_the_guards_count_unless_told_not_to() -> None:
|
||||
state = {"ats:count:fake:acme": json.dumps({"asks": "[]", "count": 40})}
|
||||
ats.keep_size(state, "fake:acme", 40, reset=False)
|
||||
assert "ats:count:fake:acme" in state
|
||||
ats.keep_size(state, "fake:acme", 3)
|
||||
assert "ats:count:fake:acme" not in state
|
||||
assert state["ats:size:fake:acme"] == "3"
|
||||
|
||||
|
||||
def test_a_filter_that_maps_to_no_host_id_asks_nothing_and_is_incomplete(
|
||||
monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
module = _listing_host(filters=Filters(country="ids"))
|
||||
module.facets = lambda get, slug: {"country": {}}
|
||||
module.ids = ["1", "2"]
|
||||
state: dict[str, str] = {}
|
||||
listing = _runner.read(state, "ats", "fake:acme", module, "acme", _listed(module),
|
||||
Ask(countries=("DK",)))
|
||||
assert (listing.rows, listing.ids, listing.complete, listing.filtered) == (
|
||||
[], [], False, True)
|
||||
|
||||
# Through the plugin: stamped, but not complete, so the settle closes nothing.
|
||||
monkeypatch.setitem(readers.HOSTS, "fake", module)
|
||||
monkeypatch.setattr(ats, "_get", lambda client, url, host, slug, **kw: b'["1"]')
|
||||
source = ats.ATSSource({"boards": [{"url": "fake:acme", "label": "Acme",
|
||||
"params": {"countries": ["DK"]}}], "run": "r"})
|
||||
assert list(source.fetch()) == []
|
||||
assert (source.state["ats:ok:fake:acme"], source.state["ats:complete:fake:acme"]) == (
|
||||
"r", "")
|
||||
|
||||
|
||||
def test_a_filtered_read_that_cannot_ask_remote_says_so() -> None:
|
||||
reach = {"countries": ["DK"], "remote": True}
|
||||
blind = _listing_host(filters=Filters(country="many"))
|
||||
ask = _runner.ask_for(blind, None, reach)
|
||||
assert (ask.countries, ask.remote) == (("DK",), None)
|
||||
assert _runner.remote_wanted(blind, None, reach)
|
||||
listing = _runner.read_listing(blind, "acme", _listed(blind), ask,
|
||||
remote=_runner.remote_wanted(blind, None, reach))
|
||||
assert listing.filtered and listing.remote_unasked
|
||||
state: dict[str, str] = {}
|
||||
_runner.stamp(state, "ats", "fake:acme", "r", listing, listing.ids)
|
||||
assert state["ats:remote_unasked:fake:acme"] == "1"
|
||||
|
||||
# A host that filters on remote asks it as a series of its own.
|
||||
sees = _listing_host(filters=Filters(country="many", remote="many"))
|
||||
ask = _runner.ask_for(sees, None, reach)
|
||||
listing = _runner.read_listing(sees, "acme", _listed(sees), ask, remote=True)
|
||||
assert listing.filtered and not listing.remote_unasked
|
||||
# A params' remote of False asks nothing and wants nothing.
|
||||
assert not _runner.remote_wanted(blind, {"remote": False}, reach)
|
||||
+7
-17
@@ -103,8 +103,6 @@ def test_ask_for_narrows_the_params_to_the_declared_filters() -> None:
|
||||
both = _whole(filters=Filters(country="many", remote="many"))
|
||||
ask = _runner.ask_for(both, {"countries": ["DE"]}, {"countries": ["DK"], "remote": True})
|
||||
assert (ask.countries, ask.remote) == (("DE",), True)
|
||||
# A module without READS reads the whole board.
|
||||
assert _runner.ask_for(types.ModuleType("old"), {"countries": ["DK"]}) == Ask()
|
||||
|
||||
|
||||
def test_a_searchs_params_go_whole_into_the_ask_with_its_key(
|
||||
@@ -139,11 +137,12 @@ def test_ids_filters_are_mapped_through_the_facets() -> None:
|
||||
ask = Ask(countries=("DK", "XX"))
|
||||
[mapped] = _runner.series(module, ask, {"country": {"DK": "h-1", "SE": "h-2"}})
|
||||
assert mapped.countries == ("h-1",)
|
||||
# None known: the host has nothing in reach, and the read is empty.
|
||||
# None known: nothing is asked, which is no evidence, so the read is
|
||||
# incomplete and closes nothing.
|
||||
assert _runner.series(module, Ask(countries=("XX",)), {"country": {"DK": "h-1"}}) == []
|
||||
listing = _runner.read_listing(module, "acme", Web(), Ask(countries=("XX",)),
|
||||
facets={"country": {}})
|
||||
assert (listing.rows, listing.complete, listing.filtered) == ([], True, True)
|
||||
assert (listing.rows, listing.complete, listing.filtered) == ([], False, True)
|
||||
|
||||
|
||||
def test_facets_are_asked_once_a_day_and_only_for_an_ids_filter() -> None:
|
||||
@@ -189,17 +188,6 @@ def test_a_body_over_the_cap_is_read_again_at_the_fallback_feed() -> None:
|
||||
_runner.read_listing(module, "acme", web)
|
||||
|
||||
|
||||
def test_a_module_without_reads_gets_the_older_signatures() -> None:
|
||||
module: Any = types.ModuleType("old")
|
||||
module.NAME = "old"
|
||||
module.feed_url = lambda slug: f"https://jobs.fake.example/{slug}/list"
|
||||
module.parse = lambda body, slug: [_row(i) for i in json.loads(body)]
|
||||
listing = _runner.read_listing(module, "acme",
|
||||
Web({"https://jobs.fake.example/acme/list": b'["1"]'}),
|
||||
Ask(countries=("DK",)))
|
||||
assert [r["source_id"] for r in listing.rows] == ["1"] and listing.complete
|
||||
|
||||
|
||||
def test_a_paged_board_with_a_total_is_complete_when_it_read_as_many() -> None:
|
||||
pages = [["1", "2"], ["3"]]
|
||||
listing = _runner.read_listing(_paged(pages, total=3), "acme", _pages_web(pages),
|
||||
@@ -333,9 +321,11 @@ def test_a_suspect_read_far_under_the_last_count_fails() -> None:
|
||||
web.answers["https://jobs.fake.example/acme/list"] = b'["1"]'
|
||||
with pytest.raises(_runner.Suspect, match="under 10% of the 20"):
|
||||
_runner.read(state, "ats", "t", module, "acme", web)
|
||||
# A reader whose empty answer is trusted reads it as it is.
|
||||
# A reader whose empty answer is trusted is held too, from a count of
|
||||
# `SUSPECT_MIN`.
|
||||
module.READS = module.READS.model_copy(update={"empty": "trusted"})
|
||||
assert _runner.read(state, "ats", "t", module, "acme", web).ids == ["1"]
|
||||
with pytest.raises(_runner.Suspect):
|
||||
_runner.read(state, "ats", "t", module, "acme", web)
|
||||
|
||||
|
||||
def test_the_guard_compares_only_reads_that_asked_the_same() -> None:
|
||||
|
||||
Reference in new issue
Block a user