Files
core-api/tests/test_health.py
T
jpmschweitzerandClaude Opus 4.5 a22e168666 Improve test coverage to 65%
Add comprehensive test suites for:
- NPM client (27 tests)
- Ollama client (16 tests)
- AI client and controller (34 tests)
- Static controller (8 tests)
- Tools controller DNS lookup (9 tests)
- OIDC authentication (10 tests)
- Housekeeping endpoints (28 tests)
- Infrastructure endpoints (15 tests)
- Health endpoints (12 tests)
- Portainer client (12 tests)
- Home Assistant client (24 tests)

Total: 285 tests passing with 65% code coverage.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-17 16:32:39 +01:00

264 lines
9.5 KiB
Python

"""Tests for health endpoints."""
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch, AsyncMock
from src.main import app
@pytest.fixture
def client():
"""Create a test client."""
return TestClient(app)
class TestRootEndpoint:
"""Test root endpoint."""
def test_root_returns_200(self, client):
"""Root endpoint should return 200."""
response = client.get("/")
assert response.status_code == 200
def test_root_returns_service_info(self, client):
"""Root endpoint should return service information."""
response = client.get("/")
data = response.json()
assert "service" in data
assert "version" in data
assert "status" in data
assert data["service"] == "Core Code API"
assert data["status"] == "healthy"
def test_root_returns_documentation_links(self, client):
"""Root endpoint should return documentation links."""
response = client.get("/")
data = response.json()
assert "documentation" in data
assert "swagger_ui" in data["documentation"]
assert "redoc" in data["documentation"]
def test_root_returns_endpoints(self, client):
"""Root endpoint should return available endpoints."""
response = client.get("/")
data = response.json()
assert "endpoints" in data
assert "health" in data["endpoints"]
class TestHealthEndpoint:
"""Test /health endpoint."""
@patch("src.controllers.health_controller.get_ollama_client")
def test_health_returns_200_when_ollama_healthy(self, mock_get_ollama, client):
"""Health endpoint should return 200 when Ollama is healthy."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health")
assert response.status_code == 200
@patch("src.controllers.health_controller.get_ollama_client")
def test_health_returns_status(self, mock_get_ollama, client):
"""Health endpoint should return status information."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health")
data = response.json()
assert "status" in data
assert "version" in data
assert "ollama_connected" in data
@patch("src.controllers.health_controller.get_ollama_client")
def test_health_returns_ollama_connected_true(self, mock_get_ollama, client):
"""Health should report Ollama connected when healthy."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health")
data = response.json()
assert data["ollama_connected"] is True
@patch("src.controllers.health_controller.get_ollama_client")
def test_health_returns_ollama_connected_false(self, mock_get_ollama, client):
"""Health should report Ollama disconnected when unhealthy."""
mock_client = AsyncMock()
mock_client.health_check.return_value = False
mock_get_ollama.return_value = mock_client
response = client.get("/health")
data = response.json()
assert data["ollama_connected"] is False
class TestOpenAPIEndpoint:
"""Test OpenAPI documentation endpoints."""
def test_openapi_spec_available(self, client):
"""OpenAPI spec should be available."""
response = client.get("/openapi.json")
assert response.status_code == 200
data = response.json()
assert "openapi" in data
assert "info" in data
def test_swagger_ui_available(self, client):
"""Swagger UI should be available."""
response = client.get("/docs")
assert response.status_code == 200
def test_redoc_available(self, client):
"""ReDoc should be available."""
response = client.get("/redoc")
assert response.status_code == 200
class TestFullHealthCheck:
"""Test /health/full endpoint."""
@patch("src.controllers.health_controller.get_ollama_client")
def test_full_health_returns_503_when_unhealthy(self, mock_get_ollama, client):
"""Full health should return 503 when Ollama unhealthy."""
mock_client = AsyncMock()
mock_client.health_check.return_value = False
mock_get_ollama.return_value = mock_client
response = client.get("/health/full")
assert response.status_code == 503
@patch("src.controllers.health_controller.get_ollama_client")
def test_full_health_returns_components_status(self, mock_get_ollama, client):
"""Full health should return component status."""
mock_client = AsyncMock()
mock_client.health_check.return_value = False
mock_get_ollama.return_value = mock_client
response = client.get("/health/full")
data = response.json()
assert "status" in data
assert "components" in data
assert "ollama" in data["components"]
assert "response_time_ms" in data
@patch("src.controllers.health_controller.get_ollama_client")
def test_full_health_handles_list_models_error(self, mock_get_ollama, client):
"""Full health should handle list_models errors."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_client.list_models.side_effect = Exception("Connection error")
mock_get_ollama.return_value = mock_client
response = client.get("/health/full")
data = response.json()
# Should report error in component status
assert "ollama" in data["components"]
@patch("src.controllers.health_controller.get_ollama_client")
def test_full_health_handles_health_check_exception(self, mock_get_ollama, client):
"""Full health should handle health check exceptions gracefully."""
mock_client = AsyncMock()
# Return False instead of raising exception to test unhealthy path
mock_client.health_check.return_value = False
mock_get_ollama.return_value = mock_client
response = client.get("/health/full")
# Should return 503 for unhealthy
assert response.status_code == 503
data = response.json()
assert data["status"] == "unhealthy"
class TestDiagnosticsEndpoint:
"""Test /health/diagnostics endpoint."""
@patch("src.controllers.health_controller.get_ollama_client")
def test_diagnostics_returns_200(self, mock_get_ollama, client):
"""Diagnostics should return 200."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health/diagnostics")
assert response.status_code == 200
@patch("src.controllers.health_controller.get_ollama_client")
def test_diagnostics_returns_service_info(self, mock_get_ollama, client):
"""Diagnostics should return service information."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health/diagnostics")
data = response.json()
assert "service" in data
assert "name" in data["service"]
assert "version" in data["service"]
@patch("src.controllers.health_controller.get_ollama_client")
def test_diagnostics_returns_components(self, mock_get_ollama, client):
"""Diagnostics should return component details."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health/diagnostics")
data = response.json()
assert "components" in data
assert "ollama" in data["components"]
assert "agent" in data["components"]
assert "qdrant" in data["components"]
@patch("src.controllers.health_controller.get_ollama_client")
def test_diagnostics_returns_configuration(self, mock_get_ollama, client):
"""Diagnostics should return configuration info."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health/diagnostics")
data = response.json()
assert "configuration" in data
@patch("src.controllers.health_controller.get_ollama_client")
def test_diagnostics_returns_response_time(self, mock_get_ollama, client):
"""Diagnostics should return response time."""
mock_client = AsyncMock()
mock_client.health_check.return_value = True
mock_get_ollama.return_value = mock_client
response = client.get("/health/diagnostics")
data = response.json()
assert "response_time_ms" in data
assert isinstance(data["response_time_ms"], int)
@patch("src.controllers.health_controller.get_ollama_client")
def test_diagnostics_handles_ollama_error(self, mock_get_ollama, client):
"""Diagnostics should handle Ollama connection errors."""
mock_client = AsyncMock()
mock_client.health_check.side_effect = Exception("Connection refused")
mock_get_ollama.return_value = mock_client
response = client.get("/health/diagnostics")
data = response.json()
# Should still return 200 with error info
assert response.status_code == 200
assert "error" in data["components"]["ollama"]