Merge branch 'wp-h-companies'
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
06b464790a
8 files changed
+347
-17
No files matched your search
@@ -0,0 +1,160 @@
|
||||
"""Companies as symbols: `company_aliases`, the role that lands on an alias, the
|
||||
merge, and the empty enrichment columns (migration 20)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models import company_fold
|
||||
from app.storage import Store
|
||||
from app.storage.schema import _add_company_aliases
|
||||
from desk.models import Item
|
||||
|
||||
|
||||
def undo_v20(conn: sqlite3.Connection) -> None:
|
||||
conn.execute("DROP TABLE company_aliases")
|
||||
for col in ("sector", "stage", "size_band", "founded", "last_funding",
|
||||
"last_funding_currency", "facts_source", "facts_at"):
|
||||
conn.execute(f"ALTER TABLE companies DROP COLUMN {col}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def store(tmp_path: Path) -> Store:
|
||||
return Store(tmp_path / "roledesk.db")
|
||||
|
||||
|
||||
def posting(source_id: str, company: str) -> Item:
|
||||
item = Item(source_id=source_id, location=f"https://boards.example/{source_id}",
|
||||
title="Backend Engineer", body="",
|
||||
published_at=datetime(2026, 9, 20, tzinfo=UTC), raw={"company": company})
|
||||
item.source = "ats"
|
||||
return item
|
||||
|
||||
|
||||
def company_of(store: Store, source_id: str) -> str:
|
||||
row = store._row("SELECT company_key FROM roles WHERE source_id=?", (source_id,))
|
||||
return str(row["company_key"])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name, fold", [
|
||||
("Ørsted", "orsted"), ("Orsted A/S", "orsted"), ("Æble ApS", "aeble"),
|
||||
("Åland Oy", "aland"), ("Celonis SE", "celonis"), ("Trade Republic Bank GmbH", "trade republic bank"),
|
||||
("", ""),
|
||||
])
|
||||
def test_the_fold_drops_marks_and_legal_forms(name: str, fold: str) -> None:
|
||||
assert company_fold(name) == fold
|
||||
|
||||
|
||||
def test_a_company_is_its_own_alias(store: Store) -> None:
|
||||
store.add_company("Ørsted A/S")
|
||||
assert store.company_aliases() == {"ørsted": ["orsted"]}
|
||||
|
||||
|
||||
def test_a_role_from_a_spelling_lands_on_the_company(store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
store.upsert_role(posting("1", "Orsted"))
|
||||
store.upsert_role(posting("2", "ORSTED A/S"))
|
||||
store.upsert_role(posting("3", "Ørsted"))
|
||||
store.upsert_role(posting("4", "Nobody"))
|
||||
assert [company_of(store, s) for s in "1234"] == ["ørsted"] * 3 + ["nobody"]
|
||||
assert [c.key for c in store.companies()] == ["ørsted"]
|
||||
|
||||
|
||||
def test_an_added_alias_finds_the_company(store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
store.add_alias("ørsted", "Dong Energy")
|
||||
store.upsert_role(posting("1", "Dong Energy A/S"))
|
||||
assert company_of(store, "1") == "ørsted"
|
||||
with pytest.raises(KeyError):
|
||||
store.add_alias("nope", "X")
|
||||
with pytest.raises(ValueError):
|
||||
store.add_alias("ørsted", " ")
|
||||
|
||||
|
||||
def test_renaming_and_removing_keep_the_aliases_true(store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
store.update_company("ørsted", name="Orsted Wind")
|
||||
assert store.company_aliases()["ørsted"] == ["orsted", "orsted wind"]
|
||||
store.remove_company("ørsted")
|
||||
assert store.company_aliases() == {}
|
||||
|
||||
|
||||
def test_duplicates_are_listed_by_fold_and_not_merged(store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
store.add_company("Orsted")
|
||||
store.add_company("Other")
|
||||
assert store.duplicate_companies() == [("orsted", ["orsted", "ørsted"])]
|
||||
assert len(store.companies()) == 3
|
||||
# The company with the name's own key wins when a role names it.
|
||||
store.upsert_role(posting("1", "Orsted"))
|
||||
assert company_of(store, "1") == "orsted"
|
||||
|
||||
|
||||
def test_a_merge_moves_roles_subscriptions_and_ratings(store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
store.add_company("Orsted")
|
||||
store.upsert_role(posting("1", "Orsted"))
|
||||
store.upsert_role(posting("2", "Ørsted"))
|
||||
sub = store.add_subscription("ats", "orsted-board", label="Orsted", company="Orsted",
|
||||
board="greenhouse")
|
||||
a, b = store.save_agent("A"), store.save_agent("B")
|
||||
store.set_affinity(a.id, "orsted", 2)
|
||||
store.set_affinity(a.id, "ørsted", -1) # the kept company's rating wins
|
||||
store.set_affinity(b.id, "orsted", 1)
|
||||
|
||||
kept = store.merge_companies("ørsted", "orsted")
|
||||
|
||||
assert kept.key == "ørsted" and store.company("orsted") is None
|
||||
assert {company_of(store, s) for s in "12"} == {"ørsted"}
|
||||
assert store.subscription(sub.id).company == "Ørsted"
|
||||
assert store.company("ørsted", a.id).affinity == -1
|
||||
assert store.company("ørsted", b.id).affinity == 1
|
||||
assert store.duplicate_companies() == []
|
||||
assert store.company_aliases() == {"ørsted": ["orsted"]}
|
||||
# A later role from the gone spelling lands on the kept company.
|
||||
store.upsert_role(posting("3", "Orsted"))
|
||||
assert company_of(store, "3") == "ørsted"
|
||||
with pytest.raises(ValueError):
|
||||
store.merge_companies("ørsted", "ørsted")
|
||||
with pytest.raises(KeyError):
|
||||
store.merge_companies("ørsted", "gone")
|
||||
|
||||
|
||||
def test_the_enrichment_columns_start_empty_and_can_be_filled(store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
row = store._row("SELECT * FROM companies WHERE key='ørsted'")
|
||||
assert (row["sector"], row["stage"], row["size_band"], row["founded"],
|
||||
row["last_funding"], row["last_funding_currency"], row["facts_source"],
|
||||
row["facts_at"]) == ("", "", "", None, None, "", "", "")
|
||||
store.update_company("ørsted", sector="Energy", founded=2006, last_funding=100,
|
||||
last_funding_currency="DKK", facts_source="thehub",
|
||||
facts_at="2026-09-30")
|
||||
row = store._row("SELECT * FROM companies WHERE key='ørsted'")
|
||||
assert (row["sector"], row["founded"], row["facts_source"]) == ("Energy", 2006, "thehub")
|
||||
|
||||
|
||||
def test_migration_20_seeds_an_old_file_and_leaves_collisions(tmp_path: Path) -> None:
|
||||
path = tmp_path / "old.db"
|
||||
Store(path)
|
||||
with sqlite3.connect(path) as conn:
|
||||
undo_v20(conn)
|
||||
for key, name in (("ørsted", "Ørsted"), ("orsted", "Orsted"), ("x", "X")):
|
||||
conn.execute("INSERT INTO companies (key, name, created_at, updated_at) "
|
||||
"VALUES (?, ?, '', '')", (key, name))
|
||||
conn.execute("PRAGMA user_version = 19")
|
||||
reopened = Store(path)
|
||||
assert reopened.migrations_applied == [20]
|
||||
assert reopened.duplicate_companies() == [("orsted", ["orsted", "ørsted"])]
|
||||
assert len(reopened.companies()) == 3
|
||||
|
||||
|
||||
def test_the_step_runs_twice_without_harm(tmp_path: Path) -> None:
|
||||
path = tmp_path / "x.db"
|
||||
Store(path)
|
||||
with sqlite3.connect(path) as conn:
|
||||
undo_v20(conn)
|
||||
_add_company_aliases(conn)
|
||||
@@ -168,13 +168,17 @@ def test_a_v17_file_gains_dup_of_with_its_first_rebuild(tmp_path: Path) -> None:
|
||||
conn.execute(f"DROP TABLE {table}")
|
||||
for column in ("teaser", "seniority_model", "seniority_stated", "seniority_title"):
|
||||
conn.execute(f"ALTER TABLE roles DROP COLUMN {column}")
|
||||
conn.execute("DROP TABLE company_aliases")
|
||||
for column in ("sector", "stage", "size_band", "founded", "last_funding",
|
||||
"last_funding_currency", "facts_source", "facts_at"):
|
||||
conn.execute(f"ALTER TABLE companies DROP COLUMN {column}")
|
||||
conn.execute("DROP INDEX idx_roles_dup_of")
|
||||
conn.execute("ALTER TABLE roles DROP COLUMN dup_of")
|
||||
conn.execute("PRAGMA user_version = 17")
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [18, 19]
|
||||
assert migrated.migrations_applied == [18, 19, 20]
|
||||
assert dup_of(migrated) == {first.uid: None, second.uid: first.uid}
|
||||
with sqlite3.connect(path) as conn:
|
||||
assert conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'idx_roles_dup_of'"
|
||||
|
||||
+16
-6
@@ -69,7 +69,7 @@ def test_schema_creates_on_an_empty_file(tmp_path: Path) -> None:
|
||||
"application_events", "agents", "scores", "state"} <= tables
|
||||
assert "idx_roles_cluster" in indexes
|
||||
assert store.migrations_applied == []
|
||||
assert LATEST == 19
|
||||
assert LATEST == 20
|
||||
# A second open finds it current and applies nothing.
|
||||
assert Store(path).migrations_applied == []
|
||||
|
||||
@@ -88,9 +88,19 @@ def _columns(path: Path, table: str) -> list[tuple[str, str, int, str]]:
|
||||
return [(r[1], r[2], r[3], r[4]) for r in conn.execute(f"PRAGMA table_info({table})")]
|
||||
|
||||
|
||||
def _undo_v20(conn: sqlite3.Connection) -> None:
|
||||
"""A fresh file put back to the v19 shape: no company aliases, no
|
||||
enrichment columns."""
|
||||
conn.execute("DROP TABLE company_aliases")
|
||||
for column in ("sector", "stage", "size_band", "founded", "last_funding",
|
||||
"last_funding_currency", "facts_source", "facts_at"):
|
||||
conn.execute(f"ALTER TABLE companies DROP COLUMN {column}")
|
||||
|
||||
|
||||
def _undo_v19(conn: sqlite3.Connection) -> None:
|
||||
"""A fresh file put back to the v18 shape: no gazetteer, no role places,
|
||||
no teaser, one level."""
|
||||
_undo_v20(conn)
|
||||
for table in ("role_places", "place_includes", "place_aliases", "places"):
|
||||
conn.execute(f"DROP TABLE {table}")
|
||||
for column in ("teaser", "seniority_model", "seniority_stated", "seniority_title"):
|
||||
@@ -176,7 +186,7 @@ def test_a_v14_file_gains_the_watchlist_columns_and_leaves_companies_unrated(
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [15, 16, 17, 18, 19]
|
||||
assert migrated.migrations_applied == [15, 16, 17, 18, 19, 20]
|
||||
acme = migrated.company("acme")
|
||||
assert acme is not None
|
||||
assert (acme.ats, acme.careers_url) == ("teamtailor", "https://a.example")
|
||||
@@ -381,7 +391,7 @@ def test_v14_makes_each_intent_the_agents_one_prompt_with_its_vector(tmp_path: P
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [14, 15, 16, 17, 18, 19]
|
||||
assert migrated.migrations_applied == [14, 15, 16, 17, 18, 19, 20]
|
||||
assert migrated.agent(backend.id).prompts == [ # type: ignore[union-attr]
|
||||
Prompt(name="Backend", text="python backend")]
|
||||
assert migrated.agent(blank.id).prompts == [] # type: ignore[union-attr]
|
||||
@@ -405,7 +415,7 @@ def test_v16_gives_a_company_no_reason_and_keeps_its_row(tmp_path: Path) -> None
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [16, 17, 18, 19]
|
||||
assert migrated.migrations_applied == [16, 17, 18, 19, 20]
|
||||
acme = migrated.company("acme")
|
||||
assert acme is not None
|
||||
assert (acme.careers_url, acme.notes, acme.hand_reason) == (
|
||||
@@ -434,7 +444,7 @@ def test_v17_folds_every_place_and_levels_the_titles_extract_has_not_read(
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [17, 18, 19]
|
||||
assert migrated.migrations_applied == [17, 18, 19, 20]
|
||||
with sqlite3.connect(path) as conn:
|
||||
rows = {uid: (fold, level) for uid, fold, level in conn.execute(
|
||||
"SELECT uid, place_fold, seniority FROM roles")}
|
||||
@@ -676,7 +686,7 @@ def test_v19_gives_every_role_its_places_and_splits_its_level(tmp_path: Path) ->
|
||||
|
||||
migrated = Store(path)
|
||||
|
||||
assert migrated.migrations_applied == [19]
|
||||
assert migrated.migrations_applied == [19, 20]
|
||||
assert [p.name for p in migrated.role_places(joined.uid)] == ["Copenhagen", "Malmö"]
|
||||
assert [p.name for p in migrated.role_places(detailed.uid)] == ["Aarhus"]
|
||||
assert migrated.role_places(stated.uid) == []
|
||||
|
||||
@@ -1670,3 +1670,13 @@ def test_a_kept_role_lists_its_copies_and_a_copy_names_its_kept_role(
|
||||
"original_url": search.location, "posted_at": kept["copies"][0]["posted_at"]}]
|
||||
copy = client.get(f"/api/roles/{search.uid}").json()
|
||||
assert copy["dup_of"] == board.uid and copy["copies"] == []
|
||||
|
||||
|
||||
def test_suggest_companies_matches_through_an_alias(client: TestClient, store: Store) -> None:
|
||||
store.add_company("Ørsted")
|
||||
store.upsert_role(posting("1", company="Orsted"))
|
||||
store.add_alias("ørsted", "Dong Energy")
|
||||
|
||||
for q in ("orsted", "ørsted", "dong"):
|
||||
res = client.get("/api/suggest/companies", params={"q": q})
|
||||
assert [(c["key"], c["name"]) for c in res.json()] == [("ørsted", "Ørsted")]
|
||||
Reference in new issue
Block a user