Refactor tests for discovergy (#103667)
This commit is contained in:
parent
0e04cd6b35
commit
b4e8243e18
5 changed files with 120 additions and 33 deletions
|
@ -1,33 +1,59 @@
|
||||||
"""Fixtures for Discovergy integration tests."""
|
"""Fixtures for Discovergy integration tests."""
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
from pydiscovergy import Discovergy
|
||||||
|
from pydiscovergy.models import Reading
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.discovergy import DOMAIN
|
from homeassistant.components.discovergy import DOMAIN
|
||||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
from tests.components.discovergy.const import GET_METERS
|
from tests.components.discovergy.const import GET_METERS, LAST_READING, LAST_READING_GAS
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
def _meter_last_reading(meter_id: str) -> Reading:
|
||||||
def mock_meters() -> Mock:
|
"""Side effect function for Discovergy mock."""
|
||||||
"""Patch libraries."""
|
return (
|
||||||
with patch("pydiscovergy.Discovergy.meters") as discovergy:
|
LAST_READING_GAS
|
||||||
discovergy.side_effect = AsyncMock(return_value=GET_METERS)
|
if meter_id == "d81a652fe0824f9a9d336016587d3b9d"
|
||||||
yield discovergy
|
else LAST_READING
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(name="discovergy")
|
||||||
|
def mock_discovergy() -> None:
|
||||||
|
"""Mock the pydiscovergy client."""
|
||||||
|
mock = AsyncMock(spec=Discovergy)
|
||||||
|
mock.meters.return_value = GET_METERS
|
||||||
|
mock.meter_last_reading.side_effect = _meter_last_reading
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.discovergy.pydiscovergy.Discovergy",
|
||||||
|
return_value=mock,
|
||||||
|
):
|
||||||
|
yield mock
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="config_entry")
|
||||||
async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
||||||
"""Return a MockConfigEntry for testing."""
|
"""Return a MockConfigEntry for testing."""
|
||||||
entry = MockConfigEntry(
|
return MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
title="user@example.org",
|
title="user@example.org",
|
||||||
unique_id="user@example.org",
|
unique_id="user@example.org",
|
||||||
data={CONF_EMAIL: "user@example.org", CONF_PASSWORD: "supersecretpassword"},
|
data={CONF_EMAIL: "user@example.org", CONF_PASSWORD: "supersecretpassword"},
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
return entry
|
|
||||||
|
@pytest.fixture(name="setup_integration")
|
||||||
|
async def mock_setup_integration(
|
||||||
|
hass: HomeAssistant, config_entry: MockConfigEntry, discovergy: AsyncMock
|
||||||
|
) -> None:
|
||||||
|
"""Fixture for setting up the component."""
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
assert await async_setup_component(hass, DOMAIN, {})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
|
@ -30,6 +30,32 @@ GET_METERS = [
|
||||||
"last_measurement_time": 1678430543742,
|
"last_measurement_time": 1678430543742,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
Meter(
|
||||||
|
meter_id="d81a652fe0824f9a9d336016587d3b9d",
|
||||||
|
serial_number="def456",
|
||||||
|
full_serial_number="def456",
|
||||||
|
type="PIP",
|
||||||
|
measurement_type="GAS",
|
||||||
|
load_profile_type="SLP",
|
||||||
|
location=Location(
|
||||||
|
zip=12345,
|
||||||
|
city="Testhause",
|
||||||
|
street="Teststraße",
|
||||||
|
street_number="1",
|
||||||
|
country="Germany",
|
||||||
|
),
|
||||||
|
additional={
|
||||||
|
"manufacturer_id": "TST",
|
||||||
|
"printed_full_serial_number": "def456",
|
||||||
|
"administration_number": "12345",
|
||||||
|
"scaling_factor": 1,
|
||||||
|
"current_scaling_factor": 1,
|
||||||
|
"voltage_scaling_factor": 1,
|
||||||
|
"internal_meters": 1,
|
||||||
|
"first_measurement_time": 1517569090926,
|
||||||
|
"last_measurement_time": 1678430543742,
|
||||||
|
},
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
LAST_READING = Reading(
|
LAST_READING = Reading(
|
||||||
|
@ -50,3 +76,8 @@ LAST_READING = Reading(
|
||||||
"voltage3": 239000.0,
|
"voltage3": 239000.0,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
LAST_READING_GAS = Reading(
|
||||||
|
time=datetime.datetime(2023, 3, 10, 7, 32, 6, 702000),
|
||||||
|
values={"actualityDuration": 52000.0, "storageNumber": 0.0, "volume": 21064800.0},
|
||||||
|
)
|
||||||
|
|
|
@ -22,8 +22,36 @@
|
||||||
'serial_number': '**REDACTED**',
|
'serial_number': '**REDACTED**',
|
||||||
'type': 'TST',
|
'type': 'TST',
|
||||||
}),
|
}),
|
||||||
|
dict({
|
||||||
|
'additional': dict({
|
||||||
|
'administration_number': '**REDACTED**',
|
||||||
|
'current_scaling_factor': 1,
|
||||||
|
'first_measurement_time': 1517569090926,
|
||||||
|
'internal_meters': 1,
|
||||||
|
'last_measurement_time': 1678430543742,
|
||||||
|
'manufacturer_id': 'TST',
|
||||||
|
'printed_full_serial_number': '**REDACTED**',
|
||||||
|
'scaling_factor': 1,
|
||||||
|
'voltage_scaling_factor': 1,
|
||||||
|
}),
|
||||||
|
'full_serial_number': '**REDACTED**',
|
||||||
|
'load_profile_type': 'SLP',
|
||||||
|
'location': '**REDACTED**',
|
||||||
|
'measurement_type': 'GAS',
|
||||||
|
'meter_id': 'd81a652fe0824f9a9d336016587d3b9d',
|
||||||
|
'serial_number': '**REDACTED**',
|
||||||
|
'type': 'PIP',
|
||||||
|
}),
|
||||||
]),
|
]),
|
||||||
'readings': dict({
|
'readings': dict({
|
||||||
|
'd81a652fe0824f9a9d336016587d3b9d': dict({
|
||||||
|
'time': '2023-03-10T07:32:06.702000',
|
||||||
|
'values': dict({
|
||||||
|
'actualityDuration': 52000.0,
|
||||||
|
'storageNumber': 0.0,
|
||||||
|
'volume': 21064800.0,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
'f8d610b7a8cc4e73939fa33b990ded54': dict({
|
'f8d610b7a8cc4e73939fa33b990ded54': dict({
|
||||||
'time': '2023-03-10T07:32:06.702000',
|
'time': '2023-03-10T07:32:06.702000',
|
||||||
'values': dict({
|
'values': dict({
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""Test the Discovergy config flow."""
|
"""Test the Discovergy config flow."""
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin
|
from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -14,7 +14,7 @@ from tests.common import MockConfigEntry
|
||||||
from tests.components.discovergy.const import GET_METERS
|
from tests.components.discovergy.const import GET_METERS
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None:
|
async def test_form(hass: HomeAssistant, discovergy: AsyncMock) -> None:
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
|
@ -25,7 +25,10 @@ async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None:
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.discovergy.async_setup_entry",
|
"homeassistant.components.discovergy.async_setup_entry",
|
||||||
return_value=True,
|
return_value=True,
|
||||||
) as mock_setup_entry:
|
) as mock_setup_entry, patch(
|
||||||
|
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy",
|
||||||
|
return_value=discovergy,
|
||||||
|
):
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
|
@ -45,12 +48,14 @@ async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None:
|
||||||
|
|
||||||
|
|
||||||
async def test_reauth(
|
async def test_reauth(
|
||||||
hass: HomeAssistant, mock_meters: Mock, mock_config_entry: MockConfigEntry
|
hass: HomeAssistant, config_entry: MockConfigEntry, discovergy: AsyncMock
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test reauth flow."""
|
"""Test reauth flow."""
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
init_result = await hass.config_entries.flow.async_init(
|
init_result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_REAUTH, "unique_id": mock_config_entry.unique_id},
|
context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id},
|
||||||
data=None,
|
data=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -60,7 +65,10 @@ async def test_reauth(
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.discovergy.async_setup_entry",
|
"homeassistant.components.discovergy.async_setup_entry",
|
||||||
return_value=True,
|
return_value=True,
|
||||||
) as mock_setup_entry:
|
) as mock_setup_entry, patch(
|
||||||
|
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy",
|
||||||
|
return_value=discovergy,
|
||||||
|
):
|
||||||
configure_result = await hass.config_entries.flow.async_configure(
|
configure_result = await hass.config_entries.flow.async_configure(
|
||||||
init_result["flow_id"],
|
init_result["flow_id"],
|
||||||
{
|
{
|
||||||
|
@ -88,7 +96,7 @@ async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) ->
|
||||||
"""Test to handle exceptions."""
|
"""Test to handle exceptions."""
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"pydiscovergy.Discovergy.meters",
|
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters",
|
||||||
side_effect=error,
|
side_effect=error,
|
||||||
):
|
):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
@ -104,7 +112,10 @@ async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) ->
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"] == {"base": message}
|
assert result["errors"] == {"base": message}
|
||||||
|
|
||||||
with patch("pydiscovergy.Discovergy.meters", return_value=GET_METERS):
|
with patch(
|
||||||
|
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters",
|
||||||
|
return_value=GET_METERS,
|
||||||
|
):
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,31 +1,22 @@
|
||||||
"""Test Discovergy diagnostics."""
|
"""Test Discovergy diagnostics."""
|
||||||
from unittest.mock import patch
|
import pytest
|
||||||
|
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||||
from tests.components.discovergy.const import GET_METERS, LAST_READING
|
|
||||||
from tests.typing import ClientSessionGenerator
|
from tests.typing import ClientSessionGenerator
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("setup_integration")
|
||||||
async def test_entry_diagnostics(
|
async def test_entry_diagnostics(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client: ClientSessionGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
mock_config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test config entry diagnostics."""
|
"""Test config entry diagnostics."""
|
||||||
with patch("pydiscovergy.Discovergy.meters", return_value=GET_METERS), patch(
|
result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||||
"pydiscovergy.Discovergy.meter_last_reading", return_value=LAST_READING
|
|
||||||
):
|
|
||||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
result = await get_diagnostics_for_config_entry(
|
|
||||||
hass, hass_client, mock_config_entry
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result == snapshot
|
assert result == snapshot
|
||||||
|
|
Loading…
Add table
Reference in a new issue