Files
labFusion/services/service-adapters/tests/test_general_routes.py
GSRN 5906b37f5b
Some checks failed
Integration Tests / integration-tests (push) Failing after 20s
Integration Tests / performance-tests (push) Has been skipped
Service Adapters (Python FastAPI) / test (3.11) (push) Failing after 49s
Service Adapters (Python FastAPI) / test (3.13) (push) Failing after 53s
Service Adapters (Python FastAPI) / test (3.12) (push) Failing after 55s
Service Adapters (Python FastAPI) / build (push) Has been skipped
test: Simplify mock responses in service health check tests
### Summary of Changes
- Consolidated mock response definitions in the `test_get_services` method for improved readability.
- Enhanced maintainability of the test suite by streamlining the structure of service status mock data.

### Expected Results
- Improved clarity in test definitions, making it easier to understand the expected service responses.
- Facilitated future updates to the test suite by reducing complexity in mock data setup.
2025-09-18 13:43:15 +02:00

91 lines
3.1 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