Add config flow to Azure Event Hub integration (#61155)

* config flow added, no tests yet

* added tests

* refinement of tests

* small reverses of hub code

* fix small bug

* test fixes from review

* test fixes from review

* further refinement of tests and config flow

* removed true return from hub and added failed reason for import

* added deepcopy to default options

* deleted max_delay from options, can still be in yaml for now

* updated dropped message

* mistaken period at eol
This commit is contained in:
Eduard van Valkenburg 2021-12-16 18:57:51 +01:00 committed by GitHub
parent 619529b40c
commit 80833aa7fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 980 additions and 250 deletions

View file

@ -0,0 +1,126 @@
"""Test fixtures for AEH."""
from dataclasses import dataclass
from datetime import timedelta
import logging
from unittest.mock import MagicMock, patch
from azure.eventhub.aio import EventHubProducerClient
import pytest
from homeassistant.components.azure_event_hub.const import (
CONF_FILTER,
CONF_SEND_INTERVAL,
DOMAIN,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_ON
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
from .const import AZURE_EVENT_HUB_PATH, BASIC_OPTIONS, PRODUCER_PATH, SAS_CONFIG_FULL
from tests.common import MockConfigEntry, async_fire_time_changed
_LOGGER = logging.getLogger(__name__)
# fixtures for both init and config flow tests
@pytest.fixture(autouse=True, name="mock_get_eventhub_properties")
def mock_get_eventhub_properties_fixture():
"""Mock azure event hub properties, used to test the connection."""
with patch(f"{PRODUCER_PATH}.get_eventhub_properties") as get_eventhub_properties:
yield get_eventhub_properties
@pytest.fixture(name="filter_schema")
def mock_filter_schema():
"""Return an empty filter."""
return {}
@pytest.fixture(name="entry")
async def mock_entry_fixture(hass, filter_schema, mock_create_batch, mock_send_batch):
"""Create the setup in HA."""
entry = MockConfigEntry(
domain=DOMAIN,
data=SAS_CONFIG_FULL,
title="test-instance",
options=BASIC_OPTIONS,
)
entry.add_to_hass(hass)
assert await async_setup_component(
hass, DOMAIN, {DOMAIN: {CONF_FILTER: filter_schema}}
)
assert entry.state == ConfigEntryState.LOADED
# Clear the component_loaded event from the queue.
async_fire_time_changed(
hass,
utcnow() + timedelta(seconds=entry.options[CONF_SEND_INTERVAL]),
)
await hass.async_block_till_done()
return entry
# fixtures for init tests
@pytest.fixture(name="entry_with_one_event")
async def mock_entry_with_one_event(hass, entry):
"""Use the entry and add a single test event to the queue."""
assert entry.state == ConfigEntryState.LOADED
hass.states.async_set("sensor.test", STATE_ON)
return entry
@dataclass
class FilterTest:
"""Class for capturing a filter test."""
entity_id: str
expected_count: int
@pytest.fixture(name="mock_send_batch")
def mock_send_batch_fixture():
"""Mock send_batch."""
with patch(f"{PRODUCER_PATH}.send_batch") as mock_send_batch:
yield mock_send_batch
@pytest.fixture(autouse=True, name="mock_client")
def mock_client_fixture(mock_send_batch):
"""Mock the azure event hub producer client."""
with patch(f"{PRODUCER_PATH}.close") as mock_close:
yield (
mock_send_batch,
mock_close,
)
@pytest.fixture(name="mock_create_batch")
def mock_create_batch_fixture():
"""Mock batch creator and return mocked batch object."""
mock_batch = MagicMock()
with patch(f"{PRODUCER_PATH}.create_batch", return_value=mock_batch):
yield mock_batch
# fixtures for config flow tests
@pytest.fixture(name="mock_from_connection_string")
def mock_from_connection_string_fixture():
"""Mock AEH from connection string creation."""
mock_aeh = MagicMock(spec=EventHubProducerClient)
mock_aeh.__aenter__.return_value = mock_aeh
with patch(
f"{PRODUCER_PATH}.from_connection_string",
return_value=mock_aeh,
) as from_conn_string:
yield from_conn_string
@pytest.fixture(name="mock_setup_entry")
def mock_setup_entry():
"""Mock the setup entry call, used for config flow tests."""
with patch(
f"{AZURE_EVENT_HUB_PATH}.async_setup_entry", return_value=True
) as setup_entry:
yield setup_entry