"""API surface of the profile enrichment (docs/API.md "Profile & facts"): `CompanyCard.profile`, detail `facts`, relationship provenance, people `source`. Uses the shared API fixture family and writes a profile into the alpha company's `source_meta`.""" from __future__ import annotations import pytest import test_api_support as support from companyatlas.db import execute, jsonb, transaction client = support.client fixture_data = support.fixture_data pytestmark = pytest.mark.asyncio(loop_scope="session") V = "/api/v1" PROFILE = { "description": "Ztest Alpha is a synthetic company used by the API tests. " * 4, "description_source": "wikipedia", "description_url": "https://en.wikipedia.org/wiki/Ztest_Alpha", "description_license": "CC BY-SA 4.0", "description_attribution": "Text from Wikipedia (en), CC BY-SA 4.0", "logo_url": "https://commons.wikimedia.org/wiki/Special:FilePath/Ztest.svg", "icon_url": None, "founded_year": 1999, "legal_form": "corporation", "legal_name": "Ztest Alpha Inc.", "employees": 47756, "employees_year": 2013, "revenue": {"value": 305630000000, "currency": "USD", "year": 2023}, "net_income": None, "total_assets": None, "hq": {"city": "Testville", "region": "Test Region", "country": "ZZ", "address": None, "lat": None, "lon": None}, "ticker": "ZTA", "exchange": "Nasdaq", "isin": None, "lei": "7ZW8QJWVPR4P1J1KQY45", "sec_cik": None, "public_company": True, "wikipedia_url": "https://en.wikipedia.org/wiki/Ztest_Alpha", "wikidata_url": "https://www.wikidata.org/wiki/Q95", "official_website": "https://ztest.example/", "phone": None, "products": ["Ztest Search"], "industries": [], "industry_labels": ["software industry"], "socials": {"linkedin": "https://www.linkedin.com/company/ztest"}, "enriched_at": "2026-09-13T00:00:00+00:00", "version": "profile-v1", "sources": [{"field": "description", "source": "wikipedia", "url": "https://en.wikipedia.org/wiki/Ztest_Alpha", "retrieved_at": "2026-09-13T00:00:00+00:00"}, {"field": "employees", "source": "wikidata", "url": "https://www.wikidata.org/wiki/Q95", "retrieved_at": "2026-09-13T00:00:00+00:00"}, {"field": "founded_year", "source": "wikidata", "url": "https://www.wikidata.org/wiki/Q95", "retrieved_at": "2026-09-13T00:00:00+00:00"}, {"field": "hq_city", "source": "wikidata", "url": "https://www.wikidata.org/wiki/Q95", "retrieved_at": "2026-09-13T00:00:00+00:00"}, {"field": "ticker", "source": "wikidata", "url": "https://www.wikidata.org/wiki/Q95", "retrieved_at": "2026-09-13T00:00:00+00:00"}, {"field": "revenue", "source": "wikidata", "url": "https://www.wikidata.org/wiki/Q95", "retrieved_at": "2026-09-13T00:00:00+00:00"}], } async def test_profile_facts_relationships_people(client, fixture_data): # type: ignore[no-untyped-def] alpha, beta = fixture_data["alpha"], fixture_data["beta"] async with transaction() as conn: await execute(conn, "update companies set source_meta = source_meta || cast(:p as jsonb) where id = :id", p=jsonb({"profile": PROFILE}), id=alpha) await execute(conn, """insert into company_relationships (id, from_company_id, to_company_id, kind, valid_from, confidence, source_url, provenance) values (:id, :a, :b, 'SUBSIDIARY_OF', '2015-10-02', 0.85, 'https://www.wikidata.org/wiki/Q95', cast(:prov as jsonb))""", id=f"rel_ztestapi{fixture_data['suffix']}p", a=alpha, b=beta, prov=jsonb({"source": "wikidata", "property": "P749", "qid": "Q20800404"})) await execute(conn, """insert into people (id, company_id, name, name_norm, title, role_category, is_executive, status, source_url) values (:id, :c, 'Ztest Chief', :nn, 'Chief Executive Officer', 'ceo', true, 'listed', 'https://www.wikidata.org/wiki/Q95')""", id=f"person_ztestapi{fixture_data['suffix']}w", c=alpha, nn=f"ztestchief{fixture_data['suffix']}") support.cache.clear() # card in a list items = (await client.get(f"{V}/companies", params={"country": "ZZ", "sort": "name"})).json()["items"] cards = {c["slug"]: c for c in items} assert cards[fixture_data["alpha_slug"]]["profile"]["employees"] == 47756 and cards[fixture_data["alpha_slug"]]["profile"]["description_license"] == "CC BY-SA 4.0" assert "profile" not in cards[fixture_data["beta_slug"]] # never enriched → absent, not null-filled # detail: profile, facts, relationships with provenance body = (await client.get(f"{V}/companies/{fixture_data['alpha_slug']}")).json() assert body["profile"]["version"] == "profile-v1" facts = {f["key"]: f for f in body["facts"]} assert set(facts) == {"founded", "headquarters", "employees", "revenue", "legal_form", "listing", "lei", "website", "wikipedia"} assert facts["employees"] == {"key": "employees", "label": "Employees", "value": "47,756 (2013)", "raw": 47756, "source": "wikidata", "url": "https://www.wikidata.org/wiki/Q95", "retrieved_at": "2026-09-13T00:00:00+00:00"} assert facts["revenue"]["value"] == "USD 305.6 B (2023)" and facts["headquarters"]["value"] == "Testville, Test Region, ZZ" assert facts["listing"]["value"] == "ZTA · Nasdaq" and facts["listing"]["source"] == "wikidata" and facts["legal_form"]["source"] is None rel = {r["kind"]: r for r in body["relationships"]} assert rel["SUBSIDIARY_OF"]["company"]["slug"] == fixture_data["beta_slug"] and rel["SUBSIDIARY_OF"]["provenance"] == {"source": "wikidata", "property": "P749", "qid": "Q20800404"} assert rel["SUBSIDIARY_OF"]["valid_from"] == "2015-10-02" and rel["SUBSIDIARY_OF"]["confidence"] == 0.85 and "first_seen_at" in rel["SUBSIDIARY_OF"] assert rel["PARTNER_OF"]["provenance"] == {} # people carry their source people = (await client.get(f"{V}/companies/{fixture_data['alpha_slug']}/people")).json() by_name = {p["name"]: p for p in people["listed"]} assert by_name["Ztest Chief"]["source"] == "wikidata" and by_name["Ztest Chief"]["title"] == "Chief Executive Officer" assert all(p["source"] in ("wikidata", "page") for p in people["listed"] + people["no_longer_listed"]) and "wikidata" in people["sources"] beta_detail = (await client.get(f"{V}/companies/{fixture_data['beta_slug']}")).json() assert beta_detail["profile"] is None and beta_detail["facts"] == []