File size: 12,220 Bytes
2c5d6e7 f485d1b 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 2c5d6e7 4bd44c0 83364ba 4bd44c0 83364ba 2c5d6e7 3558ffa 83364ba 2c5d6e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | """
Tests del router de Quirófanos.
Mockean la capa CRUD (`app.crud.cama` / `app.crud.reserva`) para ejercitar
routing + auth/roles + reglas de negocio de servicios + serialización, sin DB real.
"""
from fastapi.testclient import TestClient
from tests.factories import make_cama, make_quirofano, make_reserva
PREFIX = "/api/v1/quirofanos"
def _reserva_payload(**overrides) -> dict:
payload = {
"turno_id": 5501,
"paciente_id": 7,
"medico_cirujano_id": 22,
"fecha_hora_inicio": "2025-01-18T08:00:00Z",
"fecha_hora_fin_estimada": "2025-01-18T10:00:00Z",
"prioridad": "MEDIA",
}
payload.update(overrides)
return payload
# ─── Auth / roles ──────────────────────────────────────────────────────────
def test_listar_quirofanos_sin_token_401(client: TestClient):
assert client.get(PREFIX).status_code == 401
def test_crear_quirofano_rol_no_admin_403(client: TestClient, medico_headers):
payload = {"numero": 501, "piso": 5, "specialty_id": 43, "hospital_id": "HOSP-001"}
resp = client.post(PREFIX, json=payload, headers=medico_headers)
assert resp.status_code == 403
# ─── Listado ───────────────────────────────────────────────────────────────
def test_listar_quirofanos_ok(client: TestClient, medico_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: [make_quirofano()])
resp = client.get(PREFIX, headers=medico_headers)
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 1
q = data["quirofanos"][0]
assert q["id"] == 501
assert q["numero"] == 501 # integer (decisión changelog #12)
assert q["specialty_id"] == 43
# ─── Alta ──────────────────────────────────────────────────────────────────
def test_crear_quirofano_setea_sector_y_tipo_internos(client: TestClient, admin_headers, monkeypatch):
capturado = {}
def fake_create(db, cama_req):
capturado["req"] = cama_req
return make_quirofano(numero=cama_req.numero, piso=cama_req.piso, specialty_id=cama_req.specialty_id)
monkeypatch.setattr("app.crud.cama.create", fake_create)
payload = {"numero": 777, "piso": 5, "specialty_id": 43, "hospital_id": "HOSP-001"}
resp = client.post(PREFIX, json=payload, headers=admin_headers)
assert resp.status_code == 201
data = resp.json()
assert data["numero"] == 777
assert data["estado"] == "DISPONIBLE"
# El cliente no manda sector/tipo: el servicio los fija
assert capturado["req"].sector.value == "QUIROFANO"
assert capturado["req"].tipo.value == "CIRUGIA"
# ─── Detalle ───────────────────────────────────────────────────────────────
def test_get_quirofano_ok(client: TestClient, medico_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
resp = client.get(f"{PREFIX}/501", headers=medico_headers)
assert resp.status_code == 200
assert resp.json()["id"] == 501
def test_get_quirofano_404_si_no_existe(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
def test_get_quirofano_404_si_es_cama_comun(client: TestClient, medico_headers, monkeypatch):
# Existe la fila pero no es un quirófano → no es recurso /quirofanos
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 == 404
# ─── Update ────────────────────────────────────────────────────────────────
def test_patch_quirofano_actualiza_specialty(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano(specialty_id=43))
monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
resp = client.patch(f"{PREFIX}/501", json={"specialty_id": 99}, headers=admin_headers)
assert resp.status_code == 200
assert resp.json()["specialty_id"] == 99
def test_patch_quirofano_estado_automatico_409(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
# OCUPADA solo se asigna automáticamente, no por PATCH manual
resp = client.patch(f"{PREFIX}/501", json={"estado": "OCUPADA"}, headers=admin_headers)
assert resp.status_code == 409
# ─── Reservas ──────────────────────────────────────────────────────────────
def test_crear_reserva_medico_403(client: TestClient, medico_headers):
resp = client.post(f"{PREFIX}/501/reservas", json=_reserva_payload(), headers=medico_headers)
assert resp.status_code == 403
def test_crear_reserva_en_cama_no_quirofano_409(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama()) # SALA_COMUN
resp = client.post(f"{PREFIX}/101/reservas", json=_reserva_payload(), headers=admin_headers)
assert resp.status_code == 409
def test_crear_reserva_urgente_rechazada_422(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
resp = client.post(
f"{PREFIX}/501/reservas",
json=_reserva_payload(prioridad="URGENTE"),
headers=admin_headers,
)
assert resp.status_code == 422
def test_crear_reserva_fechas_invertidas_422(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
resp = client.post(
f"{PREFIX}/501/reservas",
json=_reserva_payload(
fecha_hora_inicio="2025-01-18T10:00:00Z",
fecha_hora_fin_estimada="2025-01-18T08:00:00Z",
),
headers=admin_headers,
)
assert resp.status_code == 422
def test_crear_reserva_ok_201(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_activa_por_turno", lambda db, turno: None)
monkeypatch.setattr("app.crud.reserva.get_superpuestas", lambda *a, **k: [])
monkeypatch.setattr("app.crud.reserva.create_programada", lambda db, cid, data: make_reserva())
resp = client.post(f"{PREFIX}/501/reservas", json=_reserva_payload(), headers=admin_headers)
assert resp.status_code == 201
assert resp.json()["estado"] == "RESERVADA"
def test_crear_reserva_superpuesta_409(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_activa_por_turno", lambda db, turno: None)
monkeypatch.setattr("app.crud.reserva.get_superpuestas", lambda *a, **k: [make_reserva()])
resp = client.post(f"{PREFIX}/501/reservas", json=_reserva_payload(), headers=admin_headers)
assert resp.status_code == 409
def test_crear_reserva_idempotente_200(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_activa_por_turno", lambda db, turno: make_reserva())
resp = client.post(f"{PREFIX}/501/reservas", json=_reserva_payload(), headers=admin_headers)
assert resp.status_code == 200
assert resp.json()["estado"] == "RESERVADA"
def test_get_reserva_404(client: TestClient, medico_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: None)
resp = client.get(f"{PREFIX}/501/reservas/999", headers=medico_headers)
assert resp.status_code == 404
# ─── Disponibilidad ────────────────────────────────────────────────────────
def test_get_reservas_ok(client: TestClient, medico_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_all", lambda *a, **k: [make_reserva()])
resp = client.get(f"{PREFIX}/501/reservas", headers=medico_headers)
assert resp.status_code == 200
data = resp.json()
assert data["total"] == 1
def test_get_reserva_ok(client: TestClient, medico_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: make_reserva())
resp = client.get(f"{PREFIX}/501/reservas/1", headers=medico_headers)
assert resp.status_code == 200
assert resp.json()["id"] == 1
def test_patch_reserva_estado_en_curso_ok(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: make_reserva(estado="RESERVADA"))
monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
monkeypatch.setattr("app.crud.reserva.save", lambda db, reserva: reserva)
resp = client.patch(f"{PREFIX}/501/reservas/1", json={"estado": "EN_CURSO"}, headers=admin_headers)
assert resp.status_code == 200
assert resp.json()["estado"] == "EN_CURSO"
def test_patch_reserva_transicion_invalida_409(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: make_reserva(estado="EN_CURSO"))
resp = client.patch(f"{PREFIX}/501/reservas/1", json={"estado": "RESERVADA"}, headers=admin_headers)
assert resp.status_code == 409
def test_patch_reserva_completada_ok(client: TestClient, admin_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
monkeypatch.setattr(
"app.crud.reserva.get_by_cama_and_id",
lambda db, cid, rid: make_reserva(estado="EN_CURSO"),
)
monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
monkeypatch.setattr("app.crud.reserva.save", lambda db, reserva: reserva)
resp = client.patch(f"{PREFIX}/501/reservas/1", json={"estado": "COMPLETADA"}, headers=admin_headers)
assert resp.status_code == 200
assert resp.json()["estado"] == "COMPLETADA"
def test_disponibilidad_sin_fechas_422(client: TestClient, medico_headers):
resp = client.get(f"{PREFIX}/disponibilidad", headers=medico_headers)
assert resp.status_code == 422
def test_disponibilidad_ok(client: TestClient, medico_headers, monkeypatch):
monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: [make_quirofano()])
monkeypatch.setattr("app.crud.reserva.get_superpuestas", lambda *a, **k: [])
resp = client.get(
f"{PREFIX}/disponibilidad",
params={"desde": "2025-01-18T08:00:00Z", "hasta": "2025-01-18T18:00:00Z"},
headers=medico_headers,
)
assert resp.status_code == 200
data = resp.json()
assert data["total_quirofanos"] == 1
assert data["quirofanos"][0]["cama_id"] == 501
|