refactor: another refactor
Some checks failed
Integration Testing / Integration Tests (2024.12.0, 3.11) (push) Failing after 27s
Integration Testing / Integration Tests (2024.12.0, 3.12) (push) Failing after 56s
Integration Testing / Integration Tests (2024.12.0, 3.13) (push) Failing after 1m38s
Integration Testing / Integration Tests (2025.9.4, 3.11) (push) Failing after 19s
Integration Testing / Integration Tests (2025.9.4, 3.12) (push) Failing after 20s
Integration Testing / Integration Tests (2025.9.4, 3.13) (push) Failing after 25s
Code Quality Check / Code Quality Analysis (push) Failing after 20s
Code Quality Check / Security Analysis (push) Failing after 21s
Some checks failed
Integration Testing / Integration Tests (2024.12.0, 3.11) (push) Failing after 27s
Integration Testing / Integration Tests (2024.12.0, 3.12) (push) Failing after 56s
Integration Testing / Integration Tests (2024.12.0, 3.13) (push) Failing after 1m38s
Integration Testing / Integration Tests (2025.9.4, 3.11) (push) Failing after 19s
Integration Testing / Integration Tests (2025.9.4, 3.12) (push) Failing after 20s
Integration Testing / Integration Tests (2025.9.4, 3.13) (push) Failing after 25s
Code Quality Check / Code Quality Analysis (push) Failing after 20s
Code Quality Check / Security Analysis (push) Failing after 21s
Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
This commit is contained in:
@@ -4,80 +4,50 @@ from unittest.mock import AsyncMock, MagicMock
|
||||
from custom_components.adguard_hub.api import AdGuardHomeAPI
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_session():
|
||||
"""Mock aiohttp session with proper async context manager."""
|
||||
session = MagicMock()
|
||||
response = MagicMock()
|
||||
response.raise_for_status = MagicMock()
|
||||
response.json = AsyncMock(return_value={"status": "ok"})
|
||||
response.status = 200
|
||||
response.content_length = 100
|
||||
class TestAdGuardHomeAPI:
|
||||
"""Test the AdGuard Home API wrapper."""
|
||||
|
||||
# Properly mock the async context manager
|
||||
context_manager = MagicMock()
|
||||
context_manager.__aenter__ = AsyncMock(return_value=response)
|
||||
context_manager.__aexit__ = AsyncMock(return_value=None)
|
||||
def test_api_initialization(self):
|
||||
"""Test API initialization."""
|
||||
api = AdGuardHomeAPI(
|
||||
host="192.168.1.100",
|
||||
port=3000,
|
||||
username="admin",
|
||||
password="password",
|
||||
ssl=True,
|
||||
)
|
||||
|
||||
session.request = MagicMock(return_value=context_manager)
|
||||
|
||||
return session
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_connection(mock_session):
|
||||
"""Test API connection."""
|
||||
api = AdGuardHomeAPI(
|
||||
host="test-host",
|
||||
port=3000,
|
||||
username="admin",
|
||||
password="password",
|
||||
session=mock_session
|
||||
)
|
||||
|
||||
result = await api.test_connection()
|
||||
assert result is True
|
||||
mock_session.request.assert_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_get_status(mock_session):
|
||||
"""Test getting status."""
|
||||
api = AdGuardHomeAPI(
|
||||
host="test-host",
|
||||
port=3000,
|
||||
session=mock_session
|
||||
)
|
||||
|
||||
status = await api.get_status()
|
||||
assert status == {"status": "ok"}
|
||||
mock_session.request.assert_called()
|
||||
|
||||
|
||||
@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.host == "192.168.1.100"
|
||||
assert api.port == 3000
|
||||
assert api.username == "admin"
|
||||
assert api.password == "password"
|
||||
assert api.ssl is True
|
||||
assert api.base_url == "https://192.168.1.100:3000"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_context_manager(self):
|
||||
"""Test API as async context manager."""
|
||||
async with AdGuardHomeAPI(host="192.168.1.100", port=3000) as api:
|
||||
assert api is not None
|
||||
assert api.host == "192.168.1.100"
|
||||
assert api.port == 3000
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_error_handling():
|
||||
"""Test API error handling."""
|
||||
# Test with a session that raises an exception
|
||||
session = MagicMock()
|
||||
context_manager = MagicMock()
|
||||
context_manager.__aenter__ = AsyncMock(side_effect=Exception("Connection error"))
|
||||
context_manager.__aexit__ = AsyncMock(return_value=None)
|
||||
session.request = MagicMock(return_value=context_manager)
|
||||
@pytest.mark.asyncio
|
||||
async def test_test_connection_success(self):
|
||||
"""Test successful connection test."""
|
||||
session = MagicMock()
|
||||
response = MagicMock()
|
||||
response.status = 200
|
||||
response.json = AsyncMock(return_value={"protection_enabled": True})
|
||||
response.raise_for_status = MagicMock()
|
||||
response.content_length = 100
|
||||
|
||||
api = AdGuardHomeAPI(
|
||||
host="test-host",
|
||||
port=3000,
|
||||
session=session
|
||||
)
|
||||
context_manager = MagicMock()
|
||||
context_manager.__aenter__ = AsyncMock(return_value=response)
|
||||
context_manager.__aexit__ = AsyncMock(return_value=None)
|
||||
session.request = MagicMock(return_value=context_manager)
|
||||
|
||||
with pytest.raises(Exception):
|
||||
await api.get_status()
|
||||
api = AdGuardHomeAPI(host="192.168.1.100", session=session)
|
||||
result = await api.test_connection()
|
||||
|
||||
assert result is True
|
||||
|
Reference in New Issue
Block a user