Download tests/test_quirofanos.py from Modulo-6/api: direct link, hf CLI and curl.
- Browser
- Download file 12.2 kB
-
https://huggingface.co/spaces/Modulo-6/api/resolve/main/tests/test_quirofanos.py
- Command line
-
hf download hf://spaces/Modulo-6/api/tests/test_quirofanos.py
-
curl -L -o test_quirofanos.py https://huggingface.co/spaces/Modulo-6/api/resolve/main/tests/test_quirofanos.py
12.2 kB
| """ | |
| 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 | |