refactor: Refactoring most of the project
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:
2025-09-28 15:34:46 +01:00
parent 13905df0ee
commit 4553eb454a
18 changed files with 180 additions and 259 deletions

View File

@@ -1 +1 @@
"""Tests for AdGuard Control Hub."""
"""Tests for AdGuard Control Hub integration."""

View File

@@ -1,3 +1,4 @@
"""Test configuration and fixtures."""
import pytest
@@ -5,3 +6,11 @@ import pytest
def auto_enable_custom_integrations(enable_custom_integrations):
"""Enable custom integrations for all tests."""
yield
@pytest.fixture(autouse=True)
def mock_platform_setup():
"""Mock platform setup to avoid actual platform loading."""
from unittest.mock import patch
with patch("homeassistant.config_entries.ConfigEntry.async_forward_entry_setups"):
yield

View File

@@ -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()

View File

@@ -1,29 +1,71 @@
"""Test config flow for AdGuard Control Hub."""
import pytest
from homeassistant import config_entries, setup
from unittest.mock import AsyncMock, patch
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from custom_components.adguard_hub.const import DOMAIN
async def test_config_flow_success(hass):
@pytest.mark.asyncio
async def test_config_flow_success(hass: HomeAssistant):
"""Test successful config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch("custom_components.adguard_hub.config_flow.validate_input") as mock_validate:
mock_validate.return_value = {
"title": "AdGuard Control Hub (192.168.1.100)",
"host": "192.168.1.100",
}
assert result["type"] == "form"
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
async def test_config_flow_cannot_connect(hass):
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
@pytest.mark.asyncio
async def test_config_flow_cannot_connect(hass: HomeAssistant):
"""Test config flow with connection error."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data={
"host": "invalid-host",
"port": 3000,
"username": "admin",
"password": "password",
},
)
from custom_components.adguard_hub.config_flow import CannotConnect
assert result["type"] == "form"
assert result["errors"]["base"] == "cannot_connect"
with patch("custom_components.adguard_hub.config_flow.validate_input") as mock_validate:
mock_validate.side_effect = CannotConnect("Connection failed")
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data={
"host": "invalid-host",
"port": 3000,
"username": "admin",
"password": "password",
},
)
assert result["type"] == FlowResultType.FORM
assert result["errors"]["base"] == "cannot_connect"
@pytest.mark.asyncio
async def test_config_flow_invalid_auth(hass: HomeAssistant):
"""Test config flow with authentication error."""
from custom_components.adguard_hub.config_flow import InvalidAuth
with patch("custom_components.adguard_hub.config_flow.validate_input") as mock_validate:
mock_validate.side_effect = InvalidAuth("Auth failed")
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
data={
"host": "192.168.1.100",
"port": 3000,
"username": "wrong",
"password": "wrong",
},
)
assert result["type"] == FlowResultType.FORM
assert result["errors"]["base"] == "invalid_auth"

View File

@@ -2,7 +2,7 @@
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigEntry, SOURCE_USER
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from custom_components.adguard_hub import async_setup_entry, async_unload_entry
@@ -12,9 +12,10 @@ from custom_components.adguard_hub.const import DOMAIN
@pytest.fixture
def mock_config_entry():
"""Mock config entry."""
"""Mock config entry compatible with HA 2025.9.4."""
return ConfigEntry(
version=1,
minor_version=1,
domain=DOMAIN,
title="Test AdGuard",
data={
@@ -23,8 +24,12 @@ def mock_config_entry():
CONF_USERNAME: "admin",
CONF_PASSWORD: "password",
},
source="user",
options={},
source=SOURCE_USER,
entry_id="test_entry_id",
unique_id="192.168.1.100:3000",
discovery_keys=set(),
subentries_data={},
)
@@ -145,11 +150,11 @@ async def test_api_error_handling(mock_api):
await mock_api.get_clients()
@pytest.mark.asyncio
async def test_services_registration(hass: HomeAssistant):
def test_services_registration(hass: HomeAssistant):
"""Test that services are properly registered."""
from custom_components.adguard_hub.services import AdGuardControlHubServices
# Create services without running inside an existing event loop
services = AdGuardControlHubServices(hass)
services.register_services()