diff --git a/tests/test_catalog_commit_service.py b/tests/test_catalog_commit_service.py index f664d68..06913a0 100644 --- a/tests/test_catalog_commit_service.py +++ b/tests/test_catalog_commit_service.py @@ -95,3 +95,26 @@ def test_a_patient_outside_persona_is_skipped_not_dropped_silently(cacu_catalog, def test_a_record_without_discovered_fields_is_ignored(cacu_catalog, known_patient): result = commit_discovered_fields([_record(known_patient, [])], proyecto="cacu", estudio_id="citologia") assert result.records_written == 0 + + +def test_a_repeated_caption_is_folded_not_written_twice(cacu_catalog, known_patient): + """A static label printed on several pages of the same document (a date, + a signature line) discovers as one `DiscoveredField` per occurrence, + on purpose (`discover_captions`'s docstring). Fails if this stops + folding repeats before writing: `observacion` has one row per + `(procedimiento, resultado_id)`, so a second occurrence of the same + resultado_id raises `IntegrityError` and the whole record's fields — + including the ones that weren't duplicated — are lost.""" + result = commit_discovered_fields( + [_record(known_patient, [("Calidad de muestra", "Insatisfactoria"), ("Calidad de muestra", "Satisfactoria")])], + proyecto="cacu", + estudio_id="citologia", + ) + assert (result.records_written, result.fields_matched, result.fields_minted) == (1, 1, 0) + + from persistance.cacu import carga_repository + + with carga_repository.open_session() as sesion: + [observacion] = sesion.exec(select(carga_repository.observacion)).all() + assert observacion.resultado_id == "citologia.calidad_muestra" + assert observacion.valor_texto == "Insatisfactoria" diff --git a/webapi/imports/services/catalog_commit_service.py b/webapi/imports/services/catalog_commit_service.py index af7f983..acff5ac 100644 --- a/webapi/imports/services/catalog_commit_service.py +++ b/webapi/imports/services/catalog_commit_service.py @@ -27,7 +27,7 @@ from indicadores_kit.plata.procedimiento import LotePlata from indicadores_kit.registry.catalogos import Catalogo, catalogo_desde_toml, ruta_de_catalogo from indicadores_kit.tablas import tablas_de -from webapi.imports.schemas.catalog_matching import match_or_mint +from webapi.imports.schemas.catalog_matching import MatchedField, match_or_mint FUENTE_CATALOGO_ABIERTO = "catalogo_abierto" @@ -66,6 +66,7 @@ def commit_discovered_fields( matched = match_or_mint(record.discovered, proyecto=proyecto, catalogo=catalogo, estudio_id=estudio_id) if any(field.is_new for field in matched): catalogo = catalogo_desde_toml(ruta, tablas=tablas) # so a later record in this batch can match it too + matched = _fold_duplicates(matched) by_estudio: dict[str, list] = defaultdict(list) for field in matched: @@ -89,6 +90,22 @@ def commit_discovered_fields( return result +def _fold_duplicates(matched: list[MatchedField]) -> list[MatchedField]: + """A record's discovered captions can repeat the same resultado_id (a + static label such as "FECHA" printed on several pages): `observacion` + has one row per `(procedimiento, resultado_id)`, so only the first + occurrence survives. `discover_captions` keeps every repeat on purpose + and leaves folding to the caller (see its docstring).""" + seen: set[str] = set() + folded = [] + for field in matched: + if field.resultado_id in seen: + continue + seen.add(field.resultado_id) + folded.append(field) + return folded + + def _sync_catalog(dominio, proyecto: str, tablas) -> None: """A field minted by `match_or_mint` only exists in the TOML until this runs: `procedimiento.estudio_id` and `observacion`'s catalog both need