diff --git a/app/storage/store.py b/app/storage/store.py index 5099ff9..75e1e9f 100644 --- a/app/storage/store.py +++ b/app/storage/store.py @@ -301,10 +301,10 @@ def _gate_sql(gate: Gate, gaz: places.Gazetteer) -> tuple[str, str, list[Any]]: `country` and `place` match over the gazetteer, as the feed's Place filter does (`_places_sql`), and also on a role place that holds one of the gate's places (`wider`): a country gate is met by a role whose - `country` is one of its codes or that has a place in or around one of - them, so `DK` is met by `Copenhagen; London`, and by Øresund or Europe - alone, which are no evidence the role is outside Denmark; a town in - Germany is. Its column is empty only when the role has neither a country + `country` is one of its codes or that has a place in one of them, so + `DK` is met by `Copenhagen; London`, and by Øresund or Europe alone, + which are no evidence the role is outside Denmark; a town in Germany + is, also beside Europe. Its column is empty only when the role has neither a country nor a resolved place. A `require` gate on country reads a remote role whose area is stated through the area alone, as the remote chips do (`Gazetteer.overlap`): "Home Based - Worldwide" and "Germany (Remote)" @@ -370,14 +370,16 @@ def _places_sql(tokens: Sequence[str], gaz: places.Gazetteer, *, table: str = "r every place in Denmark, `region:skane` every town in Skåne, `place:malmo` Malmö by any spelling), or whose `country` is one of their countries (a role whose board gave a country and no place). `codes` are more values of - `country` to accept. `wider` also takes a role place that holds one of - them (`Gazetteer.overlap`): Øresund, Europe or Anywhere meet - `country:DK`, as the fetch gate reads them (`_gate_sql`); the feed's - Place filter leaves it off. The `country` column is not widened, as - Sweden would then meet Malmö for a role in Stockholm. A `place:` token - the gazetteer does not know is - matched on whole words of the role's folded line (`places.match_sql`), - so a town nobody has entered yet is still found. + `country` to accept. `wider` also takes a role placed only at places + that hold one of them (`Gazetteer.overlap`): Øresund, Europe or Anywhere + alone meet `country:DK`, as the fetch gate reads them (`_gate_sql`); the + feed's Place filter leaves it off. A role that also names a place + elsewhere is not taken: `London; Europe` is in London, and `Aarhus; + Denmark` does not meet Copenhagen. The `country` column is not widened, + as Sweden would then meet Malmö for a role in Stockholm. A `place:` + token the gazetteer does not know is matched on whole words of the + role's folded line (`places.match_sql`), so a town nobody has entered + yet is still found. A `remote:` token is a remote role whose area overlaps its places (`Gazetteer.overlap`), `remote:anywhere` one whose area is Anywhere. @@ -385,6 +387,7 @@ def _places_sql(tokens: Sequence[str], gaz: places.Gazetteer, *, table: str = "r `scope`, the viewing agent's `require` gates on country; with none, every remote role.""" ids: set[int] = set() + around: set[int] = set() areas: set[int] = set() found = {c for c in codes if c} clauses: list[str] = [] @@ -406,16 +409,27 @@ def _places_sql(tokens: Sequence[str], gaz: places.Gazetteer, *, table: str = "r params += values continue under = gaz.under(named) - ids |= gaz.overlap(named) if wider else under + ids |= under + if wider: + around |= gaz.overlap(named) found |= gaz.codes(under) + first: list[tuple[str, list[Any]]] = [] if ids: - clauses.insert(0, f"{table}.uid IN (SELECT uid FROM role_places " - "WHERE place_id IN (SELECT value FROM json_each(?)))") - params.insert(0, json.dumps(sorted(ids))) + first.append((f"{table}.uid IN (SELECT uid FROM role_places " + "WHERE place_id IN (SELECT value FROM json_each(?)))", + [json.dumps(sorted(ids))])) if found: - clauses.insert(1 if ids else 0, - f"{table}.country IN (SELECT value FROM json_each(?))") - params.insert(1 if ids else 0, json.dumps(sorted(found))) + first.append((f"{table}.country IN (SELECT value FROM json_each(?))", + [json.dumps(sorted(found))])) + if around - ids: + first.append((f"({table}.uid IN (SELECT uid FROM role_places " + "WHERE place_id IN (SELECT value FROM json_each(?))) " + f"AND {table}.uid NOT IN (SELECT uid FROM role_places " + "WHERE place_id IS NOT NULL " + "AND place_id NOT IN (SELECT value FROM json_each(?))))", + [json.dumps(sorted(around - ids)), json.dumps(sorted(around))])) + clauses[:0] = [sql for sql, _ in first] + params[:0] = [value for _, values in first for value in values] if areas: remote.append(_areas_sql(areas, table)) for sql, values in remote: diff --git a/tests/test_score.py b/tests/test_score.py index 8f69d20..7f00b18 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -417,6 +417,30 @@ def test_a_region_that_holds_the_agents_places_is_no_evidence_against( assert oresund not in {r.uid for r in store.roles(places=["country:DK"])} +def test_a_wider_place_beside_a_place_elsewhere_is_no_match( + store: Store, config: Config, fake: FakeEmbedder) -> None: + """Europe alone is no evidence against Denmark; `London; Europe` is in + London, and `Aarhus; Denmark` is not in Copenhagen.""" + europe = role(store, "1", place="Europe", remote="onsite") + london = role(store, "2", place="London; Europe", remote="onsite") + aarhus = role(store, "3", place="Aarhus; Denmark", remote="onsite") + denmark = role(store, "4", place="Denmark", remote="onsite") + by_country = store.save_agent("Denmark", "", [ + Gate(field="country", values=["DK"], mode="any")]) + by_town = store.save_agent("Copenhagen", "", [ + Gate(field="place", values=["copenhagen"], mode="any")]) + + run(config, store, score.run_score) + + scores = store.scores(by_country.id) + assert scores[europe][1] == "" and scores[aarhus][1] == "" and scores[denmark][1] == "" + assert scores[london] == (0.0, "none of country DK") + scores = store.scores(by_town.id) + assert scores[europe][1] == "" and scores[denmark][1] == "" + assert scores[london] == (0.0, "none of place copenhagen") + assert scores[aarhus] == (0.0, "none of place copenhagen") + + def test_a_country_that_holds_the_agents_town_is_no_match_for_a_role_placed_elsewhere( store: Store, config: Config, fake: FakeEmbedder) -> None: """Sweden holds Malmö, yet a role in Stockholm is evidence against it."""