Files
jobboard/tests/test_readers_jobindex.py
T
2026-09-30 13:26:20 +02:00

139 lines
5.8 KiB
Python

"""The Jobindex reader: the RSS twin of a search, 20 results a page, 200 at
most. No network: `get` is a stub."""
from __future__ import annotations
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, jobindex # noqa: E402
BODY = (ROOT / "tests" / "fixtures" / "search" / "jobindex.xml").read_bytes()
def _rows(body: bytes = BODY) -> list[dict[str, Any]]:
decoded, _results, _total, _cursor = jobindex.page(lambda url, **kw: body, "", None, Ask())
return list(jobindex.rows(decoded, ""))
def _feed(items: int) -> bytes:
body = "".join(f"<item><title>Role {n}, Acme</title>"
f"<link>https://www.jobindex.dk/vis-job/r{n}</link></item>"
for n in range(items))
return (f"<rss><channel><title>Jobindex</title>{body}</channel></rss>").encode()
def test_it_is_a_search_reader_with_the_owners_robots_decision() -> None:
assert readers.PROVIDERS["jobindex"] is jobindex
reads = jobindex.READS
assert (reads.scope, reads.page_size, reads.ceiling, reads.text) == (
"search", 20, 200, "excerpt")
assert "owner, 2026-09-30" in reads.robots and "geoareaid" in reads.robots
assert jobindex.allowed("")("www.jobindex.dk") and not jobindex.allowed("")("jobindex.dk")
def test_it_reads_title_company_town_date_and_teaser() -> None:
paid, bare, robot = _rows()
assert [r["source_id"] for r in (paid, bare, robot)] == ["h1701165", "r14011701", "r14011706"]
assert paid["company"] == "DSV"
assert paid["title"].startswith("Dangerous Goods Compliance Manager")
assert paid["places"] == [{"text": "Hedehusene", "remote": None}]
assert paid["location"] == "Hedehusene" and paid["country"] == "DK"
assert paid["posted"].date().isoformat() == "2026-09-23"
assert paid["teaser"].startswith("Do you want to minimise")
assert "description" not in paid
assert bare["company"] == "" and bare["places"][0]["text"] == "København"
assert (robot["title"], robot["company"]) == ("Marketing Content Creator", "Clerk.IO ApS")
def test_it_asks_for_the_query_the_area_and_the_page() -> None:
asked: list[str] = []
def get(url: str, **kwargs: Any) -> bytes:
asked.append(url)
return _feed(3)
jobindex.page(get, "", None, Ask(params={"q": "backend developer", "area": 1}))
parts = urlsplit(asked[0])
assert (parts.scheme, parts.netloc, parts.path) == (
"https", "www.jobindex.dk", "/jobsoegning.rss")
assert parse_qs(parts.query) == {"q": ["backend developer"], "sort": ["date"],
"geoareaid": ["1"], "page": ["1"]}
jobindex.page(get, "", 4, Ask(params={"q": "x"}))
assert "geoareaid" not in asked[1] and "page=4" in asked[1]
@pytest.mark.parametrize(("items", "cursor", "after"), [
(20, None, 2), # a full page has a next
(19, None, None), # a short page is the end
(20, 10, None), # the tenth page reaches the 200 ceiling
(20, 9, 10),
])
def test_it_pages_until_a_short_page_or_the_ceiling(
items: int, cursor: int | None, after: int | None) -> None:
_decoded, results, total, nxt = jobindex.page(
lambda url, **kw: _feed(items), "", cursor, Ask(params={"q": "x"}))
assert (len(results), total, nxt) == (items, None, after)
def test_a_dropped_item_does_not_pass_for_the_end() -> None:
body = _feed(19).replace(b"</channel>", b"<item><title>No link</title></item></channel>")
decoded, results, _total, nxt = jobindex.page(lambda url, **kw: body, "", None, Ask())
assert len(results) == 20 and nxt == 2
assert len(jobindex.rows(decoded, "")) == 19
def test_an_answer_without_a_channel_raises() -> None:
with pytest.raises(ValueError, match="channel"):
jobindex.page(lambda url, **kw: b"<rss/>", "", None, Ask())
def test_saved_params_keep_the_query_and_a_real_area() -> None:
assert jobindex.clean_params({"q": "x", "area": "3"}) == {"q": "x", "area": 3}
assert jobindex.clean_params({"q": "x", "area": 0}) == {"q": "x"}
# ------------------------------------------------- the link out of a vis-job page
PAGES = ROOT / "tests" / "fixtures" / "jobindex"
@pytest.mark.parametrize("name, link", [
("vis-job-employer.html",
"https://careers.kfforsikring.dk/jobs/8456088-loesningsarkitekt-til-koebstaedernes-forsikring"
"?promotion=2220670-jobindex"),
("vis-job-thehub.html",
"https://thehub.io/jobs/6ab5030dfd5518f38b6960b2"
"?utm_source=jobindex&utm_medium=jobboard&utm_content=dk"),
("vis-job-jobannonce.html",
"https://www.jobindex.dk/jobannonce/r14009457/"
"lead-software-engineer-research-tech-delivery-ai-digital-innovation"),
])
def test_posting_link_is_the_se_jobbet_button_of_a_saved_page(name: str, link: str) -> None:
assert jobindex.posting_link((PAGES / name).read_text(encoding="utf-8")) == link
def test_posting_link_skips_other_ads_and_is_empty_without_a_button() -> None:
other = '<a class="btn" href="/c?t=h1&amp;ctx=w&amp;ctt=r&amp;u=2">Se jobbet</a>'
assert jobindex.posting_link(other) == ""
assert jobindex.posting_link("<p>Ingen knap</p>") == ""
assert jobindex.posting_link("") == ""
mine = '<A HREF="https://a.example/x?a=1&amp;b=2" rel="noopener">\n Se jobbet </A>'
assert jobindex.posting_link(other + mine) == "https://a.example/x?a=1&b=2"
def test_posting_link_is_absolute_and_never_a_script() -> None:
assert jobindex.posting_link('<a href="/jobannonce/r1/x">Se jobbet</a>') \
== "https://www.jobindex.dk/jobannonce/r1/x"
assert jobindex.posting_link('<a href="javascript:alert(1)">Se jobbet</a>') == ""
assert jobindex.posting_link('<a class="x">Se jobbet</a>') == ""