def test_upload_attachment_error_retry( mock_make_attachment, mock_upload_file_to_vk, mock_request, ): mock_request.side_effect = [ {"upload_url": "_"}, RequestException(None, None, None, {"error_code": 1}), {"upload_url": "_"}, "ok", ] mock_upload_file_to_vk.side_effect = [ "ok", "ok", ] mock_make_attachment.return_value = "ok" vkontakte = VkontakteLongpoll("token") result = asyncio.get_event_loop().run_until_complete( vkontakte.upload_attachment(Attachment.new(b""), peer_id=123) ) assert result == "ok"
def test_upload_attachment_error_no_retry( mock_upload_file_to_vk, mock_request, ): mock_request.side_effect = [ {"upload_url": "_"}, RequestException(None, None, None, {"error_code": 1}), ] mock_upload_file_to_vk.side_effect = [ "ok", ] vkontakte = VkontakteLongpoll("token") with pytest.raises(RequestException): asyncio.get_event_loop().run_until_complete( vkontakte.upload_attachment(Attachment.new(b"")) )
def test_execute_send_new(): vkontakte = VkontakteLongpoll(token="token") async def _upl_att(attachment, peer_id): return attachment._replace(id=1, raw={"ok": "ok"}) vkontakte.upload_attachment = _upl_att async def req(method, kwargs): assert method == "messages.send" assert kwargs["attachment"] == "1" return 1 vkontakte._request = req attachment = Attachment.new(b"content", "image") result = asyncio.get_event_loop().run_until_complete( vkontakte.execute_send(1, "text", attachment, {}) ) assert result == 1