Migration 21 adds companies.all_roles, tags every favourite of any agent and each company whose board had params {"reach": "all"} (logged per board, the param removed). reach.plan reads a tagged company's boards with the union of the enabled agents' require reach and the base reach; favourite no longer widens the fetch. POST /api/companies/{key}/all-roles sets it and queues a pull when on. The Companies list, the reader and the company strip draw the toggle beside the affinity buttons, the list filters on it, and the board's reach editor drops its All roles option.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
8.4 KiB
Python
200 lines
8.4 KiB
Python
"""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, 21]
|
|
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)
|
|
|
|
|
|
def test_migration_21_tags_the_favourites_and_the_boards_that_read_all_roles(tmp_path: Path) -> None:
|
|
path = tmp_path / "old.db"
|
|
store = Store(path)
|
|
store.add_company("Mistral AI")
|
|
store.add_company("Acme")
|
|
store.add_subscription("ats", "smartrecruiters:mistral", label="Mistral AI",
|
|
company="Mistral AI", board="smartrecruiters",
|
|
params={"reach": "all", "note": "kept"})
|
|
store.add_subscription("ats", "workday:acme", label="Acme", company="Acme",
|
|
board="workday", params={"countries": ["DK"]})
|
|
# A board whose company has no row: its params are cleaned, no flag lands.
|
|
store.add_subscription("ats", "greenhouse:ghost", label="Ghost", company="Ghost",
|
|
board="greenhouse", params={"reach": "all"})
|
|
# A favourite of any agent, enabled or not, is tagged: favourite no
|
|
# longer widens the fetch, the tag does.
|
|
store.add_company("Black Forest Labs")
|
|
store.add_company("Liked")
|
|
off = store.save_agent("Off", enabled=False)
|
|
store.set_affinity(off.id, "black-forest-labs", 2) # type: ignore[arg-type]
|
|
store.set_affinity(off.id, "liked", 1) # type: ignore[arg-type]
|
|
with sqlite3.connect(path) as conn:
|
|
conn.execute("ALTER TABLE companies DROP COLUMN all_roles")
|
|
conn.execute("PRAGMA user_version = 20")
|
|
reopened = Store(path)
|
|
assert reopened.migrations_applied == [21]
|
|
assert {c.key: c.all_roles for c in reopened.companies()} == {
|
|
"mistral-ai": 1, "acme": 0, "black-forest-labs": 1, "liked": 0}
|
|
params = {s.target: s.params for s in reopened.subscriptions(source="ats")}
|
|
assert params == {"smartrecruiters:mistral": {"note": "kept"},
|
|
"workday:acme": {"countries": ["DK"]}, "greenhouse:ghost": {}}
|
|
|
|
|
|
def test_a_merge_keeps_the_all_roles_tag(store: Store) -> None:
|
|
store.add_company("Mistral")
|
|
store.add_company("Mistral AI")
|
|
store.set_all_roles("mistral", True)
|
|
assert store.merge_companies("mistral-ai", "mistral").all_roles == 1
|