fix: Clean up and standardize test code formatting
Some checks failed
LabFusion CI/CD Pipeline / service-adapters (push) Failing after 20s
Docker Build and Push / build-and-push (push) Failing after 37s
Integration Tests / integration-tests (push) Failing after 32s
Integration Tests / performance-tests (push) Has been skipped
Service Adapters (Python FastAPI) / test (3.1) (push) Failing after 15s
LabFusion CI/CD Pipeline / api-gateway (push) Failing after 1m18s
Service Adapters (Python FastAPI) / test (3.11) (push) Failing after 22s
Service Adapters (Python FastAPI) / test (3.12) (push) Failing after 23s
Service Adapters (Python FastAPI) / test (3.9) (push) Failing after 21s
LabFusion CI/CD Pipeline / frontend (push) Failing after 2m0s
Service Adapters (Python FastAPI) / build (push) Has been skipped
LabFusion CI/CD Pipeline / api-docs (push) Successful in 1m53s
LabFusion CI/CD Pipeline / integration-tests (push) Has been skipped
Some checks failed
LabFusion CI/CD Pipeline / service-adapters (push) Failing after 20s
Docker Build and Push / build-and-push (push) Failing after 37s
Integration Tests / integration-tests (push) Failing after 32s
Integration Tests / performance-tests (push) Has been skipped
Service Adapters (Python FastAPI) / test (3.1) (push) Failing after 15s
LabFusion CI/CD Pipeline / api-gateway (push) Failing after 1m18s
Service Adapters (Python FastAPI) / test (3.11) (push) Failing after 22s
Service Adapters (Python FastAPI) / test (3.12) (push) Failing after 23s
Service Adapters (Python FastAPI) / test (3.9) (push) Failing after 21s
LabFusion CI/CD Pipeline / frontend (push) Failing after 2m0s
Service Adapters (Python FastAPI) / build (push) Has been skipped
LabFusion CI/CD Pipeline / api-docs (push) Successful in 1m53s
LabFusion CI/CD Pipeline / integration-tests (push) Has been skipped
### Summary of Changes - Removed unnecessary blank lines and standardized import statements across test files. - Ensured consistent use of quotes in patch decorators and improved formatting of test data structures. - Enhanced readability and maintainability of test code by applying clean code principles. ### Expected Results - Improved clarity and consistency in test code, facilitating easier understanding and future modifications.
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
"""
|
||||
Tests for Home Assistant routes
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
from unittest.mock import patch, AsyncMock
|
||||
|
||||
from main import app
|
||||
|
||||
@@ -13,7 +14,7 @@ client = TestClient(app)
|
||||
class TestHomeAssistantRoutes:
|
||||
"""Test Home Assistant API routes"""
|
||||
|
||||
@patch('routes.home_assistant.httpx.AsyncClient')
|
||||
@patch("routes.home_assistant.httpx.AsyncClient")
|
||||
async def test_get_entities_success(self, mock_client_class):
|
||||
"""Test successful retrieval of Home Assistant entities"""
|
||||
# Mock the HTTP client response
|
||||
@@ -24,57 +25,57 @@ class TestHomeAssistantRoutes:
|
||||
"state": "22.5",
|
||||
"attributes": {
|
||||
"unit_of_measurement": "°C",
|
||||
"friendly_name": "Temperature"
|
||||
}
|
||||
"friendly_name": "Temperature",
|
||||
},
|
||||
},
|
||||
"light.living_room": {
|
||||
"entity_id": "light.living_room",
|
||||
"state": "on",
|
||||
"attributes": {
|
||||
"friendly_name": "Living Room Light"
|
||||
}
|
||||
}
|
||||
"attributes": {"friendly_name": "Living Room Light"},
|
||||
},
|
||||
}
|
||||
mock_response.status_code = 200
|
||||
|
||||
|
||||
mock_client = AsyncMock()
|
||||
mock_client.get.return_value = mock_response
|
||||
mock_client_class.return_value.__aenter__.return_value = mock_client
|
||||
|
||||
|
||||
response = client.get("/home-assistant/entities")
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
data = response.json()
|
||||
assert "entities" in data
|
||||
assert len(data["entities"]) == 2
|
||||
|
||||
|
||||
# Check first entity
|
||||
temp_entity = data["entities"][0]
|
||||
assert temp_entity["entity_id"] == "sensor.temperature"
|
||||
assert temp_entity["state"] == "22.5"
|
||||
assert temp_entity["attributes"]["unit_of_measurement"] == "°C"
|
||||
|
||||
@patch('routes.home_assistant.httpx.AsyncClient')
|
||||
@patch("routes.home_assistant.httpx.AsyncClient")
|
||||
async def test_get_entities_api_error(self, mock_client_class):
|
||||
"""Test handling of Home Assistant API errors"""
|
||||
# Mock HTTP error response
|
||||
mock_response = AsyncMock()
|
||||
mock_response.status_code = 500
|
||||
mock_response.text = "Internal Server Error"
|
||||
|
||||
|
||||
mock_client = AsyncMock()
|
||||
mock_client.get.return_value = mock_response
|
||||
mock_client_class.return_value.__aenter__.return_value = mock_client
|
||||
|
||||
|
||||
response = client.get("/home-assistant/entities")
|
||||
assert response.status_code == 500
|
||||
|
||||
@patch('routes.home_assistant.httpx.AsyncClient')
|
||||
@patch("routes.home_assistant.httpx.AsyncClient")
|
||||
async def test_get_entities_connection_error(self, mock_client_class):
|
||||
"""Test handling of connection errors"""
|
||||
# Mock connection error
|
||||
mock_client_class.return_value.__aenter__.side_effect = Exception("Connection failed")
|
||||
|
||||
mock_client_class.return_value.__aenter__.side_effect = Exception(
|
||||
"Connection failed"
|
||||
)
|
||||
|
||||
response = client.get("/home-assistant/entities")
|
||||
assert response.status_code == 500
|
||||
|
||||
|
||||
Reference in New Issue
Block a user