116 lines
4.2 KiB
Python
116 lines
4.2 KiB
Python
|
|
|
|
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"
|
|
|
|
|
|
def test_done_rows_do_not_use_up_the_batch(cacu_db):
|
|
repo, CitologiaCervical = _make_cacu_citologia_repo()
|
|
for n in range(3):
|
|
repo.create(CitologiaCervical(pac_id_hospital=f"H-{n}", cit_vph_pdf_blob=b"%PDF-fake",
|
|
cit_vph_ocr_estatus=OcrEstatus.REALIZADO.value))
|
|
repo.create(CitologiaCervical(pac_id_hospital="H-nuevo", cit_vph_pdf_blob=b"%PDF-fake"))
|
|
repo.create(CitologiaCervical(pac_id_hospital="H-sin-pdf"))
|
|
|
|
summary = run_pending_extractions(repo, CIT_VPH_COLUMNS, StubVphExtractor(), domain="cacu",
|
|
entity="citologiacervical", limit=1)
|
|
|
|
assert (summary.processed, summary.skipped_already_done) == (1, 3)
|
|
assert repo.list(pac_id_hospital="H-nuevo")[0].cit_vph_resultado == "POSITIVO"
|
|
assert repo.list(pac_id_hospital="H-sin-pdf")[0].cit_vph_ocr_estatus is None
|