98 lines
3.8 KiB
Python
98 lines
3.8 KiB
Python
"""What this module covers: `POST /governance/tea/eeg/upload`, which registers one EDF as an EEG through the import
|
|
pipeline (`tea.Eeg`, upsert on the laboratory id).
|
|
|
|
Fixtures: `app_client`, a migrated empty TEA database (`base_de`) behind the real app; `edf`, a synthetic EDF of two
|
|
minutes from `models/tests/tea/synth_edf.py` (nothing comes from a patient).
|
|
"""
|
|
import importlib.util
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
|
|
SYNTH = Path(__file__).resolve().parents[2] / "models" / "tests" / "tea" / "synth_edf.py"
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def edf() -> bytes:
|
|
"""A synthetic two-minute EDF, the same file for every test."""
|
|
spec = importlib.util.spec_from_file_location("synth_edf", SYNTH)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module.edf_bytes(minutes=2)
|
|
|
|
|
|
@pytest.fixture
|
|
def app_client(tmp_path):
|
|
"""The real app over an empty migrated TEA database; yields (client, engine)."""
|
|
from conftest import base_de
|
|
|
|
from webapi import create_app
|
|
|
|
base = base_de("tea", tmp_path, datos=lambda session: None)
|
|
app = create_app()
|
|
app.config.update(TESTING=True)
|
|
try:
|
|
with app.test_client() as client:
|
|
yield client, base.motor
|
|
finally:
|
|
base.motor.dispose()
|
|
|
|
|
|
def _post(client, edf, name="estudio-1.edf", **form):
|
|
from io import BytesIO
|
|
|
|
return client.post("/governance/tea/eeg/upload", data={"file": (BytesIO(edf), name), **form},
|
|
content_type="multipart/form-data")
|
|
|
|
|
|
def test_an_uploaded_edf_becomes_an_eeg_row(app_client, edf):
|
|
"""Pins down: a valid EDF is stored in `eeg` with its file, and the answer carries the new id and the lab id.
|
|
|
|
Fails if the route stops going through the `tea.Eeg` import (no row, no bytes, or no id in the answer).
|
|
"""
|
|
client, engine = app_client
|
|
response = _post(client, edf)
|
|
assert response.status_code == 201, response.get_json()
|
|
body = response.get_json()
|
|
assert body["created"] is True and body["eeg_id"] is not None and body["lab_id"]
|
|
with engine.connect() as connection:
|
|
stored = connection.execute(text("select length(eeg_estudio), eeg_pac_id from eeg")).one()
|
|
assert stored[0] == len(edf) and stored[1] is None
|
|
|
|
|
|
def test_uploading_the_same_study_twice_updates_it(app_client, edf):
|
|
"""Pins down: the laboratory id is the natural key, so a second upload updates the row and does not add one.
|
|
|
|
Fails if the upsert policy is lost and the study is duplicated or rejected.
|
|
"""
|
|
client, engine = app_client
|
|
first = _post(client, edf).get_json()
|
|
second = _post(client, edf)
|
|
assert second.status_code == 201
|
|
assert second.get_json()["created"] is False and second.get_json()["eeg_id"] == first["eeg_id"]
|
|
with engine.connect() as connection:
|
|
assert connection.execute(text("select count(*) from eeg")).scalar() == 1
|
|
|
|
|
|
def test_a_file_that_is_not_an_edf_is_refused(app_client):
|
|
"""Pins down: bytes MNE cannot open answer 400 with a message and write nothing.
|
|
|
|
Fails if the connector's validation is bypassed and garbage reaches the table.
|
|
"""
|
|
client, engine = app_client
|
|
response = _post(client, b"not an edf at all")
|
|
assert response.status_code == 400 and response.get_json()["error"]
|
|
with engine.connect() as connection:
|
|
assert connection.execute(text("select count(*) from eeg")).scalar() == 0
|
|
|
|
|
|
def test_the_request_needs_a_file_and_a_numeric_patient(app_client, edf):
|
|
"""Pins down: a missing `file` and a non-numeric `patient_id` are 400s before any import runs.
|
|
|
|
Fails if the form is read without checking, which would turn bad input into a 500.
|
|
"""
|
|
client, _ = app_client
|
|
assert client.post("/governance/tea/eeg/upload", data={}).status_code == 400
|
|
assert _post(client, edf, patient_id="abc").status_code == 400
|