62 lines
6.5 KiB
Python
62 lines
6.5 KiB
Python
"""Tests for health check endpoints."""
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from unittest.mock import patch
|
|
|
|
from main import create_app
|
|
|
|
|
|
class TestHealthCheck:
|
|
"""Test cases for health check endpoint."""
|
|
|
|
@pytest.fixture
|
|
def client(self) -> TestClient:
|
|
"""Create test client."""
|
|
app = create_app()
|
|
return TestClient(app)
|
|
|
|
@patch('app.database.DatabaseService.health_check')
|
|
def test_health_check_database_connected(self, mock_db_health, client) -> None:
|
|
"""Test health check when database is connected."""
|
|
# Mock database as healthy
|
|
mock_db_health.return_value = True
|
|
|
|
response = client.get("/health-check")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "healthy"
|
|
assert data["service"] == "loapi"
|
|
assert data["database"] == "connected"
|
|
mock_db_health.assert_called_once()
|
|
|
|
@patch('app.database.DatabaseService.health_check')
|
|
def test_health_check_database_disconnected(self, mock_db_health, client):
|
|
"""Test health check when database is disconnected."""
|
|
# Mock database as unhealthy
|
|
mock_db_health.return_value = False
|
|
|
|
response = client.get("/health-check")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "unhealthy"
|
|
assert data["service"] == "loapi"
|
|
assert data["database"] == "disconnected"
|
|
mock_db_health.assert_called_once()
|
|
|
|
@patch('app.database.DatabaseService.health_check')
|
|
def test_health_check_database_exception(self, mock_db_health, client):
|
|
"""Test health check when database check raises exception."""
|
|
# Mock database health check to raise exception
|
|
mock_db_health.side_effect = Exception("Database connection failed")
|
|
|
|
response = client.get("/health-check")
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "unhealthy"
|
|
assert data["service"] == "loapi"
|
|
assert data["database"] == "disconnected"
|
|
mock_db_health.assert_called_once()
|