"""`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 # noqa: E402 from app.places import 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) == {"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"}), ("Mountain View, California", "US", {"mountain view"}), ("Ningbo, Zhejiang, China", "CN", {"ningbo"}), ("Germany", "DE", set()), ("Remote", "", set()), ("", "", set()), ]) def test_towns(place, country, expected): # A head `places` cannot parse, "remote berlin", may come along; it matches # nothing, so only the towns that must be there and the empty case count. found = towns(place) assert found >= expected and (expected or not found) def test_a_state_or_province_is_not_a_shared_town(): # Live false merges before `places.towns`: Databricks and Bosch. assert not same_posting(cp("a", country="US", place="Mountain View, California"), cp("b", country="US", place="San Francisco, California")) assert not same_posting(cp("a", country="CN", place="Ningbo, Zhejiang, China"), cp("b", country="CN", place="Hangzhou, Zhejiang, China")) def test_assign_does_not_chain(): # A matches B by Berlin, B matches C by Hamburg, A and C do not match, so A # cannot share C's group. 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": None, "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" def test_a_row_without_a_town_joins_only_one_group(): # "Germany" matches Berlin and Munich; joining both would make them one role. rows = [cp("a", place="Berlin"), cp("b", place="Munich"), cp("c", place="Germany")] assert assign(rows) == {"a": None, "b": None, "c": None} rows = [cp("a", place="Berlin"), cp("b", place="Berlin"), cp("c", place="")] assert assign(rows) == {"a": None, "b": "a", "c": "a"} rows = [cp("a", place=""), cp("b", place="Germany")] assert assign(rows) == {"a": None, "b": "a"} def test_a_row_without_a_place_never_bridges_two_countries(): # Live: LEGO "Sales Associate", three rows with no place, one Belgium, one # Netherlands, `country` empty on all five until the place names one. rows = [cp("a", country="", place=""), cp("b", country="", place=""), cp("c", country="", place=""), cp("d", country="BE", place="Belgium"), cp("e", country="NL", place="Netherlands")] got = assign(rows) assert got["d"] is None and got["e"] is None assert got == {"a": None, "b": "a", "c": "a", "d": None, "e": None} assert not same_posting(cp("a", country="EU", place="Belgium"), cp("b", country="EU", place="Netherlands"))