"""The Adzuna reader, built against Adzuna's documented response (no key to run it live). No network: `get` is a stub.""" from __future__ import annotations import json import sys from pathlib import Path from typing import Any from urllib.parse import parse_qs, urlsplit import pytest ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from plugins import readers # noqa: E402 from plugins.readers import Ask, adzuna # noqa: E402 BODY = (ROOT / "tests" / "fixtures" / "search" / "adzuna.json").read_bytes() ASK = Ask(params={"q": "backend", "country": "de", "where": "Berlin", "key": {"app_id": "id1", "app_key": "key1"}}) def _rows(ask: Ask = ASK) -> list[dict[str, Any]]: decoded, _results, _total, _cursor = adzuna.page(lambda url, **kw: BODY, "", None, ask) return list(adzuna.rows(decoded, "")) def test_it_declares_the_key_and_states_the_robots_finding() -> None: assert readers.PROVIDERS["adzuna"] is adzuna reads = adzuna.READS assert reads.key is not None and reads.key.name == "adzuna" assert (reads.key.fields, reads.key.env) == ( ("app_id", "app_key"), ("ADZUNA_APP_ID", "ADZUNA_APP_KEY")) assert (reads.text, reads.complete, reads.page_size) == ("excerpt", "stated", 50) assert "disallows /" in reads.robots and "2026-09-30" in reads.robots def test_it_strips_highlights_keeps_the_excerpt_as_teaser_and_drops_predicted_pay() -> None: first, predicted = _rows() # javascript: refused assert [r["source_id"] for r in (first, predicted)] == ["4412345678", "4412345679"] assert first["title"] == "Senior Backend Engineer (m/w/d) - Remote" assert first["teaser"] == "Build our backend services in Python & Go." assert "description" not in first assert first["company"] == "Acme GmbH" assert first["places"] == [{"text": "Berlin, Deutschland", "remote": None}] assert first["country"] == "DE" and first["remote"] == "remote" assert (first["salary_min"], first["salary_max"], first["currency"]) == (70000, 90000, "EUR") assert (predicted["salary_min"], predicted["currency"]) == (None, None) def test_it_sends_the_key_the_country_and_the_query() -> None: asked: list[str] = [] def get(url: str, **kwargs: Any) -> bytes: asked.append(url) return BODY adzuna.page(get, "", None, ASK) parts = urlsplit(asked[0]) assert (parts.netloc, parts.path) == ("api.adzuna.com", "/v1/api/jobs/de/search/1") assert parse_qs(parts.query) == { "app_id": ["id1"], "app_key": ["key1"], "results_per_page": ["50"], "sort_by": ["date"], "what": ["backend"], "where": ["Berlin"]} def test_it_refuses_an_unknown_country() -> None: with pytest.raises(ValueError, match="country"): adzuna.page(lambda url, **kw: BODY, "", None, Ask(params={"q": "x", "country": "dk"})) @pytest.mark.parametrize(("results", "count", "after"), [(50, 120, 2), (49, 49, None)]) def test_it_pages_on_a_full_page_and_states_the_count( results: int, count: int, after: int | None) -> None: body = json.dumps({"count": count, "results": [ {"id": str(n), "title": "T", "redirect_url": f"https://x.example/{n}"} for n in range(results)]}).encode() _decoded, found, total, nxt = adzuna.page(lambda url, **kw: body, "", None, ASK) assert (len(found), total, nxt) == (results, count, after) def test_saved_params_need_a_known_country() -> None: assert adzuna.clean_params({"q": "x", "country": " DE ", "where": " Berlin "}) == { "q": "x", "country": "de", "where": "Berlin"} assert adzuna.clean_params({"q": "x", "country": "gb", "where": ""}) == { "q": "x", "country": "gb"} with pytest.raises(ValueError, match="country"): adzuna.clean_params({"q": "x", "country": "dk"})