Download tests/test_camas.py from Modulo-6/api: direct link, hf CLI and curl.
- Browser
- Download file 8.49 kB
-
https://huggingface.co/spaces/Modulo-6/api/resolve/6d76dbb2a8d2a751cc49fa06e2ed33f9f6be5aa0/tests/test_camas.py
- Command line
-
hf download hf://spaces/Modulo-6/api@6d76dbb2a8d2a751cc49fa06e2ed33f9f6be5aa0/tests/test_camas.py
-
curl -L -o test_camas.py https://huggingface.co/spaces/Modulo-6/api/resolve/6d76dbb2a8d2a751cc49fa06e2ed33f9f6be5aa0/tests/test_camas.py
8.49 kB
| """ | |
| Tests del router de Camas. | |
| Mockean la capa CRUD para ejercitar routing + auth/roles + reglas de negocio + | |
| serialización, sin DB real. | |
| """ | |
| from fastapi.testclient import TestClient | |
| from tests.factories import make_cama | |
| PREFIX = "/api/v1/camas" | |
| # ─── Auth / roles ────────────────────────────────────────────────────────── | |
| def test_get_camas_sin_token_401(client: TestClient): | |
| assert client.get(PREFIX).status_code == 401 | |
| def test_crear_cama_rol_no_admin_403(client: TestClient, medico_headers): | |
| payload = { | |
| "numero": 101, "piso": 1, "sector": "SALA_COMUN", | |
| "tipo": "ESTANDAR", "hospital_id": "HOSP-001", | |
| } | |
| resp = client.post(PREFIX, json=payload, headers=medico_headers) | |
| assert resp.status_code == 403 | |
| # ─── Listado (mapa) ──────────────────────────────────────────────────────── | |
| def test_get_camas_ok(client: TestClient, medico_headers, monkeypatch): | |
| monkeypatch.setattr( | |
| "app.crud.cama.get_all", | |
| lambda *a, **k: [make_cama(estado="DISPONIBLE"), make_cama(id=102, estado="OCUPADA")], | |
| ) | |
| resp = client.get(PREFIX, headers=medico_headers) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["total"] == 2 | |
| assert data["disponibles"] == 1 | |
| assert data["ocupadas"] == 1 | |
| # ─── Alta ────────────────────────────────────────────────────────────────── | |
| def test_crear_cama_ok(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.create", lambda db, data: make_cama(numero=data.numero)) | |
| payload = { | |
| "numero": 101, "piso": 1, "sector": "SALA_COMUN", | |
| "tipo": "ESTANDAR", "hospital_id": "HOSP-001", | |
| "contraindicaciones_incompatibles": [], | |
| } | |
| resp = client.post(PREFIX, json=payload, headers=admin_headers) | |
| assert resp.status_code == 201 | |
| data = resp.json() | |
| assert data["numero"] == 101 # integer, no "101" | |
| assert data["estado"] == "DISPONIBLE" | |
| def test_crear_cama_hospital_id_vacio_422(client: TestClient, admin_headers): | |
| payload = { | |
| "numero": 101, "piso": 1, "sector": "SALA_COMUN", | |
| "tipo": "ESTANDAR", "hospital_id": " ", | |
| } | |
| resp = client.post(PREFIX, json=payload, headers=admin_headers) | |
| assert resp.status_code == 422 | |
| # ─── Asignación / baja: reglas de estado ─────────────────────────────────── | |
| def test_asignar_cama_ocupada_409(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="OCUPADA")) | |
| payload = {"tipo": "INGRESO", "paciente_id": 7} | |
| resp = client.post(f"{PREFIX}/101/asignar", json=payload, headers=admin_headers) | |
| assert resp.status_code == 409 | |
| def test_delete_cama_en_uso_409(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="OCUPADA")) | |
| resp = client.delete(f"{PREFIX}/101", headers=admin_headers) | |
| assert resp.status_code == 409 | |
| def test_delete_cama_ok(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE")) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama) | |
| resp = client.delete(f"{PREFIX}/101", headers=admin_headers) | |
| assert resp.status_code == 200 | |
| assert resp.json()["estado"] == "BAJA_PERMANENTE" | |
| # ─── Detalle ─────────────────────────────────────────────────────────────── | |
| def test_get_cama_ok(client: TestClient, medico_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama()) | |
| resp = client.get(f"{PREFIX}/101", headers=medico_headers) | |
| assert resp.status_code == 200 | |
| assert resp.json()["id"] == 101 | |
| def test_get_cama_404(client: TestClient, medico_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: None) | |
| resp = client.get(f"{PREFIX}/999", headers=medico_headers) | |
| assert resp.status_code == 404 | |
| # ─── PATCH estado ────────────────────────────────────────────────────────── | |
| def test_patch_cama_fuera_de_servicio_ok(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE")) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama) | |
| resp = client.patch(f"{PREFIX}/101", json={"estado": "FUERA_DE_SERVICIO"}, headers=admin_headers) | |
| assert resp.status_code == 200 | |
| assert resp.json()["estado"] == "FUERA_DE_SERVICIO" | |
| def test_patch_cama_estado_automatico_409(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE")) | |
| resp = client.patch(f"{PREFIX}/101", json={"estado": "OCUPADA"}, headers=admin_headers) | |
| assert resp.status_code == 409 | |
| def test_patch_cama_limpieza_solo_puede_marcar_disponible(client: TestClient, limpieza_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE")) | |
| resp = client.patch(f"{PREFIX}/101", json={"estado": "FUERA_DE_SERVICIO"}, headers=limpieza_headers) | |
| assert resp.status_code == 403 | |
| def test_patch_cama_limpieza_ok(client: TestClient, limpieza_headers, monkeypatch): | |
| monkeypatch.setattr( | |
| "app.crud.cama.get_by_id", | |
| lambda db, cid: make_cama(estado="EN_LIMPIEZA", ultimo_movimiento_tipo="TRASLADO"), | |
| ) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama) | |
| resp = client.patch(f"{PREFIX}/101", json={"estado": "DISPONIBLE"}, headers=limpieza_headers) | |
| assert resp.status_code == 200 | |
| assert resp.json()["estado"] == "DISPONIBLE" | |
| # ─── Asignación directa ──────────────────────────────────────────────────── | |
| def test_asignar_cama_ingreso_ok(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE")) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama) | |
| monkeypatch.setattr("app.integrations.hce.get_contraindicaciones", lambda pid: []) | |
| monkeypatch.setattr("app.integrations.hce.notify_movimiento", lambda *a, **k: None) | |
| monkeypatch.setattr("app.integrations.monitoreo.notify_alta", lambda *a, **k: None) | |
| resp = client.post( | |
| f"{PREFIX}/101/asignar", | |
| json={"tipo": "INGRESO", "paciente_id": 7}, | |
| headers=admin_headers, | |
| ) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["cama_id"] == 101 | |
| assert data["paciente_id"] == 7 | |
| assert data["tipo"] == "INGRESO" | |
| def test_asignar_cama_traslado_ok(client: TestClient, admin_headers, monkeypatch): | |
| cama_destino = make_cama(id=102, numero=102, estado="DISPONIBLE") | |
| cama_origen = make_cama(id=101, estado="OCUPADA", paciente_id=7) | |
| def fake_get(db, cid): | |
| return cama_destino if cid == 102 else cama_origen | |
| monkeypatch.setattr("app.crud.cama.get_by_id", fake_get) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama) | |
| monkeypatch.setattr("app.integrations.hce.get_contraindicaciones", lambda pid: []) | |
| monkeypatch.setattr("app.integrations.hce.notify_movimiento", lambda *a, **k: None) | |
| monkeypatch.setattr("app.integrations.monitoreo.notify_alta", lambda *a, **k: None) | |
| monkeypatch.setattr("app.integrations.monitoreo.notify_baja", lambda *a, **k: None) | |
| resp = client.post( | |
| f"{PREFIX}/102/asignar", | |
| json={"tipo": "TRASLADO", "paciente_id": 7, "cama_origen_id": 101}, | |
| headers=admin_headers, | |
| ) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["tipo"] == "TRASLADO" | |
| assert data["cama_origen_id"] == 101 | |