コード例 #1
0
async def test_data_sync(event_loop):
    """Test data and subscription with sync handlers."""
    async with aiohttp.ClientSession(loop=event_loop) as session:
        client = Client(TEST_API_KEY, TEST_APP_KEY, session=session)
        client.websocket._sio.eio._trigger_event = CoroutineMock()
        client.websocket._sio.eio.connect = CoroutineMock()
        client.websocket._sio.eio.disconnect = CoroutineMock()

        on_connect = MagicMock()
        on_data = MagicMock()
        on_disconnect = MagicMock()
        on_subscribed = MagicMock()

        client.websocket.on_connect(on_connect)
        client.websocket.on_data(on_data)
        client.websocket.on_disconnect(on_disconnect)
        client.websocket.on_subscribed(on_subscribed)

        await client.websocket.connect()
        client.websocket._sio.eio.connect.assert_called_once_with(
            f"https://api.ambientweather.net/?api=1&applicationKey={TEST_APP_KEY}",
            engineio_path="socket.io",
            headers={},
            transports=["websocket"],
        )

        await client.websocket._sio._trigger_event("connect", "/")
        on_connect.assert_called_once()

        await client.websocket._sio._trigger_event("data", "/", {"foo": "bar"})
        on_data.assert_called_once()

        await client.websocket._sio._trigger_event("subscribed", "/",
                                                   {"foo": "bar"})
        on_subscribed.assert_called()

        await client.websocket.disconnect()
        await client.websocket._sio._trigger_event("disconnect", "/")
        on_disconnect.assert_called_once()
        client.websocket._sio.eio.disconnect.assert_called_once_with(
            abort=True)