From cd5595a1304a8cfc3c01b29f369162b054413b8b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 20 Nov 2023 18:13:37 +0100 Subject: [PATCH] Use send_json_auto_id in todo tests (#104245) * Use send_json_auto_id in todo tests * Update tests --- tests/components/google_tasks/test_todo.py | 21 ++--------- tests/components/local_todo/test_todo.py | 27 ++------------ tests/components/shopping_list/test_todo.py | 39 +++------------------ 3 files changed, 9 insertions(+), 78 deletions(-) diff --git a/tests/components/google_tasks/test_todo.py b/tests/components/google_tasks/test_todo.py index 7b11372f1d4..70309e64222 100644 --- a/tests/components/google_tasks/test_todo.py +++ b/tests/components/google_tasks/test_todo.py @@ -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", []) diff --git a/tests/components/local_todo/test_todo.py b/tests/components/local_todo/test_todo.py index 5747e05ad05..c6246be3dad 100644 --- a/tests/components/local_todo/test_todo.py +++ b/tests/components/local_todo/test_todo.py @@ -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 diff --git a/tests/components/shopping_list/test_todo.py b/tests/components/shopping_list/test_todo.py index 681ccea60ac..7c13344ad1d 100644 --- a/tests/components/shopping_list/test_todo.py +++ b/tests/components/shopping_list/test_todo.py @@ -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,