Mate11 commited on
Commit
83364ba
·
1 Parent(s): 3558ffa

Tests: agregar happy paths y casos de error faltantes (60 tests total)

Browse files
tests/test_camas.py CHANGED
@@ -124,3 +124,58 @@ def test_patch_cama_limpieza_solo_puede_marcar_disponible(client: TestClient, li
124
  monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
125
  resp = client.patch(f"{PREFIX}/101", json={"estado": "FUERA_DE_SERVICIO"}, headers=limpieza_headers)
126
  assert resp.status_code == 403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
125
  resp = client.patch(f"{PREFIX}/101", json={"estado": "FUERA_DE_SERVICIO"}, headers=limpieza_headers)
126
  assert resp.status_code == 403
127
+
128
+
129
+ def test_patch_cama_limpieza_ok(client: TestClient, limpieza_headers, monkeypatch):
130
+ monkeypatch.setattr(
131
+ "app.crud.cama.get_by_id",
132
+ lambda db, cid: make_cama(estado="EN_LIMPIEZA", ultimo_movimiento_tipo="TRASLADO"),
133
+ )
134
+ monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
135
+ resp = client.patch(f"{PREFIX}/101", json={"estado": "DISPONIBLE"}, headers=limpieza_headers)
136
+ assert resp.status_code == 200
137
+ assert resp.json()["estado"] == "DISPONIBLE"
138
+
139
+
140
+ # ─── Asignación directa ────────────────────────────────────────────────────
141
+
142
+ def test_asignar_cama_ingreso_ok(client: TestClient, admin_headers, monkeypatch):
143
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
144
+ monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
145
+ monkeypatch.setattr("app.integrations.hce.get_contraindicaciones", lambda pid: [])
146
+ monkeypatch.setattr("app.integrations.hce.notify_movimiento", lambda *a, **k: None)
147
+ monkeypatch.setattr("app.integrations.monitoreo.notify_alta", lambda *a, **k: None)
148
+ resp = client.post(
149
+ f"{PREFIX}/101/asignar",
150
+ json={"tipo": "INGRESO", "paciente_id": 7},
151
+ headers=admin_headers,
152
+ )
153
+ assert resp.status_code == 200
154
+ data = resp.json()
155
+ assert data["cama_id"] == 101
156
+ assert data["paciente_id"] == 7
157
+ assert data["tipo"] == "INGRESO"
158
+
159
+
160
+ def test_asignar_cama_traslado_ok(client: TestClient, admin_headers, monkeypatch):
161
+ cama_destino = make_cama(id=102, numero=102, estado="DISPONIBLE")
162
+ cama_origen = make_cama(id=101, estado="OCUPADA", paciente_id=7)
163
+
164
+ def fake_get(db, cid):
165
+ return cama_destino if cid == 102 else cama_origen
166
+
167
+ monkeypatch.setattr("app.crud.cama.get_by_id", fake_get)
168
+ monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
169
+ monkeypatch.setattr("app.integrations.hce.get_contraindicaciones", lambda pid: [])
170
+ monkeypatch.setattr("app.integrations.hce.notify_movimiento", lambda *a, **k: None)
171
+ monkeypatch.setattr("app.integrations.monitoreo.notify_alta", lambda *a, **k: None)
172
+ monkeypatch.setattr("app.integrations.monitoreo.notify_baja", lambda *a, **k: None)
173
+ resp = client.post(
174
+ f"{PREFIX}/102/asignar",
175
+ json={"tipo": "TRASLADO", "paciente_id": 7, "cama_origen_id": 101},
176
+ headers=admin_headers,
177
+ )
178
+ assert resp.status_code == 200
179
+ data = resp.json()
180
+ assert data["tipo"] == "TRASLADO"
181
+ assert data["cama_origen_id"] == 101
tests/test_quirofanos.py CHANGED
@@ -161,6 +161,14 @@ def test_crear_reserva_superpuesta_409(client: TestClient, medico_headers, monke
161
  assert resp.status_code == 409
162
 
163
 
 
 
 
 
 
 
 
 
164
  def test_get_reserva_404(client: TestClient, medico_headers, monkeypatch):
165
  monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
166
  monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: None)
@@ -204,6 +212,19 @@ def test_patch_reserva_transicion_invalida_409(client: TestClient, admin_headers
204
  assert resp.status_code == 409
205
 
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  def test_disponibilidad_sin_fechas_422(client: TestClient, medico_headers):
208
  resp = client.get(f"{PREFIX}/disponibilidad", headers=medico_headers)
209
  assert resp.status_code == 422
 
161
  assert resp.status_code == 409
162
 
163
 
164
+ def test_crear_reserva_idempotente_200(client: TestClient, medico_headers, monkeypatch):
165
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
166
+ monkeypatch.setattr("app.crud.reserva.get_activa_por_turno", lambda db, turno: make_reserva())
167
+ resp = client.post(f"{PREFIX}/501/reservas", json=_reserva_payload(), headers=medico_headers)
168
+ assert resp.status_code == 200
169
+ assert resp.json()["estado"] == "RESERVADA"
170
+
171
+
172
  def test_get_reserva_404(client: TestClient, medico_headers, monkeypatch):
173
  monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
174
  monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: None)
 
212
  assert resp.status_code == 409
213
 
214
 
215
+ def test_patch_reserva_completada_ok(client: TestClient, admin_headers, monkeypatch):
216
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
217
+ monkeypatch.setattr(
218
+ "app.crud.reserva.get_by_cama_and_id",
219
+ lambda db, cid, rid: make_reserva(estado="EN_CURSO"),
220
+ )
221
+ monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
222
+ monkeypatch.setattr("app.crud.reserva.save", lambda db, reserva: reserva)
223
+ resp = client.patch(f"{PREFIX}/501/reservas/1", json={"estado": "COMPLETADA"}, headers=admin_headers)
224
+ assert resp.status_code == 200
225
+ assert resp.json()["estado"] == "COMPLETADA"
226
+
227
+
228
  def test_disponibilidad_sin_fechas_422(client: TestClient, medico_headers):
229
  resp = client.get(f"{PREFIX}/disponibilidad", headers=medico_headers)
230
  assert resp.status_code == 422
tests/test_solicitudes.py CHANGED
@@ -8,7 +8,7 @@ tests dedicados que mockean la capa de integraciones.
8
  from datetime import datetime, timezone
9
  from fastapi.testclient import TestClient
10
 
11
- from tests.factories import make_solicitud
12
 
13
  PREFIX = "/api/v1/solicitudes"
14
 
@@ -84,6 +84,28 @@ def test_get_solicitud_ok(client: TestClient, medico_headers, monkeypatch):
84
  assert data["estado"] == "PENDIENTE"
85
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def test_resolver_solicitud_rechazar_ok(client: TestClient, admin_headers, monkeypatch):
88
  sol = make_solicitud()
89
  monkeypatch.setattr("app.crud.solicitud.get_by_id", lambda db, num: sol)
 
8
  from datetime import datetime, timezone
9
  from fastapi.testclient import TestClient
10
 
11
+ from tests.factories import make_cama, make_solicitud
12
 
13
  PREFIX = "/api/v1/solicitudes"
14
 
 
84
  assert data["estado"] == "PENDIENTE"
85
 
86
 
87
+ def test_resolver_solicitud_aprobar_ok(client: TestClient, admin_headers, monkeypatch):
88
+ sol = make_solicitud()
89
+ monkeypatch.setattr("app.crud.solicitud.get_by_id", lambda db, num: sol)
90
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
91
+ monkeypatch.setattr("app.crud.cama.save", lambda db, c: c)
92
+ monkeypatch.setattr("app.crud.solicitud.save", lambda db, s: s)
93
+ monkeypatch.setattr("app.integrations.core.validate_patient", lambda pid: {"id": pid})
94
+ monkeypatch.setattr("app.integrations.hce.get_contraindicaciones", lambda pid: [])
95
+ monkeypatch.setattr("app.integrations.hce.notify_movimiento", lambda *a, **k: None)
96
+ monkeypatch.setattr("app.integrations.hce.notify_resultado_solicitud", lambda *a, **k: None)
97
+ monkeypatch.setattr("app.integrations.monitoreo.notify_alta", lambda *a, **k: None)
98
+ resp = client.patch(
99
+ f"{PREFIX}/SOL-00001",
100
+ json={"accion": "APROBAR"},
101
+ headers=admin_headers,
102
+ )
103
+ assert resp.status_code == 200
104
+ data = resp.json()
105
+ assert data["estado"] == "APROBADA"
106
+ assert data["accion"] == "APROBAR"
107
+
108
+
109
  def test_resolver_solicitud_rechazar_ok(client: TestClient, admin_headers, monkeypatch):
110
  sol = make_solicitud()
111
  monkeypatch.setattr("app.crud.solicitud.get_by_id", lambda db, num: sol)
tests/test_webhooks.py CHANGED
@@ -7,7 +7,7 @@ reglas de negocio + serialización, sin DB real.
7
  from datetime import datetime, timezone
8
  from fastapi.testclient import TestClient
9
 
10
- from tests.factories import make_cama, make_reserva
11
 
12
  WEBHOOKS_PREFIX = "/api/v1/webhooks"
13
  TURNOS_PREFIX = "/api/v1/turnos"
@@ -87,6 +87,30 @@ def test_alerta_resuelta_404(client: TestClient, admin_headers, monkeypatch):
87
  assert resp.status_code == 404
88
 
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  # ─── Cirugía urgente sin quirófanos disponibles (M1 HCE) ─────────────────
91
 
92
  def test_cirugia_urgente_sin_quirofanos_409(client: TestClient, admin_headers, monkeypatch):
@@ -124,3 +148,10 @@ def test_cancelar_reserva_quirofano_404(client: TestClient, admin_headers, monke
124
  payload = {"motivo": "paciente cancela", "timestamp": _NOW_ISO}
125
  resp = client.post(f"{TURNOS_PREFIX}/TURNO-999/cancelacion", json=payload, headers=admin_headers)
126
  assert resp.status_code == 404
 
 
 
 
 
 
 
 
7
  from datetime import datetime, timezone
8
  from fastapi.testclient import TestClient
9
 
10
+ from tests.factories import make_cama, make_quirofano, make_reserva
11
 
12
  WEBHOOKS_PREFIX = "/api/v1/webhooks"
13
  TURNOS_PREFIX = "/api/v1/turnos"
 
87
  assert resp.status_code == 404
88
 
89
 
90
+ # ─── Cirugía urgente (M1 HCE) ────────────────────────────────────────────
91
+
92
+ def test_cirugia_urgente_ok_201(client: TestClient, admin_headers, monkeypatch):
93
+ reserva_urgente = make_reserva(origen="EMERGENCIA", prioridad="URGENTE")
94
+ monkeypatch.setattr("app.crud.reserva.get_by_urgencia_hce_id", lambda db, hce_id: None)
95
+ monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: [make_quirofano()])
96
+ monkeypatch.setattr("app.crud.reserva.get_superpuestas", lambda *a, **k: [])
97
+ monkeypatch.setattr("app.crud.reserva.create_urgente", lambda db, cama_id, data: reserva_urgente)
98
+ payload = {
99
+ "solicitud_urgencia_hce_id": "URG-001",
100
+ "paciente_id": 7,
101
+ "medico_cirujano_id": 22,
102
+ "medico_solicitante_id": 10,
103
+ "fecha_hora_inicio": "2025-01-18T10:00:00Z",
104
+ "fecha_hora_fin_estimada": "2025-01-18T12:00:00Z",
105
+ "timestamp": _NOW_ISO,
106
+ }
107
+ resp = client.post(f"{WEBHOOKS_PREFIX}/hce/cirugia-urgente", json=payload, headers=admin_headers)
108
+ assert resp.status_code == 201
109
+ data = resp.json()
110
+ assert data["reserva"]["estado"] == "RESERVADA"
111
+ assert data["reserva_desplazada"] is None
112
+
113
+
114
  # ─── Cirugía urgente sin quirófanos disponibles (M1 HCE) ─────────────────
115
 
116
  def test_cirugia_urgente_sin_quirofanos_409(client: TestClient, admin_headers, monkeypatch):
 
148
  payload = {"motivo": "paciente cancela", "timestamp": _NOW_ISO}
149
  resp = client.post(f"{TURNOS_PREFIX}/TURNO-999/cancelacion", json=payload, headers=admin_headers)
150
  assert resp.status_code == 404
151
+
152
+
153
+ def test_cancelar_reserva_en_curso_409(client: TestClient, admin_headers, monkeypatch):
154
+ monkeypatch.setattr("app.crud.reserva.get_by_turno", lambda db, turno_id: make_reserva(estado="EN_CURSO"))
155
+ payload = {"motivo": "intento cancelar", "timestamp": _NOW_ISO}
156
+ resp = client.post(f"{TURNOS_PREFIX}/TURNO-001/cancelacion", json=payload, headers=admin_headers)
157
+ assert resp.status_code == 409