66 lines
2.4 KiB
Python
66 lines
2.4 KiB
Python
"""Tests für src.routes.health — Health-Check mit Readiness."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from src.app import create_app
|
|
|
|
|
|
class TestHealthStatus:
|
|
"""Tests für /health Endpoint."""
|
|
|
|
def setup_method(self) -> None:
|
|
"""Jeder Test bekommt eine frische App mit gemocktem Redis."""
|
|
with patch("src.queue.handler.redis.Redis"):
|
|
self.app = create_app()
|
|
self.client = self.app.test_client()
|
|
|
|
def test_health_status_ok(self) -> None:
|
|
"""GET /health → 200 mit Status-JSON."""
|
|
self.app.queue_handler = MagicMock(queue_size=MagicMock(return_value=42)) # type: ignore[assignment]
|
|
response = self.client.get("/health")
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data["status"] == "ok"
|
|
assert "queue_size" in data
|
|
assert "python_version" in data
|
|
assert "platform" in data
|
|
|
|
def test_health_queue_size_integer(self) -> None:
|
|
"""queue_size ist ein Integer."""
|
|
self.app.queue_handler = MagicMock(queue_size=MagicMock(return_value=7)) # type: ignore[assignment]
|
|
response = self.client.get("/health")
|
|
data = response.get_json()
|
|
assert isinstance(data["queue_size"], int)
|
|
assert data["queue_size"] == 7
|
|
|
|
|
|
class TestHealthReady:
|
|
"""Tests für /health/ready Endpoint."""
|
|
|
|
def setup_method(self) -> None:
|
|
"""Jeder Test bekommt eine frische App."""
|
|
with patch("src.queue.handler.redis.Redis"):
|
|
self.app = create_app()
|
|
self.client = self.app.test_client()
|
|
|
|
def test_health_ready_redis_alive(self) -> None:
|
|
"""Redis ping erfolgreich → ready=true."""
|
|
response = self.client.get("/health/ready")
|
|
# Redis wird gemockt, ping sollte successful sein
|
|
assert response.status_code == 200
|
|
data = response.get_json()
|
|
assert data["ready"] is True
|
|
|
|
def test_health_ready_redis_fails(self) -> None:
|
|
"""Redis ping fehlschlägt → ready=false, 503."""
|
|
with patch.object(
|
|
self.app.queue_handler.redis, "ping", side_effect=Exception("Redis down")
|
|
):
|
|
response = self.client.get("/health/ready")
|
|
assert response.status_code == 503
|
|
data = response.get_json()
|
|
assert data["ready"] is False
|
|
assert "reason" in data
|