2024-07-07 19:19:20 +00:00
|
|
|
"""Tests for the Mealie services."""
|
|
|
|
|
|
|
|
from datetime import date
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
2024-07-21 14:43:46 +00:00
|
|
|
from aiomealie import (
|
|
|
|
MealieConnectionError,
|
|
|
|
MealieNotFoundError,
|
|
|
|
MealieValidationError,
|
|
|
|
MealplanEntryType,
|
|
|
|
)
|
2024-07-07 19:19:20 +00:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
|
|
|
import pytest
|
|
|
|
from syrupy import SnapshotAssertion
|
|
|
|
|
|
|
|
from homeassistant.components.mealie.const import (
|
|
|
|
ATTR_CONFIG_ENTRY_ID,
|
|
|
|
ATTR_END_DATE,
|
2024-07-21 14:43:46 +00:00
|
|
|
ATTR_ENTRY_TYPE,
|
2024-07-10 12:33:17 +00:00
|
|
|
ATTR_INCLUDE_TAGS,
|
2024-07-08 15:11:35 +00:00
|
|
|
ATTR_RECIPE_ID,
|
2024-07-07 19:19:20 +00:00
|
|
|
ATTR_START_DATE,
|
2024-07-10 12:33:17 +00:00
|
|
|
ATTR_URL,
|
2024-07-07 19:19:20 +00:00
|
|
|
DOMAIN,
|
|
|
|
)
|
2024-07-08 15:11:35 +00:00
|
|
|
from homeassistant.components.mealie.services import (
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
SERVICE_GET_RECIPE,
|
2024-07-10 12:33:17 +00:00
|
|
|
SERVICE_IMPORT_RECIPE,
|
2024-07-21 14:43:46 +00:00
|
|
|
SERVICE_SET_RANDOM_MEALPLAN,
|
2024-07-08 15:11:35 +00:00
|
|
|
)
|
2024-07-21 14:43:46 +00:00
|
|
|
from homeassistant.const import ATTR_DATE
|
2024-07-07 19:19:20 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-07-09 09:55:12 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
2024-07-07 19:19:20 +00:00
|
|
|
|
|
|
|
from . import setup_integration
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
|
async def test_service_mealplan(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
freezer: FrozenDateTimeFactory,
|
|
|
|
) -> None:
|
|
|
|
"""Test the get_mealplan service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
freezer.move_to("2023-10-21")
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert mock_mealie_client.get_mealplans.call_args_list[1][0] == (
|
|
|
|
date(2023, 10, 21),
|
|
|
|
date(2023, 10, 21),
|
|
|
|
)
|
|
|
|
assert response == snapshot
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
2024-07-12 13:21:48 +00:00
|
|
|
ATTR_START_DATE: "2023-10-22",
|
|
|
|
ATTR_END_DATE: "2023-10-25",
|
2024-07-07 19:19:20 +00:00
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert response
|
|
|
|
assert mock_mealie_client.get_mealplans.call_args_list[2][0] == (
|
|
|
|
date(2023, 10, 22),
|
|
|
|
date(2023, 10, 25),
|
|
|
|
)
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
2024-07-12 13:21:48 +00:00
|
|
|
ATTR_START_DATE: "2023-10-19",
|
2024-07-07 19:19:20 +00:00
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert response
|
|
|
|
assert mock_mealie_client.get_mealplans.call_args_list[3][0] == (
|
|
|
|
date(2023, 10, 19),
|
|
|
|
date(2023, 10, 21),
|
|
|
|
)
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
2024-07-12 13:21:48 +00:00
|
|
|
ATTR_END_DATE: "2023-10-22",
|
2024-07-07 19:19:20 +00:00
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert response
|
|
|
|
assert mock_mealie_client.get_mealplans.call_args_list[4][0] == (
|
|
|
|
date(2023, 10, 21),
|
|
|
|
date(2023, 10, 22),
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ServiceValidationError):
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
2024-07-12 13:21:48 +00:00
|
|
|
ATTR_START_DATE: "2023-10-22",
|
|
|
|
ATTR_END_DATE: "2023-10-19",
|
2024-07-07 19:19:20 +00:00
|
|
|
},
|
|
|
|
blocking=True,
|
2024-07-08 15:11:35 +00:00
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_service_recipe(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
) -> None:
|
|
|
|
"""Test the get_recipe service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_RECIPE,
|
|
|
|
{ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id, ATTR_RECIPE_ID: "recipe_id"},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert response == snapshot
|
|
|
|
|
|
|
|
|
2024-07-10 12:33:17 +00:00
|
|
|
async def test_service_import_recipe(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
) -> None:
|
|
|
|
"""Test the import_recipe service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_IMPORT_RECIPE,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_URL: "http://example.com",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert response == snapshot
|
|
|
|
mock_mealie_client.import_recipe.assert_called_with(
|
|
|
|
"http://example.com", include_tags=False
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_IMPORT_RECIPE,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_URL: "http://example.com",
|
|
|
|
ATTR_INCLUDE_TAGS: True,
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=False,
|
|
|
|
)
|
|
|
|
mock_mealie_client.import_recipe.assert_called_with(
|
|
|
|
"http://example.com", include_tags=True
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-09 09:55:12 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("exception", "raised_exception"),
|
|
|
|
[
|
|
|
|
(MealieNotFoundError, ServiceValidationError),
|
|
|
|
(MealieConnectionError, HomeAssistantError),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_service_recipe_exceptions(
|
2024-07-08 15:11:35 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
2024-07-09 09:55:12 +00:00
|
|
|
exception: Exception,
|
|
|
|
raised_exception: type[Exception],
|
2024-07-08 15:11:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Test the get_recipe service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
2024-07-09 09:55:12 +00:00
|
|
|
mock_mealie_client.get_recipe.side_effect = exception
|
2024-07-08 15:11:35 +00:00
|
|
|
|
2024-07-09 09:55:12 +00:00
|
|
|
with pytest.raises(raised_exception):
|
2024-07-08 15:11:35 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_RECIPE,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_RECIPE_ID: "recipe_id",
|
|
|
|
},
|
|
|
|
blocking=True,
|
2024-07-09 09:55:12 +00:00
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-10 12:33:17 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("exception", "raised_exception"),
|
|
|
|
[
|
|
|
|
(MealieValidationError, ServiceValidationError),
|
|
|
|
(MealieConnectionError, HomeAssistantError),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_service_import_recipe_exceptions(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
exception: Exception,
|
|
|
|
raised_exception: type[Exception],
|
|
|
|
) -> None:
|
|
|
|
"""Test the exceptions of the import_recipe service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
mock_mealie_client.import_recipe.side_effect = exception
|
|
|
|
|
|
|
|
with pytest.raises(raised_exception):
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_IMPORT_RECIPE,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_URL: "http://example.com",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-21 14:43:46 +00:00
|
|
|
async def test_service_set_random_mealplan(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
snapshot: SnapshotAssertion,
|
|
|
|
) -> None:
|
|
|
|
"""Test the set_random_mealplan service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
response = await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_RANDOM_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_DATE: "2023-10-21",
|
|
|
|
ATTR_ENTRY_TYPE: "lunch",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
assert response == snapshot
|
|
|
|
mock_mealie_client.random_mealplan.assert_called_with(
|
|
|
|
date(2023, 10, 21), MealplanEntryType.LUNCH
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_mealie_client.random_mealplan.reset_mock()
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_RANDOM_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_DATE: "2023-10-21",
|
|
|
|
ATTR_ENTRY_TYPE: "lunch",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=False,
|
|
|
|
)
|
|
|
|
mock_mealie_client.random_mealplan.assert_called_with(
|
|
|
|
date(2023, 10, 21), MealplanEntryType.LUNCH
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_service_set_random_mealplan_exceptions(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Test the exceptions of the set_random_mealplan service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
mock_mealie_client.random_mealplan.side_effect = MealieConnectionError
|
|
|
|
|
|
|
|
with pytest.raises(HomeAssistantError, match="Error connecting to Mealie instance"):
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_RANDOM_MEALPLAN,
|
|
|
|
{
|
|
|
|
ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id,
|
|
|
|
ATTR_DATE: "2023-10-21",
|
|
|
|
ATTR_ENTRY_TYPE: "lunch",
|
|
|
|
},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-09 09:55:12 +00:00
|
|
|
async def test_service_mealplan_connection_error(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_mealie_client: AsyncMock,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Test a connection error in the get_mealplans service."""
|
|
|
|
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
|
|
|
|
mock_mealie_client.get_mealplans.side_effect = MealieConnectionError
|
|
|
|
|
|
|
|
with pytest.raises(HomeAssistantError):
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{ATTR_CONFIG_ENTRY_ID: mock_config_entry.entry_id},
|
|
|
|
blocking=True,
|
2024-07-07 19:19:20 +00:00
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_service_mealplan_without_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_config_entry: MockConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Test the get_mealplan service without entry."""
|
|
|
|
mock_config_entry.add_to_hass(hass)
|
|
|
|
mock_config_entry2 = MockConfigEntry(domain=DOMAIN)
|
|
|
|
mock_config_entry2.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
with pytest.raises(ServiceValidationError):
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{ATTR_CONFIG_ENTRY_ID: mock_config_entry2.entry_id},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ServiceValidationError):
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_GET_MEALPLAN,
|
|
|
|
{ATTR_CONFIG_ENTRY_ID: "bad-config_id"},
|
|
|
|
blocking=True,
|
|
|
|
return_response=True,
|
|
|
|
)
|