"""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_docs_link(self, client): """Root endpoint should return docs link.""" response = client.get("/") data = response.json() assert "docs" in data assert data["docs"] == "/docs" class TestHealthEndpoint: """Test /health endpoint.""" def test_health_returns_200(self, client): """Health endpoint should return 200.""" response = client.get("/health") assert response.status_code == 200 def test_health_returns_status(self, client): """Health endpoint should return status information.""" response = client.get("/health") data = response.json() assert "status" in data assert "version" in data assert data["status"] == "healthy" 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 class TestFullHealthCheck: """Test /health/full endpoint.""" @patch("src.shared.database.Database.health_check") def test_full_health_returns_200_when_healthy(self, mock_db_health, client): """Full health should return 200 when database is healthy.""" mock_db_health.return_value = True response = client.get("/health/full") assert response.status_code == 200 @patch("src.shared.database.Database.health_check") def test_full_health_returns_503_when_unhealthy(self, mock_db_health, client): """Full health should return 503 when database is unhealthy.""" mock_db_health.return_value = False response = client.get("/health/full") assert response.status_code == 503 @patch("src.shared.database.Database.health_check") def test_full_health_returns_components_status(self, mock_db_health, client): """Full health should return component status.""" mock_db_health.return_value = True response = client.get("/health/full") data = response.json() assert "status" in data assert "components" in data assert "database" in data["components"] assert "response_time_ms" in data @patch("src.shared.database.Database.health_check") def test_full_health_handles_database_error(self, mock_db_health, client): """Full health should handle database errors gracefully.""" mock_db_health.side_effect = Exception("Connection error") response = client.get("/health/full") data = response.json() assert response.status_code == 503 assert data["status"] == "unhealthy" assert "error" in data["components"]["database"] class TestDiagnosticsEndpoint: """Test /health/diagnostics endpoint.""" def test_diagnostics_returns_200(self, client): """Diagnostics should return 200.""" response = client.get("/health/diagnostics") assert response.status_code == 200 def test_diagnostics_returns_service_info(self, client): """Diagnostics should return service information.""" response = client.get("/health/diagnostics") data = response.json() assert "service" in data assert "name" in data["service"] assert "version" in data["service"] def test_diagnostics_returns_configuration(self, client): """Diagnostics should return configuration info.""" response = client.get("/health/diagnostics") data = response.json() assert "configuration" in data def test_diagnostics_returns_response_time(self, client): """Diagnostics should return response time.""" response = client.get("/health/diagnostics") data = response.json() assert "response_time_ms" in data assert isinstance(data["response_time_ms"], int)