"""A rectangle across a municipal boundary must not return the same object twice. Partitioned selection de-duplicated ``total_feature_count`` on ``source_feature_id`` but returned the raw rows. A feature present in two municipal partitions was therefore drawn twice on the map and counted once in the headline, so the number on the panel disagreed with the geometry beside it. """ from __future__ import annotations from uuid import uuid4 from app.services.vector_feature_service import VectorFeatureService class _Row: def __init__(self, source_feature_id, row_id=None, dataset_id=None): self.source_feature_id = source_feature_id self.id = row_id or uuid4() self.dataset_id = dataset_id or uuid4() def _ids(rows): return [row.source_feature_id or str(row.id) for row in rows] def test_a_feature_in_two_partitions_is_returned_once() -> None: shared = "grb-building-42" rows = [_Row(shared), _Row("grb-building-7"), _Row(shared)] kept = VectorFeatureService.deduplicate_rows(rows) assert _ids(kept) == [shared, "grb-building-7"] def test_the_first_occurrence_wins_so_the_result_is_stable() -> None: first = _Row("dup") second = _Row("dup") assert VectorFeatureService.deduplicate_rows([first, second])[0] is first assert VectorFeatureService.deduplicate_rows([second, first])[0] is second def test_rows_without_a_source_id_fall_back_to_their_own_identity() -> None: """Two distinct rows with no source id are two distinct features.""" rows = [_Row(None), _Row(None)] assert len(VectorFeatureService.deduplicate_rows(rows)) == 2 def test_an_empty_source_id_is_not_treated_as_a_shared_identity() -> None: rows = [_Row(""), _Row("")] assert len(VectorFeatureService.deduplicate_rows(rows)) == 2 def test_deduplication_leaves_a_clean_population_untouched() -> None: rows = [_Row("a"), _Row("b"), _Row("c")] assert VectorFeatureService.deduplicate_rows(rows) == rows