mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Rework Snapcast config flow tests (#148434)
This commit is contained in:
parent
511ffdc03c
commit
6f31057d30
@ -10,7 +10,6 @@ from snapcast.control.server import CONTROL_PORT
|
|||||||
from snapcast.control.stream import Snapstream
|
from snapcast.control.stream import Snapstream
|
||||||
|
|
||||||
from homeassistant.components.snapcast.const import DOMAIN
|
from homeassistant.components.snapcast.const import DOMAIN
|
||||||
from homeassistant.components.snapcast.coordinator import Snapserver
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
@ -25,6 +24,16 @@ def mock_setup_entry() -> Generator[AsyncMock]:
|
|||||||
yield mock_setup_entry
|
yield mock_setup_entry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_server(mock_create_server: AsyncMock) -> Generator[AsyncMock]:
|
||||||
|
"""Override async_setup_entry."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.snapcast.config_flow.snapcast.control.create_server",
|
||||||
|
return_value=mock_create_server,
|
||||||
|
) as mock_server:
|
||||||
|
yield mock_server
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_create_server(
|
def mock_create_server(
|
||||||
mock_group: AsyncMock,
|
mock_group: AsyncMock,
|
||||||
@ -64,15 +73,6 @@ async def mock_config_entry() -> MockConfigEntry:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def mock_server_connection() -> Generator[Snapserver]:
|
|
||||||
"""Create a mock server connection."""
|
|
||||||
|
|
||||||
# Patch the start method of the Snapserver class to avoid network connections
|
|
||||||
with patch.object(Snapserver, "start", new_callable=AsyncMock) as mock_start:
|
|
||||||
yield mock_start
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_group(stream: str, streams: dict[str, AsyncMock]) -> AsyncMock:
|
def mock_group(stream: str, streams: dict[str, AsyncMock]) -> AsyncMock:
|
||||||
"""Create a mock Snapgroup."""
|
"""Create a mock Snapgroup."""
|
||||||
|
@ -1,59 +1,32 @@
|
|||||||
"""Test the Snapcast module."""
|
"""Test the Snapcast module."""
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries, setup
|
|
||||||
from homeassistant.components.snapcast.const import DOMAIN
|
from homeassistant.components.snapcast.const import DOMAIN
|
||||||
|
from homeassistant.config_entries import SOURCE_USER
|
||||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
TEST_CONNECTION = {CONF_HOST: "snapserver.test", CONF_PORT: 1705}
|
TEST_CONNECTION = {CONF_HOST: "127.0.0.1", CONF_PORT: 1705}
|
||||||
|
|
||||||
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
async def test_full_flow(
|
||||||
"""Test we get the form and handle errors and successful connection."""
|
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_server: AsyncMock
|
||||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
) -> None:
|
||||||
|
"""Test the full flow."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert not result["errors"]
|
assert not result["errors"]
|
||||||
|
|
||||||
# test invalid host error
|
|
||||||
with patch("snapcast.control.create_server", side_effect=socket.gaierror):
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
TEST_CONNECTION,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.FORM
|
|
||||||
assert result["step_id"] == "user"
|
|
||||||
assert result["errors"] == {"base": "invalid_host"}
|
|
||||||
|
|
||||||
# test connection error
|
|
||||||
with patch("snapcast.control.create_server", side_effect=ConnectionRefusedError):
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
TEST_CONNECTION,
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.FORM
|
|
||||||
assert result["step_id"] == "user"
|
|
||||||
assert result["errors"] == {"base": "cannot_connect"}
|
|
||||||
|
|
||||||
# test success
|
|
||||||
with patch("snapcast.control.create_server"):
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
TEST_CONNECTION,
|
TEST_CONNECTION,
|
||||||
@ -61,31 +34,70 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "Snapcast"
|
assert result["title"] == "Snapcast"
|
||||||
assert result["data"] == {CONF_HOST: "snapserver.test", CONF_PORT: 1705}
|
assert result["data"] == {CONF_HOST: "127.0.0.1", CONF_PORT: 1705}
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_abort(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
@pytest.mark.parametrize(
|
||||||
"""Test config flow abort if device is already configured."""
|
("exception", "error"),
|
||||||
entry = MockConfigEntry(
|
[
|
||||||
domain=DOMAIN,
|
(socket.gaierror, "invalid_host"),
|
||||||
data=TEST_CONNECTION,
|
(ConnectionRefusedError, "cannot_connect"),
|
||||||
)
|
],
|
||||||
entry.add_to_hass(hass)
|
)
|
||||||
|
async def test_exceptions(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_server: AsyncMock,
|
||||||
|
exception: Exception,
|
||||||
|
error: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test we get the form and handle errors and successful connection."""
|
||||||
|
|
||||||
|
mock_server.side_effect = exception
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
|
)
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert not result["errors"]
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
TEST_CONNECTION,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.FORM
|
||||||
|
assert result["step_id"] == "user"
|
||||||
|
assert result["errors"] == {"base": error}
|
||||||
|
|
||||||
|
mock_server.side_effect = None
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_configure(
|
||||||
|
result["flow_id"],
|
||||||
|
TEST_CONNECTION,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
|
|
||||||
|
|
||||||
|
async def test_already_setup(
|
||||||
|
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||||
|
) -> None:
|
||||||
|
"""Test config flow abort if device is already configured."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert not result["errors"]
|
assert not result["errors"]
|
||||||
|
|
||||||
with patch("snapcast.control.create_server", side_effect=socket.gaierror):
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
TEST_CONNECTION,
|
TEST_CONNECTION,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.ABORT
|
assert result["type"] is FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user