async def test_request_session(mocker): method = 'GET' url = 'http://somewhere.com' csess = MagicMock() csess.request = CoroutineMock(return_value=MockResponse()) csess.close = CoroutineMock() r = await yaar._request(method, url, session=csess) assert r.text == 'some text'
async def test_listen(self): conn = MagicMock(spec=websockets.client.WebSocketClientProtocol) conn.open = True conn.recv = asynctest.CoroutineMock(return_value=SLACK_MSG) conn.close = asynctest.CoroutineMock(return_value=True) websockets.connect = asynctest.CoroutineMock(return_value=True) async def side_effect(): conn.open = False websockets.connect.return_value = conn self.source._get_ws_url = asynctest.CoroutineMock( return_value="ws://example.com") self.source._inspect_msg = asynctest.CoroutineMock( return_value=SLACK_MSG, side_effect=["foo", "baz", side_effect()]) self.source._channel_id = "baz" cb = asynctest.CoroutineMock(return_value=True) res = await self.source.listen(cb) assert self.source._inspect_msg.call_count == 3 assert cb.call_count == 2 # fail with exception I self.source.websocket = "Test" websockets.connect = asynctest.CoroutineMock( side_effect=Exception("Test")) res = await self.source.listen(cb) assert res == True assert self.source.websocket == "Test" # fail with exception II websockets.connect = asynctest.CoroutineMock( side_effect=ConnectionRefusedError("ConnectionRefusedError")) res = await self.source.listen(cb) assert res == True assert self.source.websocket == "Test" # fail with exception III websockets.connect = asynctest.CoroutineMock( side_effect=websockets.exceptions.ConnectionClosed( code=1006, reason="Testing")) res = await self.source.listen(cb) assert res == True assert self.source.websocket == "Test"
async def test_close(self): websocket = AsyncMock() websocket.close = CoroutineMock() await close(websocket) websocket.close.assert_awaited_once()