Debian Codegraph @

tests/test_search.py

raw
1 """Unit tests for search."""
2
3 # pylint: disable=protected-access
4
5 from unittest.mock import AsyncMock
6
7 import pytest
8
9 from async_upnp_client.search import SsdpSearchListener
10 from async_upnp_client.ssdp import SSDP_IP_V4
11 from async_upnp_client.utils import CaseInsensitiveDict
12
13 from .common import (
14 ADVERTISEMENT_HEADERS_DEFAULT,
15 ADVERTISEMENT_REQUEST_LINE,
16 SEARCH_HEADERS_DEFAULT,
17 SEARCH_REQUEST_LINE,
18 )
19
20
21 @pytest.mark.asyncio
22 async def test_receive_search_response() -> None:
23 """Test handling a ssdp search response."""
24 # pylint: disable=protected-access
25 async_callback = AsyncMock()
26 listener = SsdpSearchListener(async_callback=async_callback)
27 headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT)
28 listener._on_data(SEARCH_REQUEST_LINE, headers)
29
30 async_callback.assert_called_with(headers)
31
32
33 @pytest.mark.asyncio
34 async def test_create_ssdp_listener_with_alternate_target() -> None:
35 """Create a SsdpSearchListener on an alternate target."""
36 async_callback = AsyncMock()
37 async_connect_callback = AsyncMock()
38
39 yeelight_target = (SSDP_IP_V4, 1982)
40 yeelight_service_type = "wifi_bulb"
41 listener = SsdpSearchListener(
42 async_callback=async_callback,
43 async_connect_callback=async_connect_callback,
44 search_target=yeelight_service_type,
45 target=yeelight_target,
46 )
47
48 assert listener.source == ("0.0.0.0", 0)
49 assert listener.target == yeelight_target
50 assert listener.search_target == yeelight_service_type
51 assert listener.async_callback == async_callback
52 assert listener.async_connect_callback == async_connect_callback
53
54
55 @pytest.mark.asyncio
56 async def test_receive_ssdp_alive_advertisement() -> None:
57 """Test handling a ssdp alive advertisement, which is ignored."""
58 async_callback = AsyncMock()
59 listener = SsdpSearchListener(async_callback=async_callback)
60 headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT)
61 listener._on_data(ADVERTISEMENT_REQUEST_LINE, headers)
62
63 async_callback.assert_not_called()