Skip to content

Commit

Permalink
Refactor tests for discovergy (#103667)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Nov 15, 2023
1 parent 0e04cd6 commit b4e8243
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 33 deletions.
50 changes: 38 additions & 12 deletions tests/components/discovergy/conftest.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
"""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

from homeassistant.components.discovergy import DOMAIN
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

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 mock_meters() -> Mock:
"""Patch libraries."""
with patch("pydiscovergy.Discovergy.meters") as discovergy:
discovergy.side_effect = AsyncMock(return_value=GET_METERS)
yield discovergy
def _meter_last_reading(meter_id: str) -> Reading:
"""Side effect function for Discovergy mock."""
return (
LAST_READING_GAS
if meter_id == "d81a652fe0824f9a9d336016587d3b9d"
else LAST_READING
)


@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

@pytest.fixture(name="config_entry")
async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"""Return a MockConfigEntry for testing."""
entry = MockConfigEntry(
return MockConfigEntry(
domain=DOMAIN,
title="user@example.org",
unique_id="user@example.org",
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()
31 changes: 31 additions & 0 deletions tests/components/discovergy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@
"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(
Expand All @@ -50,3 +76,8 @@
"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},
)
28 changes: 28 additions & 0 deletions tests/components/discovergy/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,36 @@
'serial_number': '**REDACTED**',
'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({
'd81a652fe0824f9a9d336016587d3b9d': dict({
'time': '2023-03-10T07:32:06.702000',
'values': dict({
'actualityDuration': 52000.0,
'storageNumber': 0.0,
'volume': 21064800.0,
}),
}),
'f8d610b7a8cc4e73939fa33b990ded54': dict({
'time': '2023-03-10T07:32:06.702000',
'values': dict({
Expand Down
27 changes: 19 additions & 8 deletions tests/components/discovergy/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Test the Discovergy config flow."""
from unittest.mock import Mock, patch
from unittest.mock import AsyncMock, patch

from pydiscovergy.error import DiscovergyClientError, HTTPError, InvalidLogin
import pytest
Expand All @@ -14,7 +14,7 @@
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."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
Expand All @@ -25,7 +25,10 @@ async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None:
with patch(
"homeassistant.components.discovergy.async_setup_entry",
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(
result["flow_id"],
{
Expand All @@ -45,12 +48,14 @@ async def test_form(hass: HomeAssistant, mock_meters: Mock) -> None:


async def test_reauth(
hass: HomeAssistant, mock_meters: Mock, mock_config_entry: MockConfigEntry
hass: HomeAssistant, config_entry: MockConfigEntry, discovergy: AsyncMock
) -> None:
"""Test reauth flow."""
config_entry.add_to_hass(hass)

init_result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_REAUTH, "unique_id": mock_config_entry.unique_id},
context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id},
data=None,
)

Expand All @@ -60,7 +65,10 @@ async def test_reauth(
with patch(
"homeassistant.components.discovergy.async_setup_entry",
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(
init_result["flow_id"],
{
Expand Down Expand Up @@ -88,7 +96,7 @@ async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) ->
"""Test to handle exceptions."""

with patch(
"pydiscovergy.Discovergy.meters",
"homeassistant.components.discovergy.config_flow.pydiscovergy.Discovergy.meters",
side_effect=error,
):
result = await hass.config_entries.flow.async_init(
Expand All @@ -104,7 +112,10 @@ async def test_form_fail(hass: HomeAssistant, error: Exception, message: str) ->
assert result["step_id"] == "user"
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["flow_id"],
{
Expand Down
17 changes: 4 additions & 13 deletions tests/components/discovergy/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
"""Test Discovergy diagnostics."""
from unittest.mock import patch

import pytest
from syrupy import SnapshotAssertion

from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry
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


@pytest.mark.usefixtures("setup_integration")
async def test_entry_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test config entry diagnostics."""
with patch("pydiscovergy.Discovergy.meters", return_value=GET_METERS), patch(
"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
)
result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)

assert result == snapshot

0 comments on commit b4e8243

Please sign in to comment.