"""WebApi and `models` talk to each other through the database, both ways, over the same Repository objects: rows imported over HTTP are what a model trains on, and rows a model writes are what the API serves.""" import base64 import pytest @pytest.fixture(scope="module") def client(wire_domain): wire_domain("cama") from webapi import create_app app = create_app() app.config.update(TESTING=True) with app.test_client() as test_client: yield test_client def test_imported_rows_are_what_models_reads_and_model_writes_are_what_the_api_serves(client): """Rows imported over HTTP are read back through `models`' repository, and a row written there is served by the API. Fails if `resolve_repository` stops returning the objects WebApi writes with.""" from core.cama.models import Mastografia from models.io.storage import resolve_repository csv = b"Expediente,BIRADS\nRT-1,BI-RADS 4\nRT-2,2\n" response = client.post( "/imports/commit", json={ "sources": [ { "source_type": "csv", "target": "cama.Mastografia", "column_mapping": {"Expediente": "pac_id_hospital", "BIRADS": "mast_birads"}, "payload": {"file_base64": base64.b64encode(csv).decode()}, } ] }, ) assert response.get_json()["results"][0]["created"] == 2 repository = resolve_repository("cama", "mastografia") seen = {row.pac_id_hospital: row.mast_birads for row in repository.iter_all()} assert seen == {"RT-1": "B4", "RT-2": "B2"} repository.create(Mastografia(pac_id_hospital="RT-FROM-MODEL", mast_birads="B1")) served = client.get("/data/cama.Mastografia").get_json() assert served["total"] == 3 assert "RT-FROM-MODEL" in {row["pac_id_hospital"] for row in served["rows"]}