integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
|
||||
from core.ocr import OcrEstatus
|
||||
from models.ocr import CIT_VPH_COLUMNS, OcrExtractionResult, OcrExtractor, run_pending_extractions
|
||||
|
||||
|
||||
|
||||
class StubVphExtractor(OcrExtractor):
|
||||
def extract(self, pdf_blob: bytes) -> OcrExtractionResult:
|
||||
return OcrExtractionResult(
|
||||
values={"resultado": "POSITIVO", "genotipo_16": 1, "genotipo_18": 0},
|
||||
confianza=0.92,
|
||||
estatus=OcrEstatus.REALIZADO,
|
||||
requiere_revision=False,
|
||||
metodo_principal="stub-v1",
|
||||
)
|
||||
|
||||
|
||||
class AlwaysFailsExtractor(OcrExtractor):
|
||||
def extract(self, pdf_blob: bytes) -> OcrExtractionResult:
|
||||
raise RuntimeError("boom")
|
||||
|
||||
|
||||
def _make_cacu_citologia_repo():
|
||||
from core.cacu.models import CitologiaCervical
|
||||
from persistance.cacu import citologia_cervical_repository
|
||||
|
||||
return citologia_cervical_repository, CitologiaCervical
|
||||
|
||||
|
||||
def test_run_pending_extractions_populates_result_and_metadata(cacu_db):
|
||||
repo, CitologiaCervical = _make_cacu_citologia_repo()
|
||||
repo.create(
|
||||
CitologiaCervical(
|
||||
pac_id_hospital="H-1",
|
||||
cit_vph_pdf_blob=b"%PDF-fake",
|
||||
)
|
||||
)
|
||||
|
||||
summary = run_pending_extractions(repo, CIT_VPH_COLUMNS, StubVphExtractor(), domain="cacu", entity="citologiacervical")
|
||||
|
||||
assert summary.processed == 1
|
||||
assert summary.skipped_already_done == 0
|
||||
row = repo.list()[0]
|
||||
assert row.cit_vph_resultado == "POSITIVO"
|
||||
assert row.cit_vph_genotipo_16 == 1
|
||||
assert row.cit_vph_ocr_estatus == OcrEstatus.REALIZADO
|
||||
assert row.cit_vph_ocr_requiere_revision is False
|
||||
assert row.cit_vph_ocr_metodo_principal == "stub-v1"
|
||||
|
||||
|
||||
def test_run_pending_extractions_is_idempotent(cacu_db):
|
||||
repo, CitologiaCervical = _make_cacu_citologia_repo()
|
||||
repo.create(CitologiaCervical(pac_id_hospital="H-1", cit_vph_pdf_blob=b"%PDF-fake"))
|
||||
|
||||
first = run_pending_extractions(repo, CIT_VPH_COLUMNS, StubVphExtractor(), domain="cacu", entity="citologiacervical")
|
||||
second = run_pending_extractions(repo, CIT_VPH_COLUMNS, StubVphExtractor(), domain="cacu", entity="citologiacervical")
|
||||
|
||||
assert first.processed == 1
|
||||
assert second.processed == 0
|
||||
assert second.skipped_already_done == 1
|
||||
|
||||
|
||||
def test_run_pending_extractions_persists_failure_instead_of_crashing(cacu_db):
|
||||
repo, CitologiaCervical = _make_cacu_citologia_repo()
|
||||
repo.create(CitologiaCervical(pac_id_hospital="H-1", cit_vph_pdf_blob=b"%PDF-fake"))
|
||||
|
||||
summary = run_pending_extractions(repo, CIT_VPH_COLUMNS, AlwaysFailsExtractor(), domain="cacu", entity="citologiacervical")
|
||||
|
||||
assert summary.failed == 1
|
||||
row = repo.list()[0]
|
||||
assert row.cit_vph_ocr_estatus == OcrEstatus.FALLIDO
|
||||
assert row.cit_vph_ocr_requiere_revision is True
|
||||
|
||||
|
||||
def test_run_pending_extractions_logs_to_governance(cacu_db, governance_db):
|
||||
from persistance.governance import extraction_run_repository
|
||||
|
||||
repo, CitologiaCervical = _make_cacu_citologia_repo()
|
||||
repo.create(CitologiaCervical(pac_id_hospital="H-1", cit_vph_pdf_blob=b"%PDF-fake"))
|
||||
|
||||
run_pending_extractions(
|
||||
repo,
|
||||
CIT_VPH_COLUMNS,
|
||||
StubVphExtractor(),
|
||||
domain="cacu",
|
||||
entity="citologiacervical",
|
||||
governance_repository=extraction_run_repository,
|
||||
source_description="HIS export 2026-09-01",
|
||||
)
|
||||
|
||||
runs = extraction_run_repository.list()
|
||||
assert len(runs) == 1
|
||||
assert runs[0].domain == "cacu"
|
||||
assert runs[0].source_description == "HIS export 2026-09-01"
|
||||
Reference in New Issue
Block a user