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] 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 @pytest.fixture
async def ws_get_items( async def ws_get_items(
hass_ws_client: WebSocketGenerator, ws_req_id: Callable[[], int] hass_ws_client: WebSocketGenerator,
) -> Callable[[], Awaitable[dict[str, str]]]: ) -> Callable[[], Awaitable[dict[str, str]]]:
"""Fixture to fetch items from the todo websocket.""" """Fixture to fetch items from the todo websocket."""
async def get() -> list[dict[str, str]]: async def get() -> list[dict[str, str]]:
# Fetch items using To-do platform # Fetch items using To-do platform
client = await hass_ws_client() client = await hass_ws_client()
id = ws_req_id() await client.send_json_auto_id(
await client.send_json(
{ {
"id": id,
"type": "todo/item/list", "type": "todo/item/list",
"entity_id": ENTITY_ID, "entity_id": ENTITY_ID,
} }
) )
resp = await client.receive_json() resp = await client.receive_json()
assert resp.get("id") == id
assert resp.get("success") assert resp.get("success")
return resp.get("result", {}).get("items", []) return resp.get("result", {}).get("items", [])

View File

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

View File

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