Modernize uptime tests (#88636)

* Modernize uptime tests

* Fix tests
pull/89158/head
Franck Nijhof 2023-02-24 04:15:20 +01:00 committed by GitHub
parent af49b98475
commit 2f826a6f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 20 deletions

View File

@ -0,0 +1,34 @@
# serializer version: 1
# name: test_full_user_flow
FlowResultSnapshot({
'context': dict({
'source': 'user',
}),
'data': dict({
}),
'description': None,
'description_placeholders': None,
'flow_id': <ANY>,
'handler': 'uptime',
'options': dict({
}),
'result': ConfigEntrySnapshot({
'data': dict({
}),
'disabled_by': None,
'domain': 'uptime',
'entry_id': <ANY>,
'options': dict({
}),
'pref_disable_new_entities': False,
'pref_disable_polling': False,
'source': 'user',
'title': 'Uptime',
'unique_id': None,
'version': 1,
}),
'title': 'Uptime',
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>,
'version': 1,
})
# ---

View File

@ -0,0 +1,85 @@
# serializer version: 1
# name: test_uptime_sensor
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'Uptime',
}),
'context': <ANY>,
'entity_id': 'sensor.uptime',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': '2022-03-01T00:00:00+00:00',
})
# ---
# name: test_uptime_sensor.1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.uptime',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
'original_icon': None,
'original_name': None,
'platform': 'uptime',
'supported_features': 0,
'translation_key': None,
'unit_of_measurement': None,
})
# ---
# name: test_uptime_sensor.2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'Uptime',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---
# name: test_uptime_sensor.3
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entries': <ANY>,
'configuration_url': None,
'connections': set({
}),
'disabled_by': None,
'entry_type': <DeviceEntryType.SERVICE: 'service'>,
'hw_version': None,
'id': <ANY>,
'is_new': False,
'manufacturer': None,
'model': None,
'name': 'Uptime',
'name_by_user': None,
'suggested_area': None,
'sw_version': None,
'via_device_id': None,
})
# ---

View File

@ -1,5 +1,7 @@
"""Tests for the Uptime config flow."""
from unittest.mock import MagicMock
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.uptime.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER
@ -9,9 +11,10 @@ from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_setup_entry")
async def test_full_user_flow(
hass: HomeAssistant,
mock_setup_entry: MagicMock,
snapshot: SnapshotAssertion,
) -> None:
"""Test the full user configuration flow."""
result = await hass.config_entries.flow.async_init(
@ -27,8 +30,7 @@ async def test_full_user_flow(
)
assert result2.get("type") == FlowResultType.CREATE_ENTRY
assert result2.get("title") == "Uptime"
assert result2.get("data") == {}
assert result2 == snapshot
async def test_single_instance_allowed(

View File

@ -1,36 +1,35 @@
"""The tests for the uptime sensor platform."""
import pytest
from syrupy.assertion import SnapshotAssertion
from syrupy.filters import props
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.uptime.const import DOMAIN
from homeassistant.const import ATTR_DEVICE_CLASS
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("init_integration")
@pytest.mark.freeze_time("2022-03-01 00:00:00+00:00")
async def test_uptime_sensor(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
init_integration: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test Uptime sensor."""
state = hass.states.get("sensor.uptime")
assert state
assert (state := hass.states.get("sensor.uptime"))
assert state.state == "2022-03-01T00:00:00+00:00"
assert state.attributes["friendly_name"] == "Uptime"
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TIMESTAMP
assert state == snapshot
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.uptime")
assert entry
assert entry.unique_id == init_integration.entry_id
assert (entity_entry := entity_registry.async_get(state.entity_id))
assert entity_entry == snapshot(exclude=props("unique_id"))
assert entity_entry.unique_id == init_integration.entry_id
device_registry = dr.async_get(hass)
assert entry.device_id
device_entry = device_registry.async_get(entry.device_id)
assert device_entry
assert entity_entry.device_id
assert (device_entry := device_registry.async_get(entity_entry.device_id))
assert device_entry == snapshot(exclude=props("identifiers"))
assert device_entry.identifiers == {(DOMAIN, init_integration.entry_id)}
assert device_entry.name == init_integration.title
assert device_entry.entry_type == dr.DeviceEntryType.SERVICE