"""`app.dupes` against postings as boards really wrote them. Pure: no store, no network. The live cases are the 2026-09-29 database's.""" from __future__ import annotations 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)) import pytest # noqa: E402 from app.dupes import Copy, assign, keep_order, same_posting, towns # noqa: E402 def cp(uid: str = "u", company: str = "acme", title: str = "engineer", country: str = "DE", place: str = "", host: str = "greenhouse", desc: bool = True, posted: str | None = None) -> Copy: return Copy(uid, company, title, country, place, host, desc, posted) #: `(company_key, title_key, country, host_a, place_a, host_b, place_b, kept host)`. LIVE = [ ("ericsson", "entry level 6g developer", "SE", "platsbanken", "Lund", "eightfold", "Lund, Skåne, Sweden", "eightfold"), ("gn-group", "machine learning engineer", "DK", "jobindex", "Ballerup", "workday", "Ballerup, Denmark", "workday"), ("kmd", "student worker finance", "DK", "jobindex", "Ballerup, Ballerup, Aalborg, Aalborg", "successfactors", "Ballerup, Denmark", "successfactors"), ("trackunit", "machine learning engineer", "DK", "jobindex", "Aalborg, Copenhagen, Kolding, Aarhus, Hybrid position", "teamtailor", "København, Denmark; Aarhus, Denmark; Kolding, Denmark; Aalborg, Denmark", "teamtailor"), ] @pytest.mark.parametrize("company,title,country,ha,pa,hb,pb,kept", LIVE) def test_live_cross_host_pairs(company, title, country, ha, pa, hb, pb, kept): a = cp("a", company, title, country, pa, ha) b = cp("b", company, title, country, pb, hb) assert same_posting(a, b) and same_posting(b, a) result = assign([a, b]) assert result == ({"a": "b", "b": None} if kept == hb else {"a": None, "b": "a"}) def test_sap_requisitions(): place = "Garching bei München (Munich), Germany" title = "forward deployed chief ai engineering expert" rows = [ cp("g3", "sap", title, "DE", place, "successfactors", False, "2026-09-02"), cp("g1", "sap", title, "DE", place, "successfactors", False, "2026-09-01"), cp("g2", "sap", title, "DE", place, "successfactors", False, "2026-09-01"), cp("ny", "sap", title, "US", "New York, United States", "successfactors", True, "2026-08-01"), ] # The earliest posted_at, then the smallest uid, is kept; New York is alone # although it is the only row with a description. assert assign(rows) == {"g1": None, "g2": "g1", "g3": "g1", "ny": None} def test_houston_state_code(): a = cp("a", "sap", "x", "US", "Houston, TX, United States") b = cp("b", "sap", "x", "US", "Houston, United States") assert same_posting(a, b) def test_copenhagen_district_and_remote_phrase(): a = cp("a", "acme", "x", "DK", "København K og mulighed for hjemmearbejde") b = cp("b", "acme", "x", "DK", "Copenhagen, Denmark") assert towns(a.place, "DK") == {"copenhagen"} assert same_posting(a, b) def test_different_towns_are_not_the_same(): assert not same_posting(cp("a", place="Berlin, Germany"), cp("b", place="Munich, Germany")) assert assign([cp("a", place="Berlin"), cp("b", place="Munich")]) == {"a": None, "b": None} def test_empty_place_matches(): assert same_posting(cp("a", place=""), cp("b", place="Berlin")) assert same_posting(cp("a", place=""), cp("b", place="")) assert same_posting(cp("a", place="Germany"), cp("b", place="Berlin, Germany")) def test_empty_company_never_grouped(): a, b = cp("a", company=""), cp("b", company="") assert not same_posting(a, b) assert assign([a, b]) == {"a": None, "b": None} def test_other_company_or_title_is_not_the_same(): assert not same_posting(cp("a"), cp("b", company="other")) assert not same_posting(cp("a"), cp("b", title="designer")) def test_country_rules(): assert not same_posting(cp("a", country="DE", place="Berlin"), cp("b", country="US", place="Berlin")) assert same_posting(cp("a", country="", place="Berlin"), cp("b", country="DE", place="Berlin, Germany")) assert not same_posting(cp("a", country="", place="Berlin"), cp("b", country="DE", place="Munich")) @pytest.mark.parametrize("place,country,expected", [ ("Lund, Skåne, Sweden", "SE", {"lund"}), ("Houston, TX, United States", "US", {"houston"}), ("New York, United States", "US", {"new york"}), ("Aarhus C", "DK", {"aarhus"}), ("Remote Berlin", "DE", {"berlin"}), ("Aalborg, Copenhagen, Kolding, Aarhus, Hybrid position", "DK", {"aalborg", "copenhagen", "kolding", "aarhus"}), ("Berlin und Munich", "DE", {"berlin", "munich"}), ("Germany", "DE", set()), ("Remote", "", set()), ("", "", set()), ]) def test_towns(place, country, expected): assert towns(place, country) == expected def test_assign_is_transitive(): # A matches B by Berlin, B matches C by Hamburg, A and C alone do not match. a = cp("a", place="Berlin", posted="2026-09-03") b = cp("b", place="Berlin, Hamburg", posted="2026-09-02") c = cp("c", place="Hamburg", posted="2026-09-01") assert not same_posting(a, c) assert assign([a, b, c]) == {"a": "c", "b": "c", "c": None} def test_assign_returns_every_uid(): rows = [cp("a"), cp("b", title="other"), cp("c", company="")] assert assign(rows) == {"a": None, "b": None, "c": None} assert assign([]) == {} def test_assign_separate_groups_in_one_partition(): rows = [cp("a", place="Berlin"), cp("b", place="Berlin"), cp("c", place="Munich"), cp("d", place="Munich")] assert assign(rows) == {"a": None, "b": "a", "c": None, "d": "c"} def test_keep_order_ranks(): board = cp("z", host="workday", desc=False, posted="2026-09-09") search = cp("a", host="jobindex", desc=True, posted="2026-01-01") assert min([search, board], key=keep_order) is board with_desc = cp("z", desc=True, posted="2026-09-09") without = cp("a", desc=False, posted="2026-01-01") assert min([without, with_desc], key=keep_order) is with_desc early = cp("z", posted="2026-01-01") late = cp("a", posted="2026-02-01") undated = cp("0", posted=None) assert sorted([undated, late, early], key=keep_order) == [early, late, undated] assert min([cp("b", posted="2026-01-01"), cp("a", posted="2026-01-01")], key=keep_order).uid == "a"