Complete test coverage for Tankerkonig (#115920)

* complete tests

* update snapshots after rebase
This commit is contained in:
Michael 2024-04-23 23:42:17 +02:00 committed by GitHub
parent 72ed16c3e0
commit 35db2e4101
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 262 additions and 33 deletions

View File

@ -1408,11 +1408,6 @@ omit =
homeassistant/components/tado/water_heater.py
homeassistant/components/tami4/button.py
homeassistant/components/tank_utility/sensor.py
homeassistant/components/tankerkoenig/__init__.py
homeassistant/components/tankerkoenig/binary_sensor.py
homeassistant/components/tankerkoenig/coordinator.py
homeassistant/components/tankerkoenig/entity.py
homeassistant/components/tankerkoenig/sensor.py
homeassistant/components/tapsaff/binary_sensor.py
homeassistant/components/tautulli/__init__.py
homeassistant/components/tautulli/coordinator.py

View File

@ -6,20 +6,11 @@ from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.tankerkoenig import DOMAIN
from homeassistant.components.tankerkoenig.const import CONF_FUEL_TYPES, CONF_STATIONS
from homeassistant.const import (
CONF_API_KEY,
CONF_LATITUDE,
CONF_LOCATION,
CONF_LONGITUDE,
CONF_NAME,
CONF_RADIUS,
CONF_SHOW_ON_MAP,
)
from homeassistant.const import CONF_SHOW_ON_MAP
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .const import NEARBY_STATIONS, PRICES, STATION
from .const import CONFIG_DATA, NEARBY_STATIONS, PRICES, STATION
from tests.common import MockConfigEntry
@ -55,16 +46,7 @@ async def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
options={
CONF_SHOW_ON_MAP: True,
},
data={
CONF_NAME: "Home",
CONF_API_KEY: "269534f6-xxxx-xxxx-xxxx-yyyyzzzzxxxx",
CONF_FUEL_TYPES: ["e5"],
CONF_LOCATION: {CONF_LATITUDE: 51.0, CONF_LONGITUDE: 13.0},
CONF_RADIUS: 2.0,
CONF_STATIONS: [
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
],
},
data=CONFIG_DATA,
)

View File

