diff --git a/plugins/readers/lever.py b/plugins/readers/lever.py index 7ee003b..b161296 100644 --- a/plugins/readers/lever.py +++ b/plugins/readers/lever.py @@ -112,9 +112,13 @@ def _places(categories: dict[str, Any]) -> list[Place]: def parse(body: bytes, slug: str) -> list[Row]: + """The board's postings, a JSON list. Any other answer (an error + document sent with a 200) raises, so the read fails and closes nothing + rather than reading as a board with no roles.""" data = json.loads(body) if not isinstance(data, list): - return [] + raise ValueError(f"Lever's answer is a JSON {type(data).__name__}, " + "not a list of postings") rows = [] for j in data: title = (j.get("text") or "").strip() diff --git a/plugins/readers/oracle.py b/plugins/readers/oracle.py index 4762c82..43d4f5d 100644 --- a/plugins/readers/oracle.py +++ b/plugins/readers/oracle.py @@ -110,20 +110,31 @@ def page(get: Get, slug: str, cursor: Any, ask: Ask = Ask()) -> Page: `items[0]`. Without the `expand` only the facets come back; asking for `requisitionList.secondaryLocations` gives the roles and each one's other locations, which the plain `requisitionList` leaves out. Past the last role - the list is empty.""" + the list is empty; an answer without the wrapper raises (`_search`).""" offset = int(cursor or 0) site = _parts(slug)[1] data = json.loads(get( f"{_api(slug)}/recruitingCEJobRequisitions?onlyData=true" f"&expand=requisitionList.secondaryLocations" f"&finder=findReqs;siteNumber={site},{_selected(ask)}limit={PAGE_SIZE},offset={offset}")) - search = (data.get("items") or [{}])[0] + search = _search(data) roles = search.get("requisitionList") or [] # `_paged` counts `id`, which Oracle spells `Id`. results = [{"id": str(role.get("Id") or "")} for role in roles] return search, results, search.get("TotalJobsCount"), offset + len(roles) if roles else None +def _search(data: Any) -> dict[str, Any]: + """The search wrapper of a listing's answer, `items[0]`. Every answer has + one, past the last role too; one without (an error document sent with a + 200) raises, so the read fails and closes nothing rather than reading as + an empty last page with no total, which is a complete read.""" + items = data.get("items") if isinstance(data, dict) else None + if not isinstance(items, list) or not items or not isinstance(items[0], dict): + raise ValueError("Oracle's answer has no search wrapper in items") + return items[0] + + #: How many countries `locationsFacet` lists at most; a list this long may #: leave some out. FACET_LIMIT = 10 @@ -162,7 +173,7 @@ def facets(get: Get, slug: str) -> dict[str, dict[str, str]]: def parse(body: bytes, slug: str) -> list[Row]: - return rows((json.loads(body).get("items") or [{}])[0], slug) + return rows(_search(json.loads(body)), slug) def _text(value: Any) -> str: diff --git a/tests/test_ats_lever.py b/tests/test_ats_lever.py index 8377197..6629ea9 100644 --- a/tests/test_ats_lever.py +++ b/tests/test_ats_lever.py @@ -1,15 +1,20 @@ """The Lever host module's places, country and employment, against a small -saved board. No network. The pay and the plain row are in `test_ats.py`.""" +saved board, and an answer that is no board. No network. The pay and the +plain row are in `test_ats.py`.""" from __future__ import annotations +import json import sys from pathlib import Path +import pytest + ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) +from plugins import ats # noqa: E402 from plugins.readers import Ask, lever # noqa: E402 BOARD = (ROOT / "tests" / "fixtures" / "ats" / "lever-places.json").read_bytes() @@ -33,6 +38,26 @@ def test_the_commitment_is_the_hosts_word() -> None: assert several["employment"] == "Full-time" and one["employment"] == "Permanent" +def test_an_answer_that_is_not_a_list_raises_and_an_empty_list_is_no_roles() -> None: + with pytest.raises(ValueError, match="JSON dict, not a list"): + lever.parse(b'{"ok": false, "error": "Document not found"}', "acme") + assert lever.parse(b"[]", "acme") == [] + + +def test_an_error_document_with_a_200_fails_the_read_and_stamps_nothing( + monkeypatch: pytest.MonkeyPatch) -> None: + """It used to read as a complete board with no roles, which the settle + closed whole under ten roles, where the 10% guard does not hold.""" + monkeypatch.setattr(ats, "_get", lambda *a, **kw: b'{"ok": false}') + source = ats.ATSSource({"boards": [{"url": "lever:acme", "label": "Acme", "params": {}}], + "run": "r"}) + source.state = {} + assert list(source.fetch()) == [] + assert "ats:ok:lever:acme" not in source.state + assert json.loads(source.state["ats:failing:lever:acme"])["reason"].startswith( + "ValueError: Lever's answer is a JSON dict") + + def test_it_declares_a_whole_listing_of_trusted_dates_and_its_robots_record() -> None: reads = lever.READS assert (reads.listing, reads.text, reads.places) == ("whole", "listing", "many") diff --git a/tests/test_ats_oracle.py b/tests/test_ats_oracle.py index f12d433..011cd3e 100644 --- a/tests/test_ats_oracle.py +++ b/tests/test_ats_oracle.py @@ -165,6 +165,28 @@ def test_an_empty_board_is_complete(monkeypatch: pytest.MonkeyPatch, no_delay: N assert ats._fetch_rows(object(), f"oracle:{SLUG}") == ([], True) # type: ignore[arg-type] +@pytest.mark.parametrize("answer", [ + {}, {"items": []}, {"items": {}}, {"items": [None]}, [], {"title": "Unauthorized"}]) +def test_an_answer_without_the_search_wrapper_fails_the_read( + monkeypatch: pytest.MonkeyPatch, no_delay: None, answer: Any) -> None: + """It used to read as an empty last page with no total: a complete + read of a board with no roles.""" + monkeypatch.setattr(ats, "_get", lambda *a, **k: json.dumps(answer).encode()) + with pytest.raises(ValueError, match="no search wrapper in items"): + ats._fetch_rows(object(), f"oracle:{SLUG}") # type: ignore[arg-type] + + +def test_a_board_answering_without_the_wrapper_is_recorded_failing( + monkeypatch: pytest.MonkeyPatch, no_delay: None) -> None: + monkeypatch.setattr(ats, "_get", lambda *a, **k: b'{"title": "Unauthorized"}') + source = ats.ATSSource({"boards": [{"url": f"oracle:{SLUG}", "label": "Nokia", + "params": {}}], "run": "r"}) + source.state = {} + assert list(source.fetch()) == [] + assert f"ats:ok:oracle:{SLUG}" not in source.state + assert "no search wrapper" in json.loads(source.state[f"ats:failing:oracle:{SLUG}"])["reason"] + + # ----------------------------------------------------------------- detail def _detail_get(body: bytes, asked: list[str]) -> Any: