-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests for discovergy (#103667)
- Loading branch information
Showing
5 changed files
with
120 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |