"""What this module covers: the ADOS-2 association against the TEA database (`models.ados2_relation`): the cohort it builds from bronze (first valid EEG and first ADOS-2 per child), the rows it writes to `relacion_ados2` and `relacion_ados2_paciente`, and versions (a new cohort is a later version, the same cohort writes nothing). Fixtures: `tea_db` (an empty SQLite TEA database); `stored`, the invented cohort of `ados2_cohort` without item scores (60 children, spindles in central lowering the CSS) written to bronze: each child with an ADOS-2 and a valid analysed EEG `e-NNN` whose metric z and zone z are the cohort's, plus a later valid EEG `e-000-b` of child 1, an invalid EEG of child 2 and a child 99 with an EEG and no ADOS-2. """ from datetime import date, datetime import pytest from ados2_cohort import ITEM_CODES, cohort from sqlmodel import Session, select from core.tea.models import Ados2, Eeg, EegMetrica, EegMetricaZona, Pacientes, RelacionAdos2, RelacionAdos2Paciente from models import ados2_relation as rel from tea.contracts import ZONES CODES = {"spikes": "PICOS_PAROXISTICOS", "spindles": "HUSOS_SUENO_N2", "posterior_rhythm": "RITMO_POSTERIOR", "gradient": "GRADIENTE_ANTEROPOSTERIOR"} def _eeg(session, eeg_id, pac_id, row, *, studied=date(2025, 6, 1), valid=1): session.add(Eeg(eeg_id=eeg_id, eeg_pac_id=pac_id, eeg_fecha_estudio=studied, eeg_valido=valid, eeg_edad_meses=float(row.age_months))) for biomarker, code in CODES.items(): met_id = f"{eeg_id}/{code}" zoned = biomarker in rel.ZONED session.add(EegMetrica(met_id=met_id, met_eeg_id=eeg_id, met_codigo=code, met_z=None if zoned else float(row[f"z_{biomarker}"]))) if zoned: session.add_all([EegMetricaZona(mez_id=f"{met_id}/{z}", mez_met_id=met_id, mez_zona=z, mez_z=float(row[f"z_{biomarker}_{z}"])) for z in ZONES]) def _engine(): from persistance.db import get_tea_engine return get_tea_engine() @pytest.fixture def stored(tea_db): """The CSS-only invented cohort in bronze, with the extra EEGs and child the module docstring lists.""" t = cohort().drop(columns=ITEM_CODES) with Session(_engine()) as s: for i, row in t.iterrows(): pac_id = i + 1 s.add(Pacientes(pac_id=pac_id, pac_id_hospital=f"H-{pac_id}")) s.add(Ados2(ado_id=f"a-{pac_id}", ado_pac_id=pac_id, ado_fecha=date(2025, 5, 1), ado_modulo=row.ados_module, ado_css=int(row.css))) _eeg(s, row.key, pac_id, row) _eeg(s, "e-000-b", 1, t.iloc[0], studied=date(2025, 9, 1)) _eeg(s, "e-001-x", 2, t.iloc[1], studied=date(2025, 3, 1), valid=0) s.add(Pacientes(pac_id=99, pac_id_hospital="H-99")) _eeg(s, "e-099", 99, t.iloc[2]) s.commit() return t def _read(): with Session(_engine()) as s: return s.exec(select(RelacionAdos2)).all(), s.exec(select(RelacionAdos2Paciente)).all() def test_a_fit_writes_one_version_of_estimated_rows_on_the_platform_codes(stored): """Pins down: one fit writes a `tea-