Files
ISSSTELEON/tests/test_persistance_eeg_study.py
T
Roberto Treviño d15b468d56
ci / python (push) Failing after 2m48s
ci / web (push) Successful in 9m52s
Format the EEG study test; governance spec follows the scenarios to the record
2026-11-28 17:16:26 -06:00

49 lines
2.4 KiB
Python

"""One EEG as the patient record reads it (persistance.eeg.study): each metric with its age
curve's score, and its zones and electrodes with theirs, so the record can say which zones
are most affected."""
import json
import pytest
from sqlalchemy import text
from persistance import destination, eeg
@pytest.fixture
def one_eeg(tmp_path, monkeypatch):
monkeypatch.setenv("PLATFORM_DATABASE_URL", f"sqlite:///{tmp_path / 'platform.db'}")
monkeypatch.setattr(destination, "DATA_DIR", tmp_path) # never the deployed data folder
destination.engine.cache_clear()
with destination.engine().begin() as con:
con.execute(text("create table silver.eeg (person_id text, study_date text, edf text)"))
con.execute(text("create table bronze.eeg_analysis (edf text, valid boolean, "
"level text, age_months real)")) # fmt: skip
con.execute(text("create table bronze.eeg_metrics (edf text, code text, value real, "
"electrodes text, zones text)")) # fmt: skip
con.execute(text("create table bronze.eeg_scores (edf text, metric text, part text, "
"site text, z real, outside real, p5 real, median real, p95 real, "
"model text)")) # fmt: skip
con.execute(text("insert into silver.eeg values ('p1', '2026-10-01', 'h1')"))
con.execute(text("insert into bronze.eeg_analysis values ('h1', 1, 'good', 30)"))
con.execute(text("insert into bronze.eeg_metrics values ('h1', 'spindles_n2', 2.0, "
":e, :z)"), {"e": json.dumps({"C3": 1.5}), "z": json.dumps(
{"central": 1.9, "occipital": float("nan")})}) # fmt: skip
con.execute(
text(
"insert into bronze.eeg_scores values "
"('h1', 'spindles_n2', 'value', null, -2.4, -1, 3, 4, 5, 'm/1'), "
"('h1', 'spindles_n2', 'zones', 'central', -2.6, -1, 3, 4, 5, 'm/1')"
)
)
yield
destination.engine.cache_clear()
def test_a_metric_carries_its_zones_and_electrodes_with_their_scores(one_eeg):
found = eeg.study("p1|2026-10-01")
(metric,) = found["metrics"]
assert (metric["z"], metric["p5"], metric["p95"]) == (-2.4, 3, 5)
assert metric["zones"] == [{"site": "central", "value": 1.9, "z": -2.6}] # NaN left out
assert metric["electrodes"] == [{"site": "C3", "value": 1.5, "z": None}] # no curve