Files
labFusion/services/service-adapters/tests/test_general_routes.py
GSRN 7fa17624b5
Some checks failed
Integration Tests / integration-tests (push) Failing after 20s
Service Adapters (Python FastAPI) / test (3.11) (push) Failing after 20s
Integration Tests / performance-tests (push) Has been skipped
Service Adapters (Python FastAPI) / test (3.12) (push) Failing after 21s
Service Adapters (Python FastAPI) / test (3.13) (push) Failing after 22s
Service Adapters (Python FastAPI) / build (push) Has been skipped
test: Update service health check tests to include mock responses
### Summary of Changes
- Modified the `test_get_services` method to patch the health check function and include mock responses for service statuses.
- Enhanced test coverage by simulating various service states, improving the reliability of the tests for the services endpoint.

### Expected Results
- Improved accuracy of service health checks in tests, ensuring that the endpoint behaves correctly under different service conditions.
- Enhanced maintainability of the test suite by clearly defining expected service responses.
2025-09-18 13:18:32 +02:00

109 lines
3.3 KiB
Python

"""
Tests for general API routes
"""
from unittest.mock import patch
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
class TestGeneralRoutes:
"""Test general API routes"""
def test_root_endpoint(self):
"""Test the root endpoint"""
response = client.get("/")
assert response.status_code == 200
data = response.json()
assert data["message"] == "LabFusion Service Adapters API"
assert data["version"] == "1.0.0"
def test_health_check(self):
"""Test the health check endpoint"""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
assert "timestamp" in data
# Verify timestamp is in ISO format
assert "T" in data["timestamp"] or "Z" in data["timestamp"]
@patch("routes.general.SERVICES")
@patch("routes.general.status_checker.check_all_services")
def test_get_services(self, mock_check_services, mock_services):
"""Test the get services endpoint"""
# Mock the services configuration
mock_services.items.return_value = [
(
"home_assistant",
{"enabled": True, "url": "http://homeassistant.local:8123"},
),
("frigate", {"enabled": True, "url": "http://frigate.local:5000"}),
("immich", {"enabled": False, "url": "http://immich.local:2283"}),
]
# Mock the health check results
mock_check_services.return_value = {
"home_assistant": {
"status": "unknown",
"response_time": None,
"error": None,
"uptime": None,
"metadata": {}
},
"frigate": {
"status": "unknown",
"response_time": None,
"error": None,
"uptime": None,
"metadata": {}
},
"immich": {
"status": "disabled",
"response_time": None,
"error": None,
"uptime": None,
"metadata": {}
}
}
response = client.get("/services")
assert response.status_code == 200
data = response.json()
assert "home_assistant" in data
assert "frigate" in data
assert "immich" in data
# Check service status structure
ha_service = data["home_assistant"]
assert ha_service["enabled"] is True
assert ha_service["url"] == "http://homeassistant.local:8123"
assert ha_service["status"] == "unknown"
def test_health_check_response_model(self):
"""Test that health check returns proper response model"""
response = client.get("/health")
data = response.json()
# Verify all required fields are present
required_fields = ["status", "timestamp"]
for field in required_fields:
assert field in data
def test_root_response_model(self):
"""Test that root endpoint returns proper response model"""
response = client.get("/")
data = response.json()
# Verify all required fields are present
required_fields = ["message", "version"]
for field in required_fields:
assert field in data