Esempio n. 1
0
    def test_disconnect(self):
        close_mock = MagicMock()
        self.outlet._client = MagicMock(close=close_mock)
        self.outlet._db = MagicMock()
        self.outlet.disconnect()

        close_mock.assert_called_once()
        self.assertIsNone(self.outlet._client)
        self.assertIsNone(self.outlet._db)
Esempio n. 2
0
    async def test_connect(self):
        wsaddr = "host"

        mock_jsonrpc = MagicMock()
        mock_getwsaddr = MagicMock(return_value=wsaddr)
        mock_jsonrpc.get_websocket_address = mock_getwsaddr
        self._client._jsonrpc = mock_jsonrpc

        mock_ws = MagicMock()
        mock_connect = CoroutineMock()
        mock_ws.connect = mock_connect
        self._client._ws = mock_ws

        await self._client.connect()

        mock_getwsaddr.assert_called_once()
        mock_connect.assert_awaited_once_with(wsaddr)
Esempio n. 3
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)