Add Platsbanken as a third search provider

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Sonnet 5.5 committed 2026-09-29 16:10:45 +02:00
1 parent b6afe24eaa
commit 00e111961e
4 files changed
+257 -12

No files matched your search

+125 -1
View File
@@ -1,6 +1,7 @@
"""The `search` plugin, `country_of`, and what the pull and the CLI do with
searches. No network: Jobindex's feed is a trimmed copy of a real one
(2026-09-23), Adzuna's is written from its documented response."""
(2026-09-23), Adzuna's is written from its documented response, Platsbanken's
is trimmed from a live answer (2026-09-29)."""
from __future__ import annotations
@@ -230,9 +231,132 @@ def test_an_adzuna_search_stops_at_its_count(monkeypatch: pytest.MonkeyPatch) ->
assert (asked, len(rows), done) == ([1], search.ADZUNA_PAGE, True)
#: Two hits of a live `/search?q=data science&region=CaRE_1nn_cSU` (2026-09-29),
#: trimmed to the fields the parser reads, and a third the way an ad with no
#: municipality and a remote headline comes.
PLATSBANKEN = {
"total": {"value": 32},
"hits": [
{"id": "31457942", "headline": "Postdoctoral Researcher",
"webpage_url": "https://arbetsformedlingen.se/platsbanken/annonser/31457942",
"publication_date": "2026-09-09T15:47:27", "application_deadline": "2026-10-07T23:59:59",
"description": {"text": "The European Spallation Source (ESS) is one of the largest.",
"text_formatted": "<p>The European Spallation Source (ESS)</p>"},
"employer": {"name": "European Spallation Source Eric"},
"application_details": {"url": "https://europeanspallationsource.se/careers"},
"workplace_model": {"concept_id": "ZxwM_gtz_n61", "label": "Arbete på plats"},
"workplace_address": {"municipality": "Lund", "region": "Skåne län",
"region_concept_id": "CaRE_1nn_cSU", "country": "Sverige"}},
{"id": "31435504", "headline": "Postdoctoral Fellow in Robotics and Computer Vision",
"webpage_url": "https://arbetsformedlingen.se/platsbanken/annonser/31435504",
"publication_date": "2026-09-03T15:46:06",
"description": {"text": "Robotics at ESS."},
"employer": {"name": "European Spallation Source Eric"},
"workplace_model": {"concept_id": None, "label": None},
"workplace_address": {"municipality": None, "region": "Skåne län",
"region_concept_id": "CaRE_1nn_cSU", "country": "Sverige"}},
{"id": "31400001", "headline": "Data scientist, distansarbete möjligt",
"webpage_url": "javascript:alert(1)", "workplace_address": {}},
{"id": "31400002", "headline": "Data scientist, distansarbete möjligt",
"webpage_url": "https://arbetsformedlingen.se/platsbanken/annonser/31400002",
"employer": {"name": None}, "workplace_address": {}},
],
}
def test_platsbanken_reads_title_company_place_and_date() -> None:
rows = search.parse_platsbanken(json.dumps(PLATSBANKEN).encode())
assert [r["id"] for r in rows] == ["31457942", "31435504", "31400002"] # javascript: refused
first, no_town, remote = rows
assert first["title"] == "Postdoctoral Researcher"
assert first["company"] == "European Spallation Source Eric"
assert first["place"] == "Lund"
assert first["country"] == "SE"
assert first["url"] == "https://arbetsformedlingen.se/platsbanken/annonser/31457942"
assert first["posted"] is not None and first["posted"].date().isoformat() == "2026-09-09"
assert first["description"].startswith("The European Spallation Source")
assert first["remote"] is None
assert no_town["place"] == "Skåne län" # the region when there is no municipality
assert remote["remote"] == "remote"
assert (remote["company"], remote["place"], remote["posted"]) == ("", "", None)
def _platsbanken(monkeypatch: pytest.MonkeyPatch, total: int | None,
stock: int = 250) -> list[dict[str, Any]]:
"""Platsbanken stubbed to `stock` results, `PLATSBANKEN_PAGE` a request,
stating `total`. Returns the params of each request."""
asked: list[dict[str, Any]] = []
def fake_get(client: object, url: str, params: dict[str, Any]) -> bytes:
asked.append(params)
start = params["offset"]
hits = [{"id": str(n), "headline": f"Role {n}",
"webpage_url": f"https://x.example/{n}"}
for n in range(start, min(start + params["limit"], stock))]
body: dict[str, Any] = {"hits": hits}
if total is not None:
body["total"] = {"value": total}
return json.dumps(body).encode()
monkeypatch.setattr(search, "_get", fake_get)
return asked
@pytest.mark.parametrize(("total", "pages", "found", "complete"), [
(250, 5, 250, True), # a short third page ends it
(300, 5, 250, False), # the stated total is more than there was
(250, 2, 200, False), # stopped by the page limit
(None, 5, 250, True), # no total: a short page is the end
])
def test_a_platsbanken_search_is_complete_when_it_read_its_total(
monkeypatch: pytest.MonkeyPatch, total: int | None, pages: int, found: int,
complete: bool) -> None:
_platsbanken(monkeypatch, total)
rows, done = search.search_platsbanken(None, {"q": "x", "pages": pages}, {}) # type: ignore[arg-type]
assert (len(rows), done) == (found, complete)
def test_a_platsbanken_search_stops_at_its_total_and_sends_region_and_offset(
monkeypatch: pytest.MonkeyPatch) -> None:
asked = _platsbanken(monkeypatch, 200, stock=200)
rows, done = search.search_platsbanken(
None, {"q": "data", "region": "CaRE_1nn_cSU"}, {}) # type: ignore[arg-type]
assert (len(rows), done) == (200, True)
assert [p["offset"] for p in asked] == [0, 100]
assert asked[0]["q"] == "data" and asked[0]["region"] == "CaRE_1nn_cSU"
assert asked[0]["limit"] == search.PLATSBANKEN_PAGE
def test_a_platsbanken_search_without_a_region_sends_none(
monkeypatch: pytest.MonkeyPatch) -> None:
asked = _platsbanken(monkeypatch, 5, stock=5)
search.search_platsbanken(None, {"q": "x"}, {}) # type: ignore[arg-type]
assert "region" not in asked[0]
def test_a_platsbanken_search_never_asks_past_the_offset_limit(
monkeypatch: pytest.MonkeyPatch) -> None:
asked = _platsbanken(monkeypatch, 5000, stock=5000)
monkeypatch.setattr(search, "MAX_PAGES", 30)
_rows, done = search.search_platsbanken(None, {"q": "x", "pages": 30}, {}) # type: ignore[arg-type]
assert max(p["offset"] for p in asked) == search.PLATSBANKEN_MAX_OFFSET
assert not done
def test_a_platsbanken_search_refuses_an_unknown_region() -> None:
with pytest.raises(ValueError, match="region"):
search.search_platsbanken(None, {"q": "x", "region": "nope"}, {}) # type: ignore[arg-type]
def test_platsbanken_regions_are_the_21_lan() -> None:
assert len(search.PLATSBANKEN_REGIONS) == 21
assert search.PLATSBANKEN_REGIONS["CaRE_1nn_cSU"] == "Skåne län"
def test_provider_of() -> None:
assert search.provider_of("jobindex:copenhagen-backend") == "jobindex"
assert search.provider_of("adzuna:berlin") == "adzuna"
assert search.provider_of("platsbanken:skane-data") == "platsbanken"
assert search.provider_of("indeed:x") == ""
assert search.provider_of("jobindex:") == ""