mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Rework opensky tests (#114441)
* Rework opensky tests * Rework opensky tests * Fix
This commit is contained in:
parent
6587ee20db
commit
b7527feb5f
@ -100,9 +100,7 @@ class OpenSkyOptionsFlowHandler(OptionsFlowWithConfigEntry):
|
|||||||
if user_input[CONF_CONTRIBUTING_USER] and not authentication:
|
if user_input[CONF_CONTRIBUTING_USER] and not authentication:
|
||||||
errors["base"] = "no_authentication"
|
errors["base"] = "no_authentication"
|
||||||
if authentication and not errors:
|
if authentication and not errors:
|
||||||
async with OpenSky(
|
opensky = OpenSky(session=async_get_clientsession(self.hass))
|
||||||
session=async_get_clientsession(self.hass)
|
|
||||||
) as opensky:
|
|
||||||
try:
|
try:
|
||||||
await opensky.authenticate(
|
await opensky.authenticate(
|
||||||
BasicAuth(
|
BasicAuth(
|
||||||
|
@ -1,20 +1,12 @@
|
|||||||
"""Opensky tests."""
|
"""Opensky tests."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from python_opensky import StatesResponse
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
from tests.common import load_json_object_fixture
|
|
||||||
|
|
||||||
|
|
||||||
def patch_setup_entry() -> bool:
|
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
|
||||||
"""Patch interface."""
|
"""Set up the integration."""
|
||||||
return patch(
|
config_entry.add_to_hass(hass)
|
||||||
"homeassistant.components.opensky.async_setup_entry", return_value=True
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
)
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
||||||
def get_states_response_fixture(fixture: str) -> StatesResponse:
|
|
||||||
"""Return the states response from json."""
|
|
||||||
states_json = load_json_object_fixture(fixture)
|
|
||||||
return StatesResponse.from_api(states_json)
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
"""Configure tests for the OpenSky integration."""
|
"""Configure tests for the OpenSky integration."""
|
||||||
|
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Generator
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from python_opensky import StatesResponse
|
||||||
|
|
||||||
from homeassistant.components.opensky.const import (
|
from homeassistant.components.opensky.const import (
|
||||||
CONF_ALTITUDE,
|
CONF_ALTITUDE,
|
||||||
@ -17,14 +18,18 @@ from homeassistant.const import (
|
|||||||
CONF_RADIUS,
|
CONF_RADIUS,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
|
||||||
from homeassistant.setup import async_setup_component
|
|
||||||
|
|
||||||
from . import get_states_response_fixture
|
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
|
||||||
|
|
||||||
ComponentSetup = Callable[[MockConfigEntry], Awaitable[None]]
|
@pytest.fixture
|
||||||
|
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
|
||||||
|
"""Override async_setup_entry."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.opensky.async_setup_entry",
|
||||||
|
return_value=True,
|
||||||
|
) as mock_setup_entry:
|
||||||
|
yield mock_setup_entry
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="config_entry")
|
@pytest.fixture(name="config_entry")
|
||||||
@ -81,19 +86,22 @@ def mock_config_entry_authenticated() -> MockConfigEntry:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="setup_integration")
|
@pytest.fixture
|
||||||
async def mock_setup_integration(
|
async def opensky_client() -> Generator[AsyncMock, None, None]:
|
||||||
hass: HomeAssistant,
|
"""Mock the OpenSky client."""
|
||||||
) -> Callable[[MockConfigEntry], Awaitable[None]]:
|
with (
|
||||||
"""Fixture for setting up the component."""
|
patch(
|
||||||
|
"homeassistant.components.opensky.OpenSky",
|
||||||
async def func(mock_config_entry: MockConfigEntry) -> None:
|
autospec=True,
|
||||||
mock_config_entry.add_to_hass(hass)
|
) as mock_client,
|
||||||
with patch(
|
patch(
|
||||||
"python_opensky.OpenSky.get_states",
|
"homeassistant.components.opensky.config_flow.OpenSky",
|
||||||
return_value=get_states_response_fixture("opensky/states.json"),
|
new=mock_client,
|
||||||
|
),
|
||||||
):
|
):
|
||||||
assert await async_setup_component(hass, DOMAIN, {})
|
client = mock_client.return_value
|
||||||
await hass.async_block_till_done()
|
client.get_states.return_value = StatesResponse.from_api(
|
||||||
|
load_json_object_fixture("states.json", DOMAIN)
|
||||||
return func
|
)
|
||||||
|
client.is_authenticated = False
|
||||||
|
yield client
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Test OpenSky config flow."""
|
"""Test OpenSky config flow."""
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from python_opensky.exceptions import OpenSkyUnauthenticatedError
|
from python_opensky.exceptions import OpenSkyUnauthenticatedError
|
||||||
@ -23,15 +23,12 @@ from homeassistant.const import (
|
|||||||
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 . import get_states_response_fixture, patch_setup_entry
|
|
||||||
from .conftest import ComponentSetup
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
from tests.components.opensky import setup_integration
|
||||||
|
|
||||||
|
|
||||||
async def test_full_user_flow(hass: HomeAssistant) -> None:
|
async def test_full_user_flow(hass: HomeAssistant, mock_setup_entry) -> None:
|
||||||
"""Test the full user configuration flow."""
|
"""Test the full user configuration flow."""
|
||||||
with patch_setup_entry():
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
@ -79,22 +76,20 @@ async def test_full_user_flow(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
async def test_options_flow_failures(
|
async def test_options_flow_failures(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
mock_setup_entry: AsyncMock,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
user_input: dict[str, Any],
|
user_input: dict[str, Any],
|
||||||
error: str,
|
error: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test load and unload entry."""
|
"""Test load and unload entry."""
|
||||||
await setup_integration(config_entry)
|
await setup_integration(hass, config_entry)
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
|
||||||
with patch(
|
opensky_client.authenticate.side_effect = OpenSkyUnauthenticatedError
|
||||||
"python_opensky.OpenSky.authenticate",
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
side_effect=OpenSkyUnauthenticatedError(),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "init"
|
assert result["step_id"] == "init"
|
||||||
|
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
@ -103,16 +98,10 @@ async def test_options_flow_failures(
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == FlowResultType.FORM
|
||||||
assert result["step_id"] == "init"
|
assert result["step_id"] == "init"
|
||||||
assert result["errors"]["base"] == error
|
assert result["errors"]["base"] == error
|
||||||
with (
|
opensky_client.authenticate.side_effect = None
|
||||||
patch("python_opensky.OpenSky.authenticate"),
|
|
||||||
patch(
|
|
||||||
"python_opensky.OpenSky.get_states",
|
|
||||||
return_value=get_states_response_fixture("opensky/states_1.json"),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
@ -124,7 +113,7 @@ async def test_options_flow_failures(
|
|||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
CONF_RADIUS: 10000,
|
CONF_RADIUS: 10000,
|
||||||
CONF_USERNAME: "homeassistant",
|
CONF_USERNAME: "homeassistant",
|
||||||
@ -135,21 +124,14 @@ async def test_options_flow_failures(
|
|||||||
|
|
||||||
async def test_options_flow(
|
async def test_options_flow(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
mock_setup_entry: AsyncMock,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test options flow."""
|
"""Test options flow."""
|
||||||
await setup_integration(config_entry)
|
await setup_integration(hass, config_entry)
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
with (
|
|
||||||
patch("python_opensky.OpenSky.authenticate"),
|
|
||||||
patch(
|
|
||||||
"python_opensky.OpenSky.get_states",
|
|
||||||
return_value=get_states_response_fixture("opensky/states_1.json"),
|
|
||||||
),
|
|
||||||
):
|
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
|
@ -2,67 +2,59 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from python_opensky import OpenSkyError
|
from python_opensky import OpenSkyError
|
||||||
from python_opensky.exceptions import OpenSkyUnauthenticatedError
|
from python_opensky.exceptions import OpenSkyUnauthenticatedError
|
||||||
|
|
||||||
from homeassistant.components.opensky.const import DOMAIN
|
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
|
||||||
|
|
||||||
from .conftest import ComponentSetup
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
from tests.components.opensky import setup_integration
|
||||||
|
|
||||||
|
|
||||||
async def test_load_unload_entry(
|
async def test_load_unload_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test load and unload entry."""
|
"""Test load and unload entry."""
|
||||||
await setup_integration(config_entry)
|
await setup_integration(hass, config_entry)
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
|
||||||
|
|
||||||
state = hass.states.get("sensor.opensky")
|
assert config_entry.state is ConfigEntryState.LOADED
|
||||||
assert state
|
|
||||||
|
|
||||||
await hass.config_entries.async_remove(entry.entry_id)
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("sensor.opensky")
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
assert not state
|
|
||||||
|
|
||||||
|
|
||||||
async def test_load_entry_failure(
|
async def test_load_entry_failure(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test failure while loading."""
|
"""Test failure while loading."""
|
||||||
|
opensky_client.get_states.side_effect = OpenSkyError()
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
with patch(
|
assert not await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
"python_opensky.OpenSky.get_states",
|
|
||||||
side_effect=OpenSkyError(),
|
|
||||||
):
|
|
||||||
assert await async_setup_component(hass, DOMAIN, {})
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
|
||||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
assert config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
|
||||||
|
|
||||||
async def test_load_entry_authentication_failure(
|
async def test_load_entry_authentication_failure(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry_authenticated: MockConfigEntry,
|
config_entry_authenticated: MockConfigEntry,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test auth failure while loading."""
|
"""Test auth failure while loading."""
|
||||||
|
opensky_client.authenticate.side_effect = OpenSkyUnauthenticatedError()
|
||||||
config_entry_authenticated.add_to_hass(hass)
|
config_entry_authenticated.add_to_hass(hass)
|
||||||
with patch(
|
assert not await hass.config_entries.async_setup(
|
||||||
"python_opensky.OpenSky.authenticate",
|
config_entry_authenticated.entry_id
|
||||||
side_effect=OpenSkyUnauthenticatedError(),
|
)
|
||||||
):
|
|
||||||
assert await async_setup_component(hass, DOMAIN, {})
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
|
||||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
assert config_entry_authenticated.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
@ -1,31 +1,35 @@
|
|||||||
"""OpenSky sensor tests."""
|
"""OpenSky sensor tests."""
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from freezegun.api import FrozenDateTimeFactory
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
|
from python_opensky import StatesResponse
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.components.opensky.const import (
|
from homeassistant.components.opensky.const import (
|
||||||
|
DOMAIN,
|
||||||
EVENT_OPENSKY_ENTRY,
|
EVENT_OPENSKY_ENTRY,
|
||||||
EVENT_OPENSKY_EXIT,
|
EVENT_OPENSKY_EXIT,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant
|
from homeassistant.core import Event, HomeAssistant
|
||||||
|
|
||||||
from . import get_states_response_fixture
|
from tests.common import (
|
||||||
from .conftest import ComponentSetup
|
MockConfigEntry,
|
||||||
|
async_fire_time_changed,
|
||||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
load_json_object_fixture,
|
||||||
|
)
|
||||||
|
from tests.components.opensky import setup_integration
|
||||||
|
|
||||||
|
|
||||||
async def test_sensor(
|
async def test_sensor(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
setup_integration: ComponentSetup,
|
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
):
|
):
|
||||||
"""Test setup sensor."""
|
"""Test setup sensor."""
|
||||||
await setup_integration(config_entry)
|
await setup_integration(hass, config_entry)
|
||||||
|
|
||||||
state = hass.states.get("sensor.opensky")
|
state = hass.states.get("sensor.opensky")
|
||||||
assert state == snapshot
|
assert state == snapshot
|
||||||
@ -42,11 +46,11 @@ async def test_sensor(
|
|||||||
async def test_sensor_altitude(
|
async def test_sensor_altitude(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry_altitude: MockConfigEntry,
|
config_entry_altitude: MockConfigEntry,
|
||||||
setup_integration: ComponentSetup,
|
opensky_client: AsyncMock,
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
):
|
):
|
||||||
"""Test setup sensor with a set altitude."""
|
"""Test setup sensor with a set altitude."""
|
||||||
await setup_integration(config_entry_altitude)
|
await setup_integration(hass, config_entry_altitude)
|
||||||
|
|
||||||
state = hass.states.get("sensor.opensky")
|
state = hass.states.get("sensor.opensky")
|
||||||
assert state == snapshot
|
assert state == snapshot
|
||||||
@ -55,12 +59,12 @@ async def test_sensor_altitude(
|
|||||||
async def test_sensor_updating(
|
async def test_sensor_updating(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
|
opensky_client: AsyncMock,
|
||||||
freezer: FrozenDateTimeFactory,
|
freezer: FrozenDateTimeFactory,
|
||||||
setup_integration: ComponentSetup,
|
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
):
|
):
|
||||||
"""Test updating sensor."""
|
"""Test updating sensor."""
|
||||||
await setup_integration(config_entry)
|
await setup_integration(hass, config_entry)
|
||||||
|
|
||||||
events = []
|
events = []
|
||||||
|
|
||||||
@ -77,13 +81,11 @@ async def test_sensor_updating(
|
|||||||
|
|
||||||
assert events == snapshot
|
assert events == snapshot
|
||||||
|
|
||||||
with patch(
|
opensky_client.get_states.return_value = StatesResponse.from_api(
|
||||||
"python_opensky.OpenSky.get_states",
|
load_json_object_fixture("states_1.json", DOMAIN)
|
||||||
return_value=get_states_response_fixture("opensky/states_1.json"),
|
)
|
||||||
):
|
|
||||||
await skip_time_and_check_events()
|
await skip_time_and_check_events()
|
||||||
with patch(
|
opensky_client.get_states.return_value = StatesResponse.from_api(
|
||||||
"python_opensky.OpenSky.get_states",
|
load_json_object_fixture("states.json", DOMAIN)
|
||||||
return_value=get_states_response_fixture("opensky/states.json"),
|
)
|
||||||
):
|
|
||||||
await skip_time_and_check_events()
|
await skip_time_and_check_events()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user