Beispiel #1
0
async def test_data_async(event_loop):
    """Test data and subscription with async 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 = AsyncMock()
        client.websocket._sio.eio.connect = AsyncMock()
        client.websocket._sio.eio.disconnect = AsyncMock()

        async_on_connect = AsyncMock()
        async_on_data = AsyncMock()
        async_on_disconnect = AsyncMock()
        async_on_subscribed = AsyncMock()

        client.websocket.async_on_connect(async_on_connect)
        client.websocket.async_on_data(async_on_data)
        client.websocket.async_on_disconnect(async_on_disconnect)
        client.websocket.async_on_subscribed(async_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", "/")
        async_on_connect.assert_called_once()

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

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

        await client.websocket.disconnect()
        await client.websocket._sio._trigger_event("disconnect", "/")
        async_on_disconnect.assert_called_once()
        client.websocket._sio.eio.disconnect.assert_called_once_with(abort=True)
Beispiel #2
0
async def test_data_async():
    """Test data and subscription with async handlers."""
    websocket = Websocket(TEST_API_KEY, TEST_APP_KEY)
    websocket._sio.connect = AsyncMock()
    websocket._sio.disconnect = AsyncMock()
    websocket._sio.eio._trigger_event = AsyncMock()
    websocket._sio.namespaces = {"/": 1}

    async_on_connect = AsyncMock()
    async_on_data = AsyncMock()
    async_on_disconnect = AsyncMock()
    async_on_subscribed = AsyncMock()

    websocket.async_on_connect(async_on_connect)
    websocket.async_on_data(async_on_data)
    websocket.async_on_disconnect(async_on_disconnect)
    websocket.async_on_subscribed(async_on_subscribed)

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

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

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

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

    await websocket.disconnect()
    await websocket._sio._trigger_event("disconnect", "/")
    async_on_disconnect.assert_called_once()
    websocket._sio.disconnect.assert_called_once()