refactor: Refactoring most of the project
Some checks failed
🧪 Integration Testing / 🔧 Test Integration (2025.9.4, 3.13) (push) Failing after 24s
Some checks failed
🧪 Integration Testing / 🔧 Test Integration (2025.9.4, 3.13) (push) Failing after 24s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Test API functionality."""
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from custom_components.adguard_hub.api import AdGuardHomeAPI
|
||||
|
||||
|
||||
@@ -13,10 +13,19 @@ def mock_session():
|
||||
response.json = AsyncMock(return_value={"status": "ok"})
|
||||
response.status = 200
|
||||
response.content_length = 100
|
||||
session.request = AsyncMock(return_value=response)
|
||||
|
||||
# Create async context manager for session.request
|
||||
async def mock_request(*args, **kwargs):
|
||||
return response
|
||||
|
||||
session.request = MagicMock()
|
||||
session.request.return_value.__aenter__ = AsyncMock(return_value=response)
|
||||
session.request.return_value.__aexit__ = AsyncMock(return_value=None)
|
||||
|
||||
return session
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_connection(mock_session):
|
||||
"""Test API connection."""
|
||||
api = AdGuardHomeAPI(
|
||||
@@ -31,6 +40,7 @@ async def test_api_connection(mock_session):
|
||||
assert result is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_get_status(mock_session):
|
||||
"""Test getting status."""
|
||||
api = AdGuardHomeAPI(
|
||||
@@ -41,3 +51,33 @@ async def test_api_get_status(mock_session):
|
||||
|
||||
status = await api.get_status()
|
||||
assert status == {"status": "ok"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_context_manager():
|
||||
"""Test API as async context manager."""
|
||||
async with AdGuardHomeAPI(host="test-host", port=3000) as api:
|
||||
assert api is not None
|
||||
assert api.host == "test-host"
|
||||
assert api.port == 3000
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_error_handling():
|
||||
"""Test API error handling."""
|
||||
from custom_components.adguard_hub.api import AdGuardConnectionError
|
||||
|
||||
# Test with a session that raises an exception
|
||||
session = MagicMock()
|
||||
session.request = MagicMock()
|
||||
session.request.return_value.__aenter__ = AsyncMock(side_effect=Exception("Connection error"))
|
||||
session.request.return_value.__aexit__ = AsyncMock(return_value=None)
|
||||
|
||||
api = AdGuardHomeAPI(
|
||||
host="test-host",
|
||||
port=3000,
|
||||
session=session
|
||||
)
|
||||
|
||||
with pytest.raises(Exception): # Should raise AdGuardHomeError
|
||||
await api.get_status()
|
||||
|
Reference in New Issue
Block a user