@ -2,6 +2,16 @@
from aiotankerkoenig import PriceInfo, Station, Status
from homeassistant.components.tankerkoenig.const import CONF_FUEL_TYPES, CONF_STATIONS
from homeassistant.const import (
CONF_API_KEY,
CONF_LATITUDE,
CONF_LOCATION,
CONF_LONGITUDE,
CONF_NAME,
CONF_RADIUS,
)
NEARBY_STATIONS = [
Station(
id="3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
@ -49,6 +59,25 @@ STATION = Station(
state="xxXX",
)
STATION_MISSING_FUELTYPE = Station(
id="3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
name="Station ABC",
brand="Station",
street="Somewhere Street",
house_number="1",
post_code=1234,
place="Somewhere",
opening_times=[],
overrides=[],
whole_day=True,
is_open=True,
e5=1.719,
e10=1.659,
lat=51.1,
lng=13.1,
state="xxXX",
)
PRICES = {
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8": PriceInfo(
status=Status.OPEN,
@ -57,3 +86,22 @@ PRICES = {
diesel=1.659,
),
}
PRICES_MISSING_FUELTYPE = {
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8": PriceInfo(
status=Status.OPEN,
e5=1.719,
e10=1.659,
),
}
CONFIG_DATA = {
CONF_NAME: "Home",
CONF_API_KEY: "269534f6-xxxx-xxxx-xxxx-yyyyzzzzxxxx",
CONF_FUEL_TYPES: ["e5"],
CONF_LOCATION: {CONF_LATITUDE: 51.0, CONF_LONGITUDE: 13.0},
CONF_RADIUS: 2.0,
CONF_STATIONS: [
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
],
}

View File

@ -0,0 +1,9 @@
# serializer version: 1
# name: test_binary_sensor
ReadOnlyDict({
'device_class': 'door',
'friendly_name': 'Station Somewhere Street 1 Status',
'latitude': 51.1,
'longitude': 13.1,
})
# ---

View File

@ -0,0 +1,52 @@
# serializer version: 1
# name: test_sensor
ReadOnlyDict({
'attribution': 'Data provided by https://www.tankerkoenig.de',
'brand': 'Station',
'city': 'Somewhere',
'friendly_name': 'Station Somewhere Street 1 Super E10',
'fuel_type': <GasType.E10: 'e10'>,
'house_number': '1',
'latitude': 51.1,
'longitude': 13.1,
'postcode': 1234,
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'station_name': 'Station ABC',
'street': 'Somewhere Street',
'unit_of_measurement': '€',
})
# ---
# name: test_sensor.1
ReadOnlyDict({
'attribution': 'Data provided by https://www.tankerkoenig.de',
'brand': 'Station',
'city': 'Somewhere',
'friendly_name': 'Station Somewhere Street 1 Super',
'fuel_type': <GasType.E5: 'e5'>,
'house_number': '1',
'latitude': 51.1,
'longitude': 13.1,
'postcode': 1234,
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'station_name': 'Station ABC',
'street': 'Somewhere Street',
'unit_of_measurement': '€',
})
# ---
# name: test_sensor.2
ReadOnlyDict({
'attribution': 'Data provided by https://www.tankerkoenig.de',
'brand': 'Station',
'city': 'Somewhere',
'friendly_name': 'Station Somewhere Street 1 Diesel',
'fuel_type': <GasType.DIESEL: 'diesel'>,
'house_number': '1',
'latitude': 51.1,
'longitude': 13.1,
'postcode': 1234,
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'station_name': 'Station ABC',
'street': 'Somewhere Street',
'unit_of_measurement': '€',
})
# ---

View File

@ -0,0 +1,25 @@
"""Tests for the Tankerkoening integration."""
from __future__ import annotations
import pytest
from syrupy import SnapshotAssertion
from homeassistant.const import STATE_ON
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("setup_integration")
async def test_binary_sensor(
hass: HomeAssistant,
config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test the tankerkoenig binary sensors."""
state = hass.states.get("binary_sensor.station_somewhere_street_1_status")
assert state
assert state.state == STATE_ON
assert state.attributes == snapshot

View File

@ -1,6 +1,6 @@
"""Tests for Tankerkoenig config flow."""
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
from aiotankerkoenig.exceptions import TankerkoenigInvalidKeyError
@ -21,6 +21,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.setup import async_setup_component
from .const import NEARBY_STATIONS
@ -208,7 +209,7 @@ async def test_reauth(hass: HomeAssistant, config_entry: MockConfigEntry) -> Non
assert entry.data[CONF_API_KEY] == "269534f6-aaaa-bbbb-cccc-yyyyzzzzxxxx"
async def test_options_flow(hass: HomeAssistant) -> None:
async def test_options_flow(hass: HomeAssistant, tankerkoenig: AsyncMock) -> None:
"""Test options flow."""
mock_config = MockConfigEntry(
@ -218,10 +219,17 @@ async def test_options_flow(hass: HomeAssistant) -> None:
unique_id=f"{DOMAIN}_{MOCK_USER_DATA[CONF_LOCATION][CONF_LATITUDE]}_{MOCK_USER_DATA[CONF_LOCATION][CONF_LONGITUDE]}",
)
mock_config.add_to_hass(hass)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
with patch(
"homeassistant.components.tankerkoenig.config_flow.Tankerkoenig.nearby_stations",
return_value=NEARBY_STATIONS,
with (
patch(
"homeassistant.components.tankerkoenig.config_flow.Tankerkoenig.nearby_stations",
return_value=NEARBY_STATIONS,
),
patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as mock_async_reload,
):
result = await hass.config_entries.options.async_init(mock_config.entry_id)
assert result["type"] is FlowResultType.FORM
@ -237,6 +245,10 @@ async def test_options_flow(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.CREATE_ENTRY
assert not mock_config.options[CONF_SHOW_ON_MAP]
await hass.async_block_till_done()
assert mock_async_reload.call_count == 1
async def test_options_flow_error(hass: HomeAssistant) -> None:
"""Test options flow."""

View File

@ -15,14 +15,20 @@ import pytest
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.tankerkoenig.const import DEFAULT_SCAN_INTERVAL, DOMAIN
from homeassistant.components.tankerkoenig.const import (
CONF_STATIONS,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
)
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_ID, STATE_UNAVAILABLE
from homeassistant.const import ATTR_ID, CONF_SHOW_ON_MAP, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from .const import CONFIG_DATA
from tests.common import MockConfigEntry, async_fire_time_changed
@ -190,3 +196,38 @@ async def test_automatic_registry_cleanup(
len(dr.async_entries_for_config_entry(device_registry, config_entry.entry_id))
== 1
)
async def test_many_stations_warning(
hass: HomeAssistant, tankerkoenig: AsyncMock, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the warning about morethan 10 selected stations."""
mock_config = MockConfigEntry(
domain=DOMAIN,
data={
**CONFIG_DATA,
CONF_STATIONS: [
"3bcd61da-xxxx-xxxx-xxxx-19d5523a7ae8",
"36b4b812-xxxx-xxxx-xxxx-c51735325858",
"54e2b642-xxxx-xxxx-xxxx-87cd4e9867f1",
"11b5c130-xxxx-xxxx-xxxx-856b8489b528",
"a9137924-xxxx-xxxx-xxxx-7029d7eb073f",
"57c6d275-xxxx-xxxx-xxxx-7f6ad9e6d638",
"bbc3c3a2-xxxx-xxxx-xxxx-840cc3d496b6",
"1db63dd9-xxxx-xxxx-xxxx-a889b53cbc65",
"18d7262e-xxxx-xxxx-xxxx-4a61ad302e14",
"a8041aa3-xxxx-xxxx-xxxx-7c6b180e5a40",
"739aa0eb-xxxx-xxxx-xxxx-a3d7b6c8a42f",
"9ad9fb26-xxxx-xxxx-xxxx-84e6a02b3096",
"74267867-xxxx-xxxx-xxxx-74ce3d45882c",
"86657222-xxxx-xxxx-xxxx-a2b795ab3cf9",
],
},
options={CONF_SHOW_ON_MAP: True},
unique_id="51.0_13.0",
)
mock_config.add_to_hass(hass)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
assert "Found more than 10 stations to check" in caplog.text

View File

@ -0,0 +1,65 @@
"""Tests for the Tankerkoening integration."""
from __future__ import annotations
from unittest.mock import AsyncMock
import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.tankerkoenig import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .const import PRICES_MISSING_FUELTYPE, STATION_MISSING_FUELTYPE
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("setup_integration")
async def test_sensor(
hass: HomeAssistant,
tankerkoenig: AsyncMock,
config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test the tankerkoenig sensors."""
state = hass.states.get("sensor.station_somewhere_street_1_super_e10")
assert state
assert state.state == "1.659"
assert state.attributes == snapshot
state = hass.states.get("sensor.station_somewhere_street_1_super")
assert state
assert state.state == "1.719"
assert state.attributes == snapshot
state = hass.states.get("sensor.station_somewhere_street_1_diesel")
assert state
assert state.state == "1.659"
assert state.attributes == snapshot
async def test_sensor_missing_fueltype(
hass: HomeAssistant,
tankerkoenig: AsyncMock,
config_entry: MockConfigEntry,
) -> None:
"""Test the tankerkoenig sensors."""
tankerkoenig.station_details.return_value = STATION_MISSING_FUELTYPE
tankerkoenig.prices.return_value = PRICES_MISSING_FUELTYPE
config_entry.add_to_hass(hass)
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
state = hass.states.get("sensor.station_somewhere_street_1_super_e10")
assert state
state = hass.states.get("sensor.station_somewhere_street_1_super")
assert state
state = hass.states.get("sensor.station_somewhere_street_1_diesel")
assert not state