Mate11 commited on
Commit
3558ffa
·
1 Parent(s): 09d4024

Tests: cobertura completa de todos los endpoints (52 tests)

Browse files
tests/test_camas.py CHANGED
@@ -87,3 +87,40 @@ def test_delete_cama_ok(client: TestClient, admin_headers, monkeypatch):
87
  resp = client.delete(f"{PREFIX}/101", headers=admin_headers)
88
  assert resp.status_code == 200
89
  assert resp.json()["estado"] == "BAJA_PERMANENTE"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  resp = client.delete(f"{PREFIX}/101", headers=admin_headers)
88
  assert resp.status_code == 200
89
  assert resp.json()["estado"] == "BAJA_PERMANENTE"
90
+
91
+
92
+ # ─── Detalle ───────────────────────────────────────────────────────────────
93
+
94
+ def test_get_cama_ok(client: TestClient, medico_headers, monkeypatch):
95
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama())
96
+ resp = client.get(f"{PREFIX}/101", headers=medico_headers)
97
+ assert resp.status_code == 200
98
+ assert resp.json()["id"] == 101
99
+
100
+
101
+ def test_get_cama_404(client: TestClient, medico_headers, monkeypatch):
102
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: None)
103
+ resp = client.get(f"{PREFIX}/999", headers=medico_headers)
104
+ assert resp.status_code == 404
105
+
106
+
107
+ # ─── PATCH estado ──────────────────────────────────────────────────────────
108
+
109
+ def test_patch_cama_fuera_de_servicio_ok(client: TestClient, admin_headers, monkeypatch):
110
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
111
+ monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
112
+ resp = client.patch(f"{PREFIX}/101", json={"estado": "FUERA_DE_SERVICIO"}, headers=admin_headers)
113
+ assert resp.status_code == 200
114
+ assert resp.json()["estado"] == "FUERA_DE_SERVICIO"
115
+
116
+
117
+ def test_patch_cama_estado_automatico_409(client: TestClient, admin_headers, monkeypatch):
118
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
119
+ resp = client.patch(f"{PREFIX}/101", json={"estado": "OCUPADA"}, headers=admin_headers)
120
+ assert resp.status_code == 409
121
+
122
+
123
+ def test_patch_cama_limpieza_solo_puede_marcar_disponible(client: TestClient, limpieza_headers, monkeypatch):
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
tests/test_quirofanos.py CHANGED
@@ -170,6 +170,40 @@ def test_get_reserva_404(client: TestClient, medico_headers, monkeypatch):
170
 
171
  # ─── Disponibilidad ────────────────────────────────────────────────────────
172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  def test_disponibilidad_sin_fechas_422(client: TestClient, medico_headers):
174
  resp = client.get(f"{PREFIX}/disponibilidad", headers=medico_headers)
175
  assert resp.status_code == 422
 
170
 
171
  # ─── Disponibilidad ────────────────────────────────────────────────────────
172
 
173
+ def test_get_reservas_ok(client: TestClient, medico_headers, monkeypatch):
174
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
175
+ monkeypatch.setattr("app.crud.reserva.get_all", lambda *a, **k: [make_reserva()])
176
+ resp = client.get(f"{PREFIX}/501/reservas", headers=medico_headers)
177
+ assert resp.status_code == 200
178
+ data = resp.json()
179
+ assert data["total"] == 1
180
+
181
+
182
+ def test_get_reserva_ok(client: TestClient, medico_headers, monkeypatch):
183
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
184
+ monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: make_reserva())
185
+ resp = client.get(f"{PREFIX}/501/reservas/1", headers=medico_headers)
186
+ assert resp.status_code == 200
187
+ assert resp.json()["id"] == 1
188
+
189
+
190
+ def test_patch_reserva_estado_en_curso_ok(client: TestClient, admin_headers, monkeypatch):
191
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
192
+ monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: make_reserva(estado="RESERVADA"))
193
+ monkeypatch.setattr("app.crud.cama.save", lambda db, cama: cama)
194
+ monkeypatch.setattr("app.crud.reserva.save", lambda db, reserva: reserva)
195
+ resp = client.patch(f"{PREFIX}/501/reservas/1", json={"estado": "EN_CURSO"}, headers=admin_headers)
196
+ assert resp.status_code == 200
197
+ assert resp.json()["estado"] == "EN_CURSO"
198
+
199
+
200
+ def test_patch_reserva_transicion_invalida_409(client: TestClient, admin_headers, monkeypatch):
201
+ monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_quirofano())
202
+ monkeypatch.setattr("app.crud.reserva.get_by_cama_and_id", lambda db, cid, rid: make_reserva(estado="EN_CURSO"))
203
+ resp = client.patch(f"{PREFIX}/501/reservas/1", json={"estado": "RESERVADA"}, headers=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
tests/test_solicitudes.py CHANGED
@@ -75,6 +75,31 @@ def test_recibir_solicitud_webhook_ok(client: TestClient, admin_headers, monkeyp
75
  assert data["solicitud_id"] == "SOL-00001"
76
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  def test_recibir_solicitud_webhook_idempotente(client: TestClient, admin_headers, monkeypatch):
79
  """Reintento de M1 con el mismo solicitud_hce_id no crea una solicitud nueva."""
80
  existente = make_solicitud()
 
75
  assert data["solicitud_id"] == "SOL-00001"
76
 
77
 
78
+ def test_get_solicitud_ok(client: TestClient, medico_headers, monkeypatch):
79
+ monkeypatch.setattr("app.crud.solicitud.get_by_id", lambda db, num: make_solicitud())
80
+ resp = client.get(f"{PREFIX}/SOL-00001", headers=medico_headers)
81
+ assert resp.status_code == 200
82
+ data = resp.json()
83
+ assert data["id"] == "SOL-00001"
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)
90
+ monkeypatch.setattr("app.crud.solicitud.save", lambda db, s: s)
91
+ monkeypatch.setattr("app.integrations.hce.notify_resultado_solicitud", lambda *a, **k: None)
92
+ resp = client.patch(
93
+ f"{PREFIX}/SOL-00001",
94
+ json={"accion": "RECHAZAR", "motivo": "sin camas"},
95
+ headers=admin_headers,
96
+ )
97
+ assert resp.status_code == 200
98
+ data = resp.json()
99
+ assert data["estado"] == "RECHAZADA"
100
+ assert data["accion"] == "RECHAZAR"
101
+
102
+
103
  def test_recibir_solicitud_webhook_idempotente(client: TestClient, admin_headers, monkeypatch):
104
  """Reintento de M1 con el mismo solicitud_hce_id no crea una solicitud nueva."""
105
  existente = make_solicitud()
tests/test_webhooks.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests de los endpoints webhook entrantes (HCE, Monitoreo, Turnos).
3
+
4
+ Mockean la capa CRUD e integraciones externas para ejercitar routing +
5
+ reglas de negocio + serialización, sin DB real.
6
+ """
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"
14
+
15
+ _NOW_ISO = datetime.now(timezone.utc).isoformat()
16
+
17
+
18
+ # ─── Alta médica (M1 HCE) ──────────────────────────────────────────────────
19
+
20
+ def test_alta_medica_ok(client: TestClient, admin_headers, monkeypatch):
21
+ cama = make_cama(estado="OCUPADA", paciente_id=7)
22
+ monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama)
23
+ monkeypatch.setattr("app.crud.cama.save", lambda db, c: c)
24
+ monkeypatch.setattr("app.integrations.monitoreo.notify_baja", lambda *a, **k: None)
25
+ payload = {
26
+ "paciente_id": 7,
27
+ "medico_firmante_id": 10,
28
+ "tipo_alta": "ALTA_MEDICA",
29
+ "timestamp": _NOW_ISO,
30
+ }
31
+ resp = client.post(f"{WEBHOOKS_PREFIX}/hce/alta-medica", json=payload, headers=admin_headers)
32
+ assert resp.status_code == 200
33
+ data = resp.json()
34
+ assert "cama_id" in data
35
+
36
+
37
+ def test_alta_medica_404(client: TestClient, admin_headers, monkeypatch):
38
+ monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: None)
39
+ payload = {
40
+ "paciente_id": 7,
41
+ "medico_firmante_id": 10,
42
+ "tipo_alta": "ALTA_MEDICA",
43
+ "timestamp": _NOW_ISO,
44
+ }
45
+ resp = client.post(f"{WEBHOOKS_PREFIX}/hce/alta-medica", json=payload, headers=admin_headers)
46
+ assert resp.status_code == 404
47
+
48
+
49
+ # ─── Alerta emergencia (M9 Monitoreo) ─────────────────────────────────────
50
+
51
+ def test_alerta_emergencia_ok(client: TestClient, admin_headers, monkeypatch):
52
+ cama = make_cama(estado="OCUPADA", paciente_id=7)
53
+ monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama)
54
+ monkeypatch.setattr("app.crud.cama.save", lambda db, c: c)
55
+ monkeypatch.setattr("app.integrations.hce.notify_evento_episodio", lambda *a, **k: None)
56
+ payload = {"paciente_id": 7, "timestamp": _NOW_ISO}
57
+ resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-emergencia", json=payload, headers=admin_headers)
58
+ assert resp.status_code == 200
59
+ data = resp.json()
60
+ assert data["cama_id"] == 101
61
+
62
+
63
+ def test_alerta_emergencia_404(client: TestClient, admin_headers, monkeypatch):
64
+ monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: None)
65
+ payload = {"paciente_id": 7, "timestamp": _NOW_ISO}
66
+ resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-emergencia", json=payload, headers=admin_headers)
67
+ assert resp.status_code == 404
68
+
69
+
70
+ # ─── Alerta resuelta (M9 Monitoreo) ───────────────────────────────────────
71
+
72
+ def test_alerta_resuelta_ok(client: TestClient, admin_headers, monkeypatch):
73
+ cama = make_cama(estado="CODIGO_ROJO", paciente_id=7)
74
+ monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama)
75
+ monkeypatch.setattr("app.crud.cama.save", lambda db, c: c)
76
+ payload = {"paciente_id": 7, "timestamp": _NOW_ISO}
77
+ resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-resuelta", json=payload, headers=admin_headers)
78
+ assert resp.status_code == 200
79
+ data = resp.json()
80
+ assert data["cama_id"] == 101
81
+
82
+
83
+ def test_alerta_resuelta_404(client: TestClient, admin_headers, monkeypatch):
84
+ monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: None)
85
+ payload = {"paciente_id": 7, "timestamp": _NOW_ISO}
86
+ resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-resuelta", json=payload, headers=admin_headers)
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):
93
+ monkeypatch.setattr("app.crud.reserva.get_by_urgencia_hce_id", lambda db, hce_id: None)
94
+ monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: [])
95
+ payload = {
96
+ "solicitud_urgencia_hce_id": "URG-001",
97
+ "paciente_id": 7,
98
+ "medico_cirujano_id": 22,
99
+ "medico_solicitante_id": 10,
100
+ "fecha_hora_inicio": "2025-01-18T10:00:00Z",
101
+ "fecha_hora_fin_estimada": "2025-01-18T12:00:00Z",
102
+ "timestamp": _NOW_ISO,
103
+ }
104
+ resp = client.post(f"{WEBHOOKS_PREFIX}/hce/cirugia-urgente", json=payload, headers=admin_headers)
105
+ assert resp.status_code == 409
106
+
107
+
108
+ # ─── Cancelación de reserva de quirófano (M2 Turnos) ─────────────────────
109
+
110
+ def test_cancelar_reserva_quirofano_ok(client: TestClient, admin_headers, monkeypatch):
111
+ reserva = make_reserva()
112
+ monkeypatch.setattr("app.crud.reserva.get_by_turno", lambda db, turno_id: reserva)
113
+ monkeypatch.setattr("app.crud.reserva.save", lambda db, r: r)
114
+ payload = {"motivo": "paciente cancela", "timestamp": _NOW_ISO}
115
+ resp = client.post(f"{TURNOS_PREFIX}/TURNO-001/cancelacion", json=payload, headers=admin_headers)
116
+ assert resp.status_code == 200
117
+ data = resp.json()
118
+ assert data["reserva_id"] == 1
119
+ assert data["estado"] == "CANCELADA"
120
+
121
+
122
+ def test_cancelar_reserva_quirofano_404(client: TestClient, admin_headers, monkeypatch):
123
+ monkeypatch.setattr("app.crud.reserva.get_by_turno", lambda db, turno_id: None)
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