Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Linear tests #116336

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/components/linear_garage_door/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
"""Tests for the Linear Garage Door integration."""

from unittest.mock import patch

from homeassistant.const import Platform
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


async def setup_integration(
hass: HomeAssistant, config_entry: MockConfigEntry, platforms: list[Platform]
) -> None:
"""Fixture for setting up the component."""
config_entry.add_to_hass(hass)

with patch(
"homeassistant.components.linear_garage_door.PLATFORMS",
platforms,
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
67 changes: 67 additions & 0 deletions tests/components/linear_garage_door/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Common fixtures for the Linear Garage Door tests."""

from collections.abc import Generator
from unittest.mock import AsyncMock, patch

import pytest

from homeassistant.components.linear_garage_door import DOMAIN
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD

from tests.common import (
MockConfigEntry,
load_json_array_fixture,
load_json_object_fixture,
)


@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Override async_setup_entry."""
with patch(
"homeassistant.components.linear_garage_door.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry


@pytest.fixture
def mock_linear() -> Generator[AsyncMock, None, None]:
"""Mock a Linear Garage Door client."""
with (
patch(
"homeassistant.components.linear_garage_door.coordinator.Linear",
autospec=True,
) as mock_client,
patch(
"homeassistant.components.linear_garage_door.config_flow.Linear",
new=mock_client,
),
):
client = mock_client.return_value
client.login.return_value = True
client.get_devices.return_value = load_json_array_fixture(
"get_devices.json", DOMAIN
)
client.get_sites.return_value = load_json_array_fixture(
"get_sites.json", DOMAIN
)
device_states = load_json_object_fixture("get_device_state.json", DOMAIN)
client.get_device_state.side_effect = lambda device_id: device_states[device_id]
yield client


@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Mock a config entry."""
return MockConfigEntry(
domain=DOMAIN,
entry_id="acefdd4b3a4a0911067d1cf51414201e",
title="test-site-name",
data={
CONF_EMAIL: "test-email",
CONF_PASSWORD: "test-password",
"site_id": "test-site-id",
"device_id": "test-uuid",
},
)
42 changes: 42 additions & 0 deletions tests/components/linear_garage_door/fixtures/get_device_state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"test1": {
"GDO": {
"Open_B": "true",
"Open_P": "100"
},
"Light": {
"On_B": "true",
"On_P": "100"
}
},
"test2": {
"GDO": {
"Open_B": "false",
"Open_P": "0"
},
"Light": {
"On_B": "false",
"On_P": "0"
}
},
"test3": {
"GDO": {
"Open_B": "false",
"Opening_P": "0"
},
"Light": {
"On_B": "false",
"On_P": "0"
}
},
"test4": {
"GDO": {
"Open_B": "true",
"Opening_P": "100"
},
"Light": {
"On_B": "true",
"On_P": "100"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"test1": {
"GDO": {
"Open_B": "true",
"Opening_P": "100"
},
"Light": {
"On_B": "true",
"On_P": "100"
}
},
"test2": {
"GDO": {
"Open_B": "false",
"Opening_P": "0"
},
"Light": {
"On_B": "false",
"On_P": "0"
}
},
"test3": {
"GDO": {
"Open_B": "false",
"Opening_P": "0"
},
"Light": {
"On_B": "false",
"On_P": "0"
}
},
"test4": {
"GDO": {
"Open_B": "true",
"Opening_P": "100"
},
"Light": {
"On_B": "true",
"On_P": "100"
}
}
}
22 changes: 22 additions & 0 deletions tests/components/linear_garage_door/fixtures/get_devices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"id": "test1",
"name": "Test Garage 1",
"subdevices": ["GDO", "Light"]
},
{
"id": "test2",
"name": "Test Garage 2",
"subdevices": ["GDO", "Light"]
},
{
"id": "test3",
"name": "Test Garage 3",
"subdevices": ["GDO", "Light"]
},
{
"id": "test4",
"name": "Test Garage 4",
"subdevices": ["GDO", "Light"]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{ "id": "test-site-id", "name": "test-site-name" }]
193 changes: 193 additions & 0 deletions tests/components/linear_garage_door/snapshots/test_cover.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# serializer version: 1
# name: test_covers[cover.test_garage_1-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.test_garage_1',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <CoverDeviceClass.GARAGE: 'garage'>,
'original_icon': None,
'original_name': None,
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 3>,
'translation_key': None,
'unique_id': 'test1-GDO',
'unit_of_measurement': None,
})
# ---
# name: test_covers[cover.test_garage_1-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'garage',
'friendly_name': 'Test Garage 1',
'supported_features': <CoverEntityFeature: 3>,
}),
'context': <ANY>,
'entity_id': 'cover.test_garage_1',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'open',
})
# ---
# name: test_covers[cover.test_garage_2-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.test_garage_2',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <CoverDeviceClass.GARAGE: 'garage'>,
'original_icon': None,
'original_name': None,
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 3>,
'translation_key': None,
'unique_id': 'test2-GDO',
'unit_of_measurement': None,
})
# ---
# name: test_covers[cover.test_garage_2-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'garage',
'friendly_name': 'Test Garage 2',
'supported_features': <CoverEntityFeature: 3>,
}),
'context': <ANY>,
'entity_id': 'cover.test_garage_2',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'closed',
})
# ---
# name: test_covers[cover.test_garage_3-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.test_garage_3',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <CoverDeviceClass.GARAGE: 'garage'>,
'original_icon': None,
'original_name': None,
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 3>,
'translation_key': None,
'unique_id': 'test3-GDO',
'unit_of_measurement': None,
})
# ---
# name: test_covers[cover.test_garage_3-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'garage',
'friendly_name': 'Test Garage 3',
'supported_features': <CoverEntityFeature: 3>,
}),
'context': <ANY>,
'entity_id': 'cover.test_garage_3',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'opening',
})
# ---
# name: test_covers[cover.test_garage_4-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'cover',
'entity_category': None,
'entity_id': 'cover.test_garage_4',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <CoverDeviceClass.GARAGE: 'garage'>,
'original_icon': None,
'original_name': None,
'platform': 'linear_garage_door',
'previous_unique_id': None,
'supported_features': <CoverEntityFeature: 3>,
'translation_key': None,
'unique_id': 'test4-GDO',
'unit_of_measurement': None,
})
# ---
# name: test_covers[cover.test_garage_4-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'garage',
'friendly_name': 'Test Garage 4',
'supported_features': <CoverEntityFeature: 3>,
}),
'context': <ANY>,
'entity_id': 'cover.test_garage_4',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'closing',
})
# ---