"""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): from core.cama.models import Mastografia from models.cama import CaMaModel 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 model = CaMaModel() seen = {row["pac_id_hospital"]: row["mast_birads"] for row in model.dataset("mastografia")} assert seen == {"RT-1": "B4", "RT-2": "B2"} model.repository("mastografia").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"]}