Mateo Ferreyra
Alta de cama: cierre en Facturacion (M7) y baja de Monitoreo (M9) antes del cambio de estado
7f24904 Download tests/test_webhooks.py from Modulo-6/api: direct link, hf CLI and curl.
- Browser
- Download file 9.95 kB
-
https://huggingface.co/spaces/Modulo-6/api/resolve/2ffb0abbe60bfb58dccf318a13db7a91f2b58052/tests/test_webhooks.py
- Command line
-
hf download hf://spaces/Modulo-6/api@2ffb0abbe60bfb58dccf318a13db7a91f2b58052/tests/test_webhooks.py
-
curl -L -o test_webhooks.py https://huggingface.co/spaces/Modulo-6/api/resolve/2ffb0abbe60bfb58dccf318a13db7a91f2b58052/tests/test_webhooks.py
9.95 kB
| """ | |
| Tests de los endpoints webhook entrantes (HCE, Monitoreo, Turnos). | |
| Mockean la capa CRUD e integraciones externas para ejercitar routing + | |
| reglas de negocio + serialización, sin DB real. | |
| """ | |
| from datetime import datetime, timezone | |
| from fastapi.testclient import TestClient | |
| from tests.factories import make_cama, make_quirofano, make_reserva | |
| WEBHOOKS_PREFIX = "/api/v1/webhooks" | |
| TURNOS_PREFIX = "/api/v1/turnos" | |
| _NOW_ISO = datetime.now(timezone.utc).isoformat() | |
| # ─── Alta médica (M1 HCE) ────────────────────────────────────────────────── | |
| def test_alta_medica_ok(client: TestClient, admin_headers, monkeypatch): | |
| cama = make_cama(estado="OCUPADA", paciente_id=7) | |
| monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, c: c) | |
| monkeypatch.setattr("app.integrations.monitoreo.notify_baja", lambda *a, **k: True) | |
| payload = { | |
| "paciente_id": 7, | |
| "medico_firmante_id": 10, | |
| "tipo_alta": "ALTA_MEDICA", | |
| "timestamp": _NOW_ISO, | |
| } | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/hce/alta-medica", json=payload, headers=admin_headers) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert "cama_id" in data | |
| def test_alta_medica_404(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: None) | |
| payload = { | |
| "paciente_id": 7, | |
| "medico_firmante_id": 10, | |
| "tipo_alta": "ALTA_MEDICA", | |
| "timestamp": _NOW_ISO, | |
| } | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/hce/alta-medica", json=payload, headers=admin_headers) | |
| assert resp.status_code == 404 | |
| # ─── Alerta emergencia (M9 Monitoreo) ───────────────────────────────────── | |
| def test_alerta_emergencia_ok(client: TestClient, admin_headers, monkeypatch): | |
| cama = make_cama(estado="OCUPADA", paciente_id=7) | |
| monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, c: c) | |
| monkeypatch.setattr("app.integrations.hce.notify_evento_episodio", lambda *a, **k: None) | |
| payload = {"paciente_id": 7, "timestamp": _NOW_ISO} | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-emergencia", json=payload, headers=admin_headers) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["cama_id"] == 101 | |
| def test_alerta_emergencia_404(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: None) | |
| payload = {"paciente_id": 7, "timestamp": _NOW_ISO} | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-emergencia", json=payload, headers=admin_headers) | |
| assert resp.status_code == 404 | |
| # ─── Alerta resuelta (M9 Monitoreo) ─────────────────────────────────────── | |
| def test_alerta_resuelta_ok(client: TestClient, admin_headers, monkeypatch): | |
| cama = make_cama(estado="CODIGO_ROJO", paciente_id=7) | |
| monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama) | |
| monkeypatch.setattr("app.crud.cama.save", lambda db, c: c) | |
| payload = {"paciente_id": 7, "timestamp": _NOW_ISO} | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-resuelta", json=payload, headers=admin_headers) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["cama_id"] == 101 | |
| def test_alerta_resuelta_404(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: None) | |
| payload = {"paciente_id": 7, "timestamp": _NOW_ISO} | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/monitoreo/alerta-resuelta", json=payload, headers=admin_headers) | |
| assert resp.status_code == 404 | |
| # ─── Cirugía urgente (M1 HCE) ──────────────────────────────────────────── | |
| def test_cirugia_urgente_ok_201(client: TestClient, admin_headers, monkeypatch): | |
| reserva_urgente = make_reserva(origen="EMERGENCIA", prioridad="URGENTE") | |
| monkeypatch.setattr("app.crud.reserva.get_by_urgencia_hce_id", lambda db, hce_id: None) | |
| monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: [make_quirofano()]) | |
| monkeypatch.setattr("app.crud.reserva.get_superpuestas", lambda *a, **k: []) | |
| monkeypatch.setattr("app.crud.reserva.create_urgente", lambda db, cama_id, data: reserva_urgente) | |
| payload = { | |
| "solicitud_urgencia_hce_id": "URG-001", | |
| "paciente_id": 7, | |
| "medico_cirujano_id": 22, | |
| "medico_solicitante_id": 10, | |
| "fecha_hora_inicio": "2025-01-18T10:00:00Z", | |
| "fecha_hora_fin_estimada": "2025-01-18T12:00:00Z", | |
| "timestamp": _NOW_ISO, | |
| } | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/hce/cirugia-urgente", json=payload, headers=admin_headers) | |
| assert resp.status_code == 201 | |
| data = resp.json() | |
| assert data["reserva"]["estado"] == "RESERVADA" | |
| assert data["reserva_desplazada"] is None | |
| # ─── Cirugía urgente sin quirófanos disponibles (M1 HCE) ───────────────── | |
| def test_cirugia_urgente_sin_quirofanos_409(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.reserva.get_by_urgencia_hce_id", lambda db, hce_id: None) | |
| monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: []) | |
| payload = { | |
| "solicitud_urgencia_hce_id": "URG-001", | |
| "paciente_id": 7, | |
| "medico_cirujano_id": 22, | |
| "medico_solicitante_id": 10, | |
| "fecha_hora_inicio": "2025-01-18T10:00:00Z", | |
| "fecha_hora_fin_estimada": "2025-01-18T12:00:00Z", | |
| "timestamp": _NOW_ISO, | |
| } | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/hce/cirugia-urgente", json=payload, headers=admin_headers) | |
| assert resp.status_code == 409 | |
| def test_cirugia_urgente_desplaza_y_reprograma(client: TestClient, admin_headers, monkeypatch): | |
| """Si no hay quirófano libre, la programada de menor prioridad se REPROGRAMA en M2 | |
| (no se cancela): se la mueve a continuación de la urgente y se llama reschedule_turno.""" | |
| programada = make_reserva( | |
| id=3, turno_id=5501, origen="PROGRAMADA", prioridad="MEDIA", | |
| fecha_hora_inicio=datetime(2025, 1, 18, 10, 0, tzinfo=timezone.utc), | |
| fecha_hora_fin_estimada=datetime(2025, 1, 18, 12, 0, tzinfo=timezone.utc), | |
| ) | |
| urgente = make_reserva(id=9, origen="EMERGENCIA", prioridad="URGENTE") | |
| reschedule_calls = [] | |
| monkeypatch.setattr("app.crud.reserva.get_by_urgencia_hce_id", lambda db, hce_id: None) | |
| monkeypatch.setattr("app.crud.cama.get_quirofanos", lambda *a, **k: [make_quirofano()]) | |
| monkeypatch.setattr("app.crud.reserva.get_superpuestas", lambda *a, **k: [programada]) | |
| monkeypatch.setattr("app.crud.reserva.create_urgente", lambda db, cama_id, data: urgente) | |
| monkeypatch.setattr("app.crud.reserva.save", lambda db, r: r) | |
| monkeypatch.setattr( | |
| "app.integrations.turnos.reschedule_turno", | |
| lambda turno_id, starts_at, ends_at: reschedule_calls.append((turno_id, starts_at, ends_at)) or True, | |
| ) | |
| payload = { | |
| "solicitud_urgencia_hce_id": "URG-002", | |
| "paciente_id": 7, | |
| "medico_cirujano_id": 22, | |
| "medico_solicitante_id": 10, | |
| "fecha_hora_inicio": "2025-01-18T10:00:00Z", | |
| "fecha_hora_fin_estimada": "2025-01-18T11:00:00Z", | |
| "timestamp": _NOW_ISO, | |
| } | |
| resp = client.post(f"{WEBHOOKS_PREFIX}/hce/cirugia-urgente", json=payload, headers=admin_headers) | |
| assert resp.status_code == 201 | |
| data = resp.json() | |
| assert data["reserva_desplazada"]["turno_id"] == 5501 | |
| # Se reprogramó (no se canceló): la programada conserva estado RESERVADA. | |
| assert programada.estado == "RESERVADA" | |
| # reschedule_turno fue llamado con el turno desplazado, arrancando al terminar la urgente. | |
| assert len(reschedule_calls) == 1 | |
| turno_id, starts_at, ends_at = reschedule_calls[0] | |
| assert turno_id == 5501 | |
| assert starts_at == datetime(2025, 1, 18, 11, 0, tzinfo=timezone.utc) | |
| # Duración original preservada (2h). | |
| assert ends_at == datetime(2025, 1, 18, 13, 0, tzinfo=timezone.utc) | |
| # ─── Cancelación de reserva de quirófano (M2 Turnos) ───────────────────── | |
| def test_cancelar_reserva_quirofano_ok(client: TestClient, admin_headers, monkeypatch): | |
| reserva = make_reserva() | |
| monkeypatch.setattr("app.crud.reserva.get_by_turno", lambda db, turno_id: reserva) | |
| monkeypatch.setattr("app.crud.reserva.save", lambda db, r: r) | |
| payload = {"motivo": "paciente cancela", "timestamp": _NOW_ISO} | |
| resp = client.post(f"{TURNOS_PREFIX}/5501/cancelacion", json=payload, headers=admin_headers) | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["reserva_id"] == 1 | |
| assert data["estado"] == "CANCELADA" | |
| def test_cancelar_reserva_quirofano_404(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.reserva.get_by_turno", lambda db, turno_id: None) | |
| payload = {"motivo": "paciente cancela", "timestamp": _NOW_ISO} | |
| resp = client.post(f"{TURNOS_PREFIX}/9999/cancelacion", json=payload, headers=admin_headers) | |
| assert resp.status_code == 404 | |
| def test_cancelar_reserva_en_curso_409(client: TestClient, admin_headers, monkeypatch): | |
| monkeypatch.setattr("app.crud.reserva.get_by_turno", lambda db, turno_id: make_reserva(estado="EN_CURSO")) | |
| payload = {"motivo": "intento cancelar", "timestamp": _NOW_ISO} | |
| resp = client.post(f"{TURNOS_PREFIX}/5501/cancelacion", json=payload, headers=admin_headers) | |
| assert resp.status_code == 409 | |