core/tests/components/mealie/test_calendar.py

88 lines
2.7 KiB
Python
Raw Normal View History

2024-06-21 09:04:55 +00:00
"""Tests for the Mealie calendar."""
from datetime import date
from http import HTTPStatus
from unittest.mock import AsyncMock, patch
2024-06-21 09:04:55 +00:00
2024-12-20 22:45:54 +00:00
from aiomealie import MealplanResponse
2024-06-21 09:04:55 +00:00
from syrupy.assertion import SnapshotAssertion
2024-12-20 22:45:54 +00:00
from homeassistant.const import STATE_OFF, Platform
2024-06-21 09:04:55 +00:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import setup_integration
from tests.common import MockConfigEntry, snapshot_platform
from tests.typing import ClientSessionGenerator
async def test_api_calendar(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_mealie_client: AsyncMock,
mock_config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
) -> None:
"""Test the API returns the calendar."""
await setup_integration(hass, mock_config_entry)
client = await hass_client()
response = await client.get("/api/calendars")
assert response.status == HTTPStatus.OK
data = await response.json()
assert data == snapshot
async def test_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_mealie_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
2024-12-20 22:45:54 +00:00
"""Test the calendar entities."""
with patch("homeassistant.components.mealie.PLATFORMS", [Platform.CALENDAR]):
await setup_integration(hass, mock_config_entry)
2024-06-21 09:04:55 +00:00
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
2024-12-20 22:45:54 +00:00
async def test_no_meal_planned(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_mealie_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the calendar handles no meal planned."""
mock_mealie_client.get_mealplans.return_value = MealplanResponse([])
await setup_integration(hass, mock_config_entry)
assert hass.states.get("calendar.mealie_dinner").state == STATE_OFF
2024-06-21 09:04:55 +00:00
async def test_api_events(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_mealie_client: AsyncMock,
mock_config_entry: MockConfigEntry,
hass_client: ClientSessionGenerator,
) -> None:
"""Test the Mealie calendar view."""
await setup_integration(hass, mock_config_entry)
client = await hass_client()
response = await client.get(
"/api/calendars/calendar.mealie_dinner?start=2023-08-01&end=2023-11-01"
)
assert mock_mealie_client.get_mealplans.called == 1
assert mock_mealie_client.get_mealplans.call_args_list[1].args == (
date(2023, 8, 1),
date(2023, 11, 1),
)
assert response.status == HTTPStatus.OK
events = await response.json()
assert events == snapshot