-
-
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.
- Loading branch information
Showing
6 changed files
with
139 additions
and
34 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 +1,26 @@ | ||
"""Test the Discovergy integration.""" | ||
from tests.components.discovergy.const import GET_METERS, LAST_READING, LAST_READING_GAS | ||
|
||
|
||
class MockDiscovergy: | ||
"""Mock Discovergy class.""" | ||
|
||
_thrown_error: Exception | None = None | ||
|
||
async def meters(self): | ||
"""Return mock meters.""" | ||
return GET_METERS | ||
|
||
async def meter_last_reading(self, meter_id: str): | ||
"""Return mock meter last reading.""" | ||
if self._thrown_error: | ||
raise self._thrown_error | ||
return ( | ||
LAST_READING_GAS | ||
if meter_id == "d81a652fe0824f9a9d336016587d3b9d" | ||
else LAST_READING | ||
) | ||
|
||
def set_thrown_exception(self, exception: Exception) -> None: | ||
"""Set thrown exception for testing purposes.""" | ||
self._thrown_error = exception |
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,50 @@ | ||
"""Fixtures for Discovergy integration tests.""" | ||
from unittest.mock import AsyncMock, Mock, patch | ||
from collections.abc import Callable, Coroutine | ||
from typing import Any | ||
from unittest.mock import patch | ||
|
||
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 import MockDiscovergy | ||
|
||
ComponentSetup = Callable[[], Coroutine[Any, Any, MockDiscovergy]] | ||
|
||
@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 | ||
|
||
|
||
@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 | ||
) -> Callable[[], Coroutine[Any, Any, MockDiscovergy]]: | ||
"""Fixture for setting up the component.""" | ||
config_entry.add_to_hass(hass) | ||
|
||
async def func() -> MockDiscovergy: | ||
mock = MockDiscovergy() | ||
with patch( | ||
"homeassistant.components.discovergy.coordinator.Discovergy", | ||
return_value=mock, | ||
), patch( | ||
"homeassistant.components.discovergy.pydiscovergy.Discovergy", | ||
return_value=mock, | ||
): | ||
assert await async_setup_component(hass, DOMAIN, {}) | ||
await hass.async_block_till_done() | ||
return mock | ||
|
||
return func |
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,25 @@ | ||
"""Test Discovergy diagnostics.""" | ||
from unittest.mock import patch | ||
|
||
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.components.discovergy.conftest import ComponentSetup | ||
from tests.typing import ClientSessionGenerator | ||
|
||
|
||
async def test_entry_diagnostics( | ||
hass: HomeAssistant, | ||
hass_client: ClientSessionGenerator, | ||
mock_config_entry: MockConfigEntry, | ||
config_entry: MockConfigEntry, | ||
setup_integration: ComponentSetup, | ||
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() | ||
await setup_integration() | ||
|
||
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 |