Use send_json_auto_id in todo tests (#104245)

* Use send_json_auto_id in todo tests

* Update tests
This commit is contained in:
Erik Montnemery 2023-11-20 18:13:37 +01:00 committed by GitHub
parent 9c5e0fc2c9
commit cd5595a130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 78 deletions

View file

@ -63,39 +63,22 @@ def platforms() -> list[str]:
return [Platform.TODO]
@pytest.fixture
def ws_req_id() -> Callable[[], int]:
"""Fixture for incremental websocket requests."""
id = 0
def next_id() -> int:
nonlocal id
id += 1
return id
return next_id
@pytest.fixture
async def ws_get_items(
hass_ws_client: WebSocketGenerator, ws_req_id: Callable[[], int]
hass_ws_client: WebSocketGenerator,
) -> Callable[[], Awaitable[dict[str, str]]]:
"""Fixture to fetch items from the todo websocket."""
async def get() -> list[dict[str, str]]:
# Fetch items using To-do platform
client = await hass_ws_client()
id = ws_req_id()
await client.send_json(
await client.send_json_auto_id(
{
"id": id,
"type": "todo/item/list",
"entity_id": ENTITY_ID,
}
)
resp = await client.receive_json()
assert resp.get("id") == id
assert resp.get("success")
return resp.get("result", {}).get("items", [])

View file

@ -13,39 +13,22 @@ from .conftest import TEST_ENTITY
from tests.typing import WebSocketGenerator
@pytest.fixture
def ws_req_id() -> Callable[[], int]:
"""Fixture for incremental websocket requests."""
id = 0
def next() -> int:
nonlocal id
id += 1
return id
return next
@pytest.fixture
async def ws_get_items(
hass_ws_client: WebSocketGenerator, ws_req_id: Callable[[], int]
hass_ws_client: WebSocketGenerator,
) -> Callable[[], Awaitable[dict[str, str]]]:
"""Fixture to fetch items from the todo websocket."""
async def get() -> list[dict[str, str]]:
# Fetch items using To-do platform
client = await hass_ws_client()
id = ws_req_id()
await client.send_json(
await client.send_json_auto_id(
{
"id": id,
"type": "todo/item/list",
"entity_id": TEST_ENTITY,
}
)
resp = await client.receive_json()
assert resp.get("id") == id
assert resp.get("success")
return resp.get("result", {}).get("items", [])
@ -55,25 +38,21 @@ async def ws_get_items(
@pytest.fixture
async def ws_move_item(
hass_ws_client: WebSocketGenerator,
ws_req_id: Callable[[], int],
) -> Callable[[str, str | None], Awaitable[None]]:
"""Fixture to move an item in the todo list."""
async def move(uid: str, previous_uid: str | None) -> None:
# Fetch items using To-do platform
client = await hass_ws_client()
id = ws_req_id()
data = {
"id": id,
"type": "todo/item/move",
"entity_id": TEST_ENTITY,
"uid": uid,
}
if previous_uid is not None:
data["previous_uid"] = previous_uid
await client.send_json(data)
await client.send_json_auto_id(data)
resp = await client.receive_json()
assert resp.get("id") == id
assert resp.get("success")
return move

View file

@ -13,39 +13,22 @@ from tests.typing import WebSocketGenerator
TEST_ENTITY = "todo.shopping_list"
@pytest.fixture
def ws_req_id() -> Callable[[], int]:
"""Fixture for incremental websocket requests."""
id = 0
def next() -> int:
nonlocal id
id += 1
return id
return next
@pytest.fixture
async def ws_get_items(
hass_ws_client: WebSocketGenerator, ws_req_id: Callable[[], int]
hass_ws_client: WebSocketGenerator,
) -> Callable[[], Awaitable[dict[str, str]]]:
"""Fixture to fetch items from the todo websocket."""
async def get() -> list[dict[str, str]]:
# Fetch items using To-do platform
client = await hass_ws_client()
id = ws_req_id()
await client.send_json(
await client.send_json_auto_id(
{
"id": id,
"type": "todo/item/list",
"entity_id": TEST_ENTITY,
}
)
resp = await client.receive_json()
assert resp.get("id") == id
assert resp.get("success")
return resp.get("result", {}).get("items", [])
@ -55,25 +38,21 @@ async def ws_get_items(
@pytest.fixture
async def ws_move_item(
hass_ws_client: WebSocketGenerator,
ws_req_id: Callable[[], int],
) -> Callable[[str, str | None], Awaitable[None]]:
"""Fixture to move an item in the todo list."""
async def move(uid: str, previous_uid: str | None) -> dict[str, Any]:
# Fetch items using To-do platform
client = await hass_ws_client()
id = ws_req_id()
data = {
"id": id,
"type": "todo/item/move",
"entity_id": TEST_ENTITY,
"uid": uid,
}
if previous_uid is not None:
data["previous_uid"] = previous_uid
await client.send_json(data)
await client.send_json_auto_id(data)
resp = await client.receive_json()
assert resp.get("id") == id
return resp
return move
@ -83,7 +62,6 @@ async def test_get_items(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test creating a shopping list item with the WS API and verifying with To-do API."""
@ -94,9 +72,7 @@ async def test_get_items(
assert state.state == "0"
# Native shopping list websocket
await client.send_json(
{"id": ws_req_id(), "type": "shopping_list/items/add", "name": "soda"}
)
await client.send_json_auto_id({"type": "shopping_list/items/add", "name": "soda"})
msg = await client.receive_json()
assert msg["success"] is True
data = msg["result"]
@ -117,7 +93,6 @@ async def test_get_items(
async def test_add_item(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test adding shopping_list item and listing it."""
@ -145,7 +120,6 @@ async def test_add_item(
async def test_remove_item(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test removing a todo item."""
@ -187,7 +161,6 @@ async def test_remove_item(
async def test_bulk_remove(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test removing a todo item."""
@ -232,7 +205,6 @@ async def test_bulk_remove(
async def test_update_item(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test updating a todo item."""
@ -286,7 +258,6 @@ async def test_update_item(
async def test_partial_update_item(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test updating a todo item with partial information."""
@ -363,7 +334,6 @@ async def test_partial_update_item(
async def test_update_invalid_item(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
) -> None:
"""Test updating a todo item that does not exist."""
@ -410,7 +380,6 @@ async def test_update_invalid_item(
async def test_move_item(
hass: HomeAssistant,
sl_setup: None,
ws_req_id: Callable[[], int],
ws_get_items: Callable[[], Awaitable[dict[str, str]]],
ws_move_item: Callable[[str, str | None], Awaitable[dict[str, Any]]],
src_idx: int,