113b792a7b
How: `catalog_matching`, `catalog_commit_service` and the imports blueprint import `Catalogo`, `catalogo_desde_toml`, `ruta_de_catalogo`, `CatalogoInvalido`, `LotePlata`, `valor_de_observacion` and `tablas_de` from `webapi.orchestration`; the commit writes through `carga_repository.save_load` and `catalog_repository.sync`. `add_resultado` and its tests come over unchanged from the indicator kit. Implications: the import path no longer needs the indicator kit, which isn't a dependency.
91 lines
3.8 KiB
Python
91 lines
3.8 KiB
Python
"""`catalog_matching.match_or_mint`: fails if it stops reusing an existing
|
|
catalog field for an OCR caption that's clearly the same thing worded
|
|
differently, or if an unmatched caption stops being added to the catalog
|
|
and silently drops its value instead.
|
|
"""
|
|
from datetime import date
|
|
|
|
import pytest
|
|
from webapi.orchestration.reference import catalogs as catalogos
|
|
from webapi.orchestration.reference.catalogs import Catalogo
|
|
from webapi.orchestration.tables import tablas_de
|
|
|
|
from extraction.nlp import DiscoveredField, Evidence
|
|
from webapi.imports.schemas.catalog_matching import match_or_mint
|
|
|
|
EVIDENCE = Evidence("fila.pdf", 1, "")
|
|
|
|
|
|
def _field(caption: str, value: str) -> DiscoveredField:
|
|
return DiscoveredField(caption, value, EVIDENCE)
|
|
|
|
|
|
@pytest.fixture
|
|
def catalogo() -> Catalogo:
|
|
tablas = tablas_de("cacu")
|
|
return Catalogo(
|
|
estudios=[tablas.CatEstudio(estudio_id="citologia", etiqueta="Citología cervical")],
|
|
resultados=[
|
|
tablas.CatResultado(
|
|
resultado_id="citologia.calidad_muestra", estudio_id="citologia",
|
|
etiqueta="Calidad de la muestra", tipo_valor="codigo",
|
|
),
|
|
tablas.CatResultado(
|
|
resultado_id="citologia.celulas_glandulares", estudio_id="citologia",
|
|
etiqueta="Células glandulares", tipo_valor="texto",
|
|
),
|
|
],
|
|
valores=[
|
|
tablas.CatValor(
|
|
resultado_id="citologia.calidad_muestra", valor_id="SATISFACTORIO",
|
|
etiqueta="Satisfactoria", orden=1, estado="vigente",
|
|
),
|
|
],
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def catalogo_toml(tmp_path, monkeypatch):
|
|
"""`add_resultado` writes to disk; keep it off the real `cacu.toml`."""
|
|
monkeypatch.setattr(catalogos, "RAIZ_CATALOGOS", tmp_path)
|
|
(tmp_path / "cacu.toml").write_text("", encoding="utf-8")
|
|
return tmp_path / "cacu.toml"
|
|
|
|
|
|
def test_a_worded_differently_caption_reuses_the_existing_resultado(catalogo, catalogo_toml):
|
|
[matched] = match_or_mint(
|
|
[_field("Calidad de muestra", "SATISFACTORIO")], proyecto="cacu", catalogo=catalogo, estudio_id="citologia"
|
|
)
|
|
assert matched.resultado_id == "citologia.calidad_muestra" and not matched.is_new
|
|
assert matched.observacion["valor_id"] == "SATISFACTORIO"
|
|
assert catalogo_toml.read_text(encoding="utf-8") == "" # nothing minted
|
|
|
|
|
|
def test_an_unmatched_caption_in_a_known_estudio_is_minted_there(catalogo, catalogo_toml):
|
|
[matched] = match_or_mint(
|
|
[_field("Tipo de fijador", "Formol")], proyecto="cacu", catalogo=catalogo, estudio_id="citologia"
|
|
)
|
|
assert matched.is_new and matched.resultado_id.startswith("citologia.")
|
|
assert matched.observacion["valor_texto"] == "Formol"
|
|
|
|
sincronizado = catalogos.catalogo_desde_toml(catalogo_toml, tablas=tablas_de("cacu"))
|
|
assert matched.resultado_id in {r.resultado_id for r in sincronizado.resultados}
|
|
|
|
|
|
def test_an_unmatched_caption_with_no_estudio_falls_back_to_documento_externo(catalogo, catalogo_toml):
|
|
[matched] = match_or_mint([_field("Observación suelta", "algo")], proyecto="cacu", catalogo=catalogo)
|
|
assert matched.resultado_id == "documento_externo." + matched.resultado_id.split(".", 1)[1]
|
|
|
|
sincronizado = catalogos.catalogo_desde_toml(catalogo_toml, tablas=tablas_de("cacu"))
|
|
assert "documento_externo" in {e.estudio_id for e in sincronizado.estudios}
|
|
|
|
|
|
def test_the_inferred_tipo_valor_follows_the_value_shape(catalogo, catalogo_toml):
|
|
fecha, numero, texto = match_or_mint(
|
|
[_field("Fecha de algo", "15/01/2024"), _field("Cuenta de algo", "12.5"), _field("Nota libre", "ninguna")],
|
|
proyecto="cacu", catalogo=catalogo, estudio_id="citologia",
|
|
)
|
|
assert fecha.observacion["valor_fecha"] == date(2024, 1, 15)
|
|
assert numero.observacion["valor_numerico"] == 12.5
|
|
assert texto.observacion["valor_texto"] == "ninguna"
|