From c264ee22e7df4d8634b72ac8c782fc742ce01c5c Mon Sep 17 00:00:00 2001 From: Manu <4445816+tr4nt0r@users.noreply.github.com> Date: Tue, 29 Oct 2024 14:08:05 +0100 Subject: [PATCH] Add tests for switch platform of Habitica integration (#128204) --- .../habitica/snapshots/test_switch.ambr | 48 ++++++ tests/components/habitica/test_switch.py | 138 ++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 tests/components/habitica/snapshots/test_switch.ambr create mode 100644 tests/components/habitica/test_switch.py diff --git a/tests/components/habitica/snapshots/test_switch.ambr b/tests/components/habitica/snapshots/test_switch.ambr new file mode 100644 index 00000000000..3affbd11e2a --- /dev/null +++ b/tests/components/habitica/snapshots/test_switch.ambr @@ -0,0 +1,48 @@ +# serializer version: 1 +# name: test_switch[switch.test_user_rest_in_the_inn-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.test_user_rest_in_the_inn', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Rest in the inn', + 'platform': 'habitica', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': , + 'unique_id': '00000000-0000-0000-0000-000000000000_sleep', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch[switch.test_user_rest_in_the_inn-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'switch', + 'friendly_name': 'test-user Rest in the inn', + }), + 'context': , + 'entity_id': 'switch.test_user_rest_in_the_inn', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/habitica/test_switch.py b/tests/components/habitica/test_switch.py new file mode 100644 index 00000000000..55ba7b19b22 --- /dev/null +++ b/tests/components/habitica/test_switch.py @@ -0,0 +1,138 @@ +"""Tests for the Habitica switch platform.""" + +from collections.abc import Generator +from http import HTTPStatus +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.habitica.const import DEFAULT_URL +from homeassistant.components.switch import ( + DOMAIN as SWITCH_DOMAIN, + SERVICE_TOGGLE, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import entity_registry as er + +from .conftest import mock_called_with + +from tests.common import MockConfigEntry, snapshot_platform +from tests.test_util.aiohttp import AiohttpClientMocker + + +@pytest.fixture(autouse=True) +def switch_only() -> Generator[None]: + """Enable only the switch platform.""" + with patch( + "homeassistant.components.habitica.PLATFORMS", + [Platform.SWITCH], + ): + yield + + +@pytest.mark.usefixtures("mock_habitica") +async def test_switch( + hass: HomeAssistant, + config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, +) -> None: + """Test switch entities.""" + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) + + +@pytest.mark.parametrize( + ("service_call"), + [ + SERVICE_TURN_ON, + SERVICE_TURN_OFF, + SERVICE_TOGGLE, + ], +) +async def test_turn_on_off_toggle( + hass: HomeAssistant, + config_entry: MockConfigEntry, + service_call: str, + mock_habitica: AiohttpClientMocker, +) -> None: + """Test switch turn on/off, toggle method.""" + + mock_habitica.post( + f"{DEFAULT_URL}/api/v3/user/sleep", + json={"success": True, "data": False}, + ) + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + await hass.services.async_call( + SWITCH_DOMAIN, + service_call, + {ATTR_ENTITY_ID: "switch.test_user_rest_in_the_inn"}, + blocking=True, + ) + + assert mock_called_with(mock_habitica, "post", f"{DEFAULT_URL}/api/v3/user/sleep") + + +@pytest.mark.parametrize( + ("service_call"), + [ + SERVICE_TURN_ON, + SERVICE_TURN_OFF, + SERVICE_TOGGLE, + ], +) +@pytest.mark.parametrize( + ("status_code", "exception"), + [ + (HTTPStatus.TOO_MANY_REQUESTS, ServiceValidationError), + (HTTPStatus.BAD_REQUEST, HomeAssistantError), + ], +) +async def test_turn_on_off_toggle_exceptions( + hass: HomeAssistant, + config_entry: MockConfigEntry, + service_call: str, + mock_habitica: AiohttpClientMocker, + status_code: HTTPStatus, + exception: Exception, +) -> None: + """Test switch turn on/off, toggle method.""" + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + mock_habitica.post( + f"{DEFAULT_URL}/api/v3/user/sleep", + status=status_code, + json={"success": True, "data": False}, + ) + + with pytest.raises(expected_exception=exception): + await hass.services.async_call( + SWITCH_DOMAIN, + service_call, + {ATTR_ENTITY_ID: "switch.test_user_rest_in_the_inn"}, + blocking=True, + ) + + assert mock_called_with(mock_habitica, "post", f"{DEFAULT_URL}/api/v3/user/sleep")