112 lines
4.6 KiB
Python
112 lines
4.6 KiB
Python
"""Workable: every location of a role, the country and the hours it states.
|
|
No network: the fixture has the shape of Hugging Face's board, read on
|
|
2026-09-30."""
|
|
|
|
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 desk.sources import strip_html # noqa: E402
|
|
from plugins.readers import Ask, _runner, workable # noqa: E402
|
|
|
|
FIXTURES = ROOT / "tests" / "fixtures" / "ats"
|
|
|
|
|
|
def _rows() -> list[dict]:
|
|
return workable.parse((FIXTURES / "workable-places.json").read_bytes(), "acme")
|
|
|
|
|
|
def test_the_declaration_says_what_the_list_gives() -> None:
|
|
reads = workable.READS
|
|
assert (reads.listing, reads.page_size, reads.places, reads.text) == \
|
|
("paged", 10, "many", "detail")
|
|
assert reads.fields.posted == "trusted" and reads.fields.employment == "stated"
|
|
assert reads.robots.startswith("api: no robots.txt check needed")
|
|
|
|
|
|
def test_page_takes_the_ask_and_posts_the_same_body() -> None:
|
|
sent: list[dict] = []
|
|
|
|
def get(url: str, *, method: str = "GET", payload: dict | None = None, **_: object) -> bytes:
|
|
sent.append({"url": url, "method": method, "payload": payload})
|
|
return b'{"total": 0, "results": []}'
|
|
|
|
workable.page(get, "acme", None, Ask())
|
|
workable.page(get, "acme", "tok", Ask())
|
|
assert [s["payload"] for s in sent] == [{}, {"token": "tok"}]
|
|
|
|
|
|
def test_every_location_of_a_role_is_a_place_once() -> None:
|
|
first, remote, oslo = _rows()
|
|
assert [p["text"] for p in first["places"]] == ["Berlin, Germany", "Munich, Bavaria, Germany",
|
|
"Paris, France"]
|
|
assert first["location"] == "Berlin, Germany; Munich, Bavaria, Germany; Paris, France"
|
|
assert "country" not in first # prep derives it from the places
|
|
assert [p["remote"] for p in first["places"]] == [False] * 3
|
|
assert remote["places"] == [{"text": "France", "remote": True}]
|
|
# A list-less answer (the older shape) still gives its one place.
|
|
assert oslo["places"] == [{"text": "Oslo, Norway", "remote": False}]
|
|
|
|
|
|
def test_the_stated_hours_are_the_hosts_word() -> None:
|
|
assert [r["employment"] for r in _rows()] == ["full", "part", "volunteer"]
|
|
|
|
|
|
def test_the_shape_of_a_real_list_answer_is_read() -> None:
|
|
data = json.loads((FIXTURES / "workable-places.json").read_bytes())
|
|
assert workable.rows(data, "acme")[0]["url"] == "https://apply.workable.com/acme/j/AAA111/"
|
|
|
|
|
|
def test_a_row_keeps_the_shortcode_the_detail_url_needs() -> None:
|
|
first, remote, oslo = _rows()
|
|
assert [r["source_id"] for r in (first, remote, oslo)] == ["8001", "8002", "8003"]
|
|
assert [r["shortcode"] for r in (first, remote, oslo)] == ["AAA111", "BBB222", "CCC333"]
|
|
|
|
|
|
def test_detail_reads_description_requirements_and_benefits() -> None:
|
|
asked: list[str] = []
|
|
body = (FIXTURES / "workable-detail.json").read_bytes()
|
|
|
|
def get(url: str, **_: object) -> bytes:
|
|
asked.append(url)
|
|
return body
|
|
|
|
row = {"source_id": "6125026", "shortcode": "DB4D7C0EC8"}
|
|
found = workable.detail(get, "huggingface", row)
|
|
assert asked == ["https://apply.workable.com/api/v2/accounts/huggingface/jobs/DB4D7C0EC8"]
|
|
assert set(found) == {"description"} and set(found) <= set(_runner.DETAIL_FIELDS)
|
|
text, data = found["description"], json.loads(body)
|
|
for key in ("description", "requirements", "benefits"):
|
|
assert strip_html(data[key])[:40] in text
|
|
assert text.index(strip_html(data["description"])[:40]) < \
|
|
text.index(strip_html(data["benefits"])[:40])
|
|
assert "<" not in text and " " not in text and len(text) > 1500
|
|
|
|
|
|
def test_detail_joins_what_is_there_and_unescapes() -> None:
|
|
body = json.dumps({"description": "<p>Build & run</p>", "requirements": None,
|
|
"benefits": "<ul><li>Pension</li></ul>"}).encode()
|
|
assert workable.detail(lambda url, **_: body, "acme", {"shortcode": "AAA111"}) == \
|
|
{"description": "Build & run Pension"}
|
|
|
|
|
|
@pytest.mark.parametrize("body", [b"{}", b"[]", b'{"title": "x"}'])
|
|
def test_detail_refuses_an_answer_that_is_not_a_role(body: bytes) -> None:
|
|
with pytest.raises(ValueError, match="not a Workable role"):
|
|
workable.detail(lambda url, **_: body, "acme", {"shortcode": "AAA111"})
|
|
|
|
|
|
@pytest.mark.parametrize("row", [{}, {"shortcode": ""}, {"shortcode": "a/b"}, {"shortcode": ".."}])
|
|
def test_detail_needs_a_plain_shortcode(row: dict) -> None:
|
|
with pytest.raises(ValueError, match="shortcode"):
|
|
workable.detail(lambda url, **_: b"{}", "acme", row)
|