"""The helpers the host modules share, and the one employment vocabulary.""" from __future__ import annotations import importlib import logging import sys from pathlib import Path 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 _shared # noqa: E402 from plugins.readers._shared import ( # noqa: E402 EMPLOYMENTS, add_place, country_if_single, place, remote_word, table_word, ) #: The module-level names a reader keeps its employment words in. _TABLES = ("_EMPLOYMENT", "_CONTRACT", "_SCHEDULE") #: The hosts whose word tables are prep's, in `app/prep/hosts/.py`. _PREP_HOSTS = ("oracle", "personio", "phenom", "pinpoint", "recruitee", "rippling", "smartrecruiters", "teamtailor", "workable", "workday") def _prep_table(name: str, table_name: str): """A word table of `app.prep.hosts.`, or None where it has none.""" module = importlib.import_module(f"app.prep.hosts.{name}") return getattr(module, table_name, None) def test_every_employment_table_maps_into_the_shared_vocabulary() -> None: found = 0 for name, module in readers.READERS.items(): for table_name in _TABLES: table = getattr(module, table_name, None) if table is None: continue found += 1 values = table.values() if isinstance(table, dict) else [v for _k, v in table] # "" is a known word that names no employment ("permanent"). assert set(values) <= {*EMPLOYMENTS, ""}, (name, table_name) for name in _PREP_HOSTS: for table_name in ("EMPLOYMENT", "MATCH"): table = _prep_table(name, table_name) if table is None: continue values = table.values() if isinstance(table, dict) else [v for _k, v in table] found += 1 assert set(values) <= {*EMPLOYMENTS, ""}, (name, table_name) assert found >= 15 def test_the_prep_tables_of_the_hosts_speak_the_apps_vocabulary() -> None: from app.models import SENIORITIES for name in _PREP_HOSTS: seniority = _prep_table(name, "SENIORITY") if seniority is not None: assert set(seniority.values()) <= {*SENIORITIES, ""}, name remote = _prep_table(name, "REMOTE") if remote is not None: assert set(remote.values()) <= {"remote", "hybrid", "onsite", ""}, name for table in (seniority, remote, _prep_table(name, "EMPLOYMENT")): assert table is None or all(k == k.lower() for k in table), name def test_the_vocabulary_is_the_documented_one() -> None: assert EMPLOYMENTS == ("full_time", "part_time", "contract", "temporary", "internship", "freelance") def test_place_is_remote_on_the_word_and_otherwise_as_stated() -> None: assert place("Remote (DE)") == {"text": "Remote (DE)", "remote": True} assert place("Berlin, remote") == {"text": "Berlin, remote", "remote": True} assert place("Berlin") == {"text": "Berlin", "remote": None} assert place("Berlin", False) == {"text": "Berlin", "remote": False} assert place("Remote", False)["remote"] is True assert place("Remotely", None)["remote"] is None def test_add_place_skips_empty_and_repeated_texts_and_keeps_order() -> None: found: list = [] for text in ("Berlin", "", "Oslo", "Berlin"): add_place(found, place(text)) assert [p["text"] for p in found] == ["Berlin", "Oslo"] def test_table_word_maps_ignores_case_and_logs_only_unknown_words(caplog) -> None: table = {"full-time": "full_time", "permanent": ""} log = logging.getLogger("plugins.readers.acme") with caplog.at_level(logging.DEBUG, logger=log.name): assert table_word(table, " Full-Time ", log, "type") == "full_time" assert table_word(table, "Permanent", log, "type") is None assert table_word(table, None, log, "type") is None assert not caplog.records assert table_word(table, "Odd", log, "type") is None assert [r.getMessage() for r in caplog.records] == ["acme: unknown type 'Odd'"] def test_country_if_single_needs_exactly_one_code() -> None: assert country_if_single({"DE"}) == "DE" assert country_if_single({"DE", "SE"}) is None assert country_if_single(set()) is None def test_remote_word_reads_danish_swedish_and_english() -> None: assert remote_word("Remote, Danmark") == "remote" assert remote_word("Birkerød, Hybrid position") == "hybrid" assert remote_word("Hedehusene") is None assert _shared.remote_word is remote_word