86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
"""The helpers the host modules share, and the one employment vocabulary."""
|
|
|
|
from __future__ import annotations
|
|
|
|
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")
|
|
|
|
|
|
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)
|
|
assert found >= 15
|
|
|
|
|
|
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
|