126 lines
5.4 KiB
Python
126 lines
5.4 KiB
Python
"""What this module covers: the cohort-build reads of `CohortRepository`: `latest_build`, `members`,
|
|
`build` and `builds`.
|
|
|
|
Fixtures: `repository`, an in-memory SQLite database with the CaCu tables and three builds; `_build`
|
|
writes one `cohorte_armado` row.
|
|
"""
|
|
|
|
import json
|
|
from datetime import date, datetime, timezone
|
|
|
|
import pytest
|
|
from core.cacu.base import CaCuModel
|
|
from core.cacu.indicadores.models import (
|
|
CohorteArmado,
|
|
CohorteExclusion,
|
|
CohorteMiembro,
|
|
CohorteVariable,
|
|
EvaluacionRiesgo,
|
|
EvaluacionRiesgoLote,
|
|
)
|
|
from core.indicadores.cohortes import TablasCohorte
|
|
from sqlmodel import Session, create_engine
|
|
|
|
from persistance.edges.gold import CohortBuild, CohortRepository, CohortMember
|
|
|
|
TABLES = TablasCohorte(CohorteArmado, CohorteMiembro, CohorteExclusion, CohorteVariable, EvaluacionRiesgoLote,
|
|
EvaluacionRiesgo)
|
|
|
|
|
|
def _build(armado_id, url, fingerprint, known_until=None):
|
|
"""A `cohorte_armado` row with the parameters the cohort builder writes; `known_until` is its
|
|
`conocida_hasta`.
|
|
"""
|
|
parameters = {"reclutamiento_inicio": "2025-01-01", "reclutamiento_fin": "2026-01-01", "corte": "2025-12-31",
|
|
"conocida_hasta": known_until}
|
|
return CohorteArmado(armado_id=armado_id, url=url, version="0.2.0", definicion_sha="d", reglas_sha="r",
|
|
parametros=json.dumps(parameters), datos_as_of=date(2025, 12, 31), huella=fingerprint, marco_sha="m" + fingerprint,
|
|
n_miembros=2, armado_en=datetime(2026, 1, armado_id, tzinfo=timezone.utc))
|
|
|
|
|
|
@pytest.fixture
|
|
def repository():
|
|
"""Builds 1 and 3 of `cacu/cohorte_vigilancia` and build 2 of `cacu/otra`. Build 3 has patients 20 and
|
|
10 (inserted in that order) and `conocida_hasta` 2026-01-02 08:00 UTC; build 1 has patient 99 and no
|
|
load cut-off.
|
|
"""
|
|
engine = create_engine("sqlite:///:memory:")
|
|
CaCuModel.metadata.create_all(engine)
|
|
with Session(engine) as session:
|
|
session.add_all([
|
|
_build(1, "cacu/cohorte_vigilancia", "a"),
|
|
_build(2, "cacu/otra", "b"),
|
|
_build(3, "cacu/cohorte_vigilancia", "c", known_until="2026-01-02T08:00:00+00:00"),
|
|
CohorteMiembro(armado_id=3, pac_id=20, fecha_indice=date(2025, 3, 1), indice_fuente_tabla="citologiacervical",
|
|
indice_fuente_id="20"),
|
|
CohorteMiembro(armado_id=3, pac_id=10, fecha_indice=date(2025, 2, 1), indice_fuente_tabla="citologiacervical",
|
|
indice_fuente_id="10"),
|
|
CohorteMiembro(armado_id=1, pac_id=99, fecha_indice=date(2024, 1, 1), indice_fuente_tabla="citologiacervical",
|
|
indice_fuente_id="99"),
|
|
])
|
|
session.commit()
|
|
return CohortRepository(lambda: Session(engine), TABLES)
|
|
|
|
|
|
def test_latest_build_of_a_cohort_among_several(repository):
|
|
"""Pins down: `latest_build` returns the highest build id of the given cohort, ignoring other cohorts.
|
|
|
|
Fails if it stops filtering by `url` or picks the build by something other than its id.
|
|
"""
|
|
assert repository.latest_build("cacu/cohorte_vigilancia") == 3
|
|
assert repository.latest_build("cacu/otra") == 2
|
|
|
|
|
|
def test_members_of_a_build_in_order(repository):
|
|
"""Pins down: `members` returns only that build's members, as `CohortMember`, ordered by `pac_id`.
|
|
|
|
Fails if the order or the fields of `CohortMember` change, or members of other builds leak in.
|
|
"""
|
|
assert repository.members(3) == [
|
|
CohortMember(10, date(2025, 2, 1), "citologiacervical", "10"),
|
|
CohortMember(20, date(2025, 3, 1), "citologiacervical", "20"),
|
|
]
|
|
|
|
|
|
def test_lineage_fields_of_a_build(repository):
|
|
"""Pins down: `build` returns the lineage fields, with `conocida_hasta` parsed from the build's
|
|
parameters.
|
|
|
|
Fails if a field of `CohortBuild` is dropped or reordered, or `conocida_hasta` stops being read from
|
|
`parametros`.
|
|
"""
|
|
assert repository.build(3) == CohortBuild(3, "cacu/cohorte_vigilancia", "0.2.0", "c", "mc", date(2025, 12, 31),
|
|
datetime(2026, 1, 2, 8, tzinfo=timezone.utc), 2,
|
|
datetime(2026, 1, 3, tzinfo=timezone.utc))
|
|
|
|
|
|
def test_a_build_made_without_a_load_cut_has_no_known_until(repository):
|
|
"""Pins down: a build whose `conocida_hasta` is null reports `None`.
|
|
|
|
Fails if a null cut-off is turned into a date.
|
|
"""
|
|
assert repository.build(1).conocida_hasta is None
|
|
|
|
|
|
def test_every_build_newest_first_or_those_of_one_cohort(repository):
|
|
"""Pins down: `builds` lists every build newest first, as `CohortBuild` with its lineage fields
|
|
(`conocida_hasta` read from the parameters), and only one cohort's with `url`; an unknown cohort gives an
|
|
empty list.
|
|
|
|
Fails if the order changes, the `url` filter is dropped, or a build's fields differ from `build`'s.
|
|
"""
|
|
assert [b.armado_id for b in repository.builds()] == [3, 2, 1]
|
|
assert [b.armado_id for b in repository.builds("cacu/cohorte_vigilancia")] == [3, 1]
|
|
assert repository.builds("cacu/cohorte_vigilancia")[0] == repository.build(3)
|
|
assert repository.builds("cacu/no_existe") == []
|
|
|
|
|
|
def test_unknown_cohort_or_build(repository):
|
|
"""Pins down: an unknown cohort or build gives `None` or an empty list, not an error.
|
|
|
|
Fails if any of the three reads raises on missing data.
|
|
"""
|
|
assert repository.latest_build("cacu/no_existe") is None
|
|
assert repository.build(42) is None
|
|
assert repository.members(42) == []
|