Allow adding items Picnic shopping cart by searching (#102862)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Duco Sebel 2023-11-24 13:06:32 +01:00 committed by GitHub
parent adc56b6b67
commit 62473936e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 204 additions and 12 deletions

View file

@ -1,18 +1,31 @@
"""Tests for Picnic Tasks todo platform."""
from unittest.mock import MagicMock
from unittest.mock import MagicMock, Mock
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.todo import DOMAIN
from homeassistant.core import HomeAssistant
from .conftest import ENTITY_ID
from tests.common import MockConfigEntry
async def test_cart_list_with_items(hass: HomeAssistant, init_integration) -> None:
async def test_cart_list_with_items(
hass: HomeAssistant,
init_integration,
get_items,
snapshot: SnapshotAssertion,
) -> None:
"""Test loading of shopping cart."""
state = hass.states.get("todo.mock_title_shopping_cart")
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == "10"
assert snapshot == await get_items()
async def test_cart_list_empty_items(
hass: HomeAssistant, mock_picnic_api: MagicMock, mock_config_entry: MockConfigEntry
@ -23,7 +36,7 @@ async def test_cart_list_empty_items(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("todo.mock_title_shopping_cart")
state = hass.states.get(ENTITY_ID)
assert state
assert state.state == "0"
@ -37,7 +50,7 @@ async def test_cart_list_unexpected_response(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("todo.mock_title_shopping_cart")
state = hass.states.get(ENTITY_ID)
assert state is None
@ -50,5 +63,63 @@ async def test_cart_list_null_response(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("todo.mock_title_shopping_cart")
state = hass.states.get(ENTITY_ID)
assert state is None
async def test_create_todo_list_item(
hass: HomeAssistant, init_integration: MockConfigEntry, mock_picnic_api: MagicMock
) -> None:
"""Test for creating a picnic cart item."""
assert len(mock_picnic_api.get_cart.mock_calls) == 1
mock_picnic_api.search = Mock()
mock_picnic_api.search.return_value = [
{
"items": [
{
"id": 321,
"name": "Picnic Melk",
"unit_quantity": "2 liter",
}
]
}
]
mock_picnic_api.add_product = Mock()
await hass.services.async_call(
DOMAIN,
"add_item",
{"item": "Melk"},
target={"entity_id": ENTITY_ID},
blocking=True,
)
args = mock_picnic_api.search.call_args
assert args
assert args[0][0] == "Melk"
args = mock_picnic_api.add_product.call_args
assert args
assert args[0][0] == "321"
assert args[0][1] == 1
assert len(mock_picnic_api.get_cart.mock_calls) == 2
async def test_create_todo_list_item_not_found(
hass: HomeAssistant, init_integration: MockConfigEntry, mock_picnic_api: MagicMock
) -> None:
"""Test for creating a picnic cart item when ID is not found."""
mock_picnic_api.search = Mock()
mock_picnic_api.search.return_value = [{"items": []}]
with pytest.raises(ValueError):
await hass.services.async_call(
DOMAIN,
"add_item",
{"item": "Melk"},
target={"entity_id": ENTITY_ID},
blocking=True,
)