mirror of
https://github.com/Electric-Special/ha-core.git
synced 2026-03-21 04:05:20 +01:00
Improve tests in Bring! integration (#162853)
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
"""Test todo entity notification action of the Bring! integration."""
|
"""Test todo entity notification action of the Bring! integration."""
|
||||||
|
|
||||||
import re
|
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from bring_api import BringNotificationType, BringRequestException
|
from bring_api import BringNotificationType, BringRequestException
|
||||||
@@ -15,7 +14,7 @@ from homeassistant.components.bring.services import (
|
|||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@@ -63,10 +62,7 @@ async def test_send_notification_exception(
|
|||||||
|
|
||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
mock_bring_client.notify.side_effect = BringRequestException
|
mock_bring_client.notify.side_effect = BringRequestException
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError,
|
|
||||||
match="Failed to send push notification for Bring! due to a connection error, try again later",
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_PUSH_NOTIFICATION,
|
SERVICE_PUSH_NOTIFICATION,
|
||||||
@@ -76,6 +72,7 @@ async def test_send_notification_exception(
|
|||||||
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "notify_request_failed"
|
||||||
|
|
||||||
|
|
||||||
async def test_send_notification_service_validation_error(
|
async def test_send_notification_service_validation_error(
|
||||||
@@ -91,12 +88,7 @@ async def test_send_notification_service_validation_error(
|
|||||||
|
|
||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
mock_bring_client.notify.side_effect = ValueError
|
mock_bring_client.notify.side_effect = ValueError
|
||||||
with pytest.raises(
|
with pytest.raises(ServiceValidationError) as err:
|
||||||
HomeAssistantError,
|
|
||||||
match=re.escape(
|
|
||||||
"This action requires field item, please enter a valid value for item"
|
|
||||||
),
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_PUSH_NOTIFICATION,
|
SERVICE_PUSH_NOTIFICATION,
|
||||||
@@ -104,3 +96,5 @@ async def test_send_notification_service_validation_error(
|
|||||||
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "notify_missing_argument"
|
||||||
|
assert err.value.translation_placeholders == {"field": "item"}
|
||||||
|
|||||||
@@ -82,10 +82,7 @@ async def test_send_reaction_exception(
|
|||||||
|
|
||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
mock_bring_client.notify.side_effect = BringRequestException
|
mock_bring_client.notify.side_effect = BringRequestException
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError,
|
|
||||||
match="Failed to send reaction for Bring! due to a connection error, try again later",
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_ACTIVITY_STREAM_REACTION,
|
SERVICE_ACTIVITY_STREAM_REACTION,
|
||||||
@@ -95,6 +92,7 @@ async def test_send_reaction_exception(
|
|||||||
},
|
},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "reaction_request_failed"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_bring_client")
|
@pytest.mark.usefixtures("mock_bring_client")
|
||||||
@@ -143,10 +141,7 @@ async def test_send_reaction_unknown_entity(
|
|||||||
entity_registry.async_update_entity(
|
entity_registry.async_update_entity(
|
||||||
"event.einkauf_activities", disabled_by=er.RegistryEntryDisabler.USER
|
"event.einkauf_activities", disabled_by=er.RegistryEntryDisabler.USER
|
||||||
)
|
)
|
||||||
with pytest.raises(
|
with pytest.raises(ServiceValidationError) as err:
|
||||||
ServiceValidationError,
|
|
||||||
match="Failed to send reaction for Bring! — Unknown entity event.einkauf_activities",
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_ACTIVITY_STREAM_REACTION,
|
SERVICE_ACTIVITY_STREAM_REACTION,
|
||||||
@@ -157,6 +152,11 @@ async def test_send_reaction_unknown_entity(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert err.value.translation_key == "entity_not_found"
|
||||||
|
assert err.value.translation_placeholders == {
|
||||||
|
"entity_id": "event.einkauf_activities"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_send_reaction_not_found(
|
async def test_send_reaction_not_found(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@@ -174,10 +174,7 @@ async def test_send_reaction_not_found(
|
|||||||
|
|
||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError,
|
|
||||||
match="Failed to send reaction for Bring! — No recent activity found",
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_ACTIVITY_STREAM_REACTION,
|
SERVICE_ACTIVITY_STREAM_REACTION,
|
||||||
@@ -187,3 +184,4 @@ async def test_send_reaction_not_found(
|
|||||||
},
|
},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "activity_not_found"
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
"""Test for todo platform of the Bring! integration."""
|
"""Test for todo platform of the Bring! integration."""
|
||||||
|
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
import re
|
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
from bring_api import BringItemOperation, BringItemsResponse, BringRequestException
|
from bring_api import BringItemOperation, BringItemsResponse, BringRequestException
|
||||||
@@ -107,9 +106,7 @@ async def test_add_item_exception(
|
|||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
mock_bring_client.save_item.side_effect = BringRequestException
|
mock_bring_client.save_item.side_effect = BringRequestException
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError, match="Failed to save item Äpfel to Bring! list"
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
TODO_DOMAIN,
|
TODO_DOMAIN,
|
||||||
TodoServices.ADD_ITEM,
|
TodoServices.ADD_ITEM,
|
||||||
@@ -117,6 +114,8 @@ async def test_add_item_exception(
|
|||||||
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "todo_save_item_failed"
|
||||||
|
assert err.value.translation_placeholders == {"name": "Äpfel"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_uuid")
|
@pytest.mark.usefixtures("mock_uuid")
|
||||||
@@ -170,9 +169,7 @@ async def test_update_item_exception(
|
|||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
mock_bring_client.batch_update_list.side_effect = BringRequestException
|
mock_bring_client.batch_update_list.side_effect = BringRequestException
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError, match="Failed to update item Paprika to Bring! list"
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
TODO_DOMAIN,
|
TODO_DOMAIN,
|
||||||
TodoServices.UPDATE_ITEM,
|
TodoServices.UPDATE_ITEM,
|
||||||
@@ -184,6 +181,8 @@ async def test_update_item_exception(
|
|||||||
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "todo_update_item_failed"
|
||||||
|
assert err.value.translation_placeholders == {"name": "Paprika"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_uuid")
|
@pytest.mark.usefixtures("mock_uuid")
|
||||||
@@ -245,9 +244,7 @@ async def test_rename_item_exception(
|
|||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
mock_bring_client.batch_update_list.side_effect = BringRequestException
|
mock_bring_client.batch_update_list.side_effect = BringRequestException
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError, match="Failed to rename item Gurke to Bring! list"
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
TODO_DOMAIN,
|
TODO_DOMAIN,
|
||||||
TodoServices.UPDATE_ITEM,
|
TodoServices.UPDATE_ITEM,
|
||||||
@@ -259,6 +256,8 @@ async def test_rename_item_exception(
|
|||||||
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
assert err.value.translation_key == "todo_rename_item_failed"
|
||||||
|
assert err.value.translation_placeholders == {"name": "Gurke"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_uuid")
|
@pytest.mark.usefixtures("mock_uuid")
|
||||||
@@ -309,10 +308,7 @@ async def test_delete_items_exception(
|
|||||||
|
|
||||||
assert bring_config_entry.state is ConfigEntryState.LOADED
|
assert bring_config_entry.state is ConfigEntryState.LOADED
|
||||||
mock_bring_client.batch_update_list.side_effect = BringRequestException
|
mock_bring_client.batch_update_list.side_effect = BringRequestException
|
||||||
with pytest.raises(
|
with pytest.raises(HomeAssistantError) as err:
|
||||||
HomeAssistantError,
|
|
||||||
match=re.escape("Failed to delete 1 item(s) from Bring! list"),
|
|
||||||
):
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
TODO_DOMAIN,
|
TODO_DOMAIN,
|
||||||
TodoServices.REMOVE_ITEM,
|
TodoServices.REMOVE_ITEM,
|
||||||
@@ -320,3 +316,6 @@ async def test_delete_items_exception(
|
|||||||
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
target={ATTR_ENTITY_ID: "todo.einkauf"},
|
||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert err.value.translation_key == "todo_delete_item_failed"
|
||||||
|
assert err.value.translation_placeholders == {"count": "1"}
|
||||||
|
|||||||
Reference in New Issue
Block a user