fix: some minor fixes
Some checks failed
Tests / test (3.13) (push) Failing after 26s
Tests / lint (push) Failing after 16s
Tests / hacs (push) Failing after 30s

Signed-off-by: Rafal Zielinski <sq4ind@gmail.com>
This commit is contained in:
2025-10-02 16:09:10 +01:00
parent bd6a73fa15
commit e23d60d895
10 changed files with 185 additions and 22 deletions

View File

@@ -3,7 +3,7 @@ import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.config_entries import ConfigEntry
from custom_components.adguard_control_hub.const import (
CONF_HOST,
@@ -50,8 +50,8 @@ def mock_adguard_api():
@pytest.fixture
def mock_config_entry():
"""Mock config entry."""
def mock_config_entry_data():
"""Mock config entry data."""
return {
CONF_HOST: "192.168.1.100",
CONF_PORT: 3000,
@@ -63,7 +63,11 @@ def mock_config_entry():
@pytest.fixture
def mock_setup_entry():
"""Mock setup entry."""
with patch("custom_components.adguard_control_hub.AdGuardHomeAPI") as mock_api_class:
yield mock_api_class
def mock_config_entry(mock_config_entry_data):
"""Mock config entry."""
entry = MagicMock(spec=ConfigEntry)
entry.data = mock_config_entry_data
entry.entry_id = "test_entry_id"
entry.title = "AdGuard Home (192.168.1.100)"
entry.domain = DOMAIN
return entry

View File

@@ -13,6 +13,7 @@ from custom_components.adguard_control_hub.config_flow import (
from custom_components.adguard_control_hub.const import CONF_SSL, CONF_VERIFY_SSL, DOMAIN
@pytest.mark.asyncio
async def test_form(hass: HomeAssistant) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
@@ -22,6 +23,7 @@ async def test_form(hass: HomeAssistant) -> None:
assert result["errors"] is None
@pytest.mark.asyncio
async def test_form_valid_connection(hass: HomeAssistant, mock_adguard_api) -> None:
"""Test we get the form with valid connection."""
with patch(
@@ -58,6 +60,7 @@ async def test_form_valid_connection(hass: HomeAssistant, mock_adguard_api) -> N
}
@pytest.mark.asyncio
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
"""Test we handle cannot connect error."""
with patch(
@@ -80,6 +83,7 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
assert result2["errors"] == {"base": "cannot_connect"}
@pytest.mark.asyncio
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
"""Test we handle invalid auth error."""
with patch(

View File

@@ -1,5 +1,5 @@
"""Test AdGuard Control Hub initialization."""
from unittest.mock import patch
from unittest.mock import patch, AsyncMock
import pytest
from homeassistant.config_entries import ConfigEntryState
@@ -9,39 +9,71 @@ from homeassistant.core import HomeAssistant
from custom_components.adguard_control_hub.const import DOMAIN
async def test_setup_entry(hass: HomeAssistant, mock_config_entry, mock_adguard_api) -> None:
@pytest.mark.asyncio
async def test_setup_entry(hass: HomeAssistant, mock_config_entry_data, mock_adguard_api) -> None:
"""Test successful setup of entry."""
with patch(
"custom_components.adguard_control_hub.AdGuardHomeAPI"
) as mock_api_class:
mock_api_class.return_value = mock_adguard_api
entry = hass.config_entries.async_add(mock_config_entry)
# Create a mock config entry
from homeassistant.config_entries import ConfigEntry
entry = ConfigEntry(
version=1,
minor_version=1,
domain=DOMAIN,
title="Test AdGuard",
data=mock_config_entry_data,
options={},
source="test",
unique_id="test_unique_id",
)
# Add entry to hass
hass.config_entries._entries[entry.entry_id] = entry
with patch("custom_components.adguard_control_hub.PLATFORMS", []):
await hass.config_entries.async_setup(entry.entry_id)
result = await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
assert result is True
assert DOMAIN in hass.data
async def test_unload_entry(hass: HomeAssistant, mock_config_entry, mock_adguard_api) -> None:
@pytest.mark.asyncio
async def test_unload_entry(hass: HomeAssistant, mock_config_entry_data, mock_adguard_api) -> None:
"""Test successful unload of entry."""
with patch(
"custom_components.adguard_control_hub.AdGuardHomeAPI"
) as mock_api_class:
mock_api_class.return_value = mock_adguard_api
entry = hass.config_entries.async_add(mock_config_entry)
# Create a mock config entry
from homeassistant.config_entries import ConfigEntry
entry = ConfigEntry(
version=1,
minor_version=1,
domain=DOMAIN,
title="Test AdGuard",
data=mock_config_entry_data,
options={},
source="test",
unique_id="test_unique_id",
)
# Add entry to hass
hass.config_entries._entries[entry.entry_id] = entry
with patch("custom_components.adguard_control_hub.PLATFORMS", []):
# Setup first
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.LOADED
assert DOMAIN in hass.data
await hass.config_entries.async_unload(entry.entry_id)
# Then unload
result = await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ConfigEntryState.NOT_LOADED
assert result is True

17
tests/test_simple.py Normal file
View File

@@ -0,0 +1,17 @@
"""Simple test to verify pytest configuration."""
import pytest
@pytest.mark.asyncio
async def test_async_works():
"""Test that async functions work with pytest."""
async def simple_async_function():
return True
result = await simple_async_function()
assert result is True
def test_sync_works():
"""Test that sync functions work with pytest."""
assert True is True