def test_upload_file_to_vk_error(self): class FakePost: def __init__(self, url, data): pass async def __aenter__(self): class FakeResponse: status = 200 async def text(self): return json.dumps({"error": "exists"}) return FakeResponse() async def __aexit__(self, exc_type, exc, tb): pass class Session: post = FakePost class Manager: session = Session() env = VKEnvironment(Manager()) result = self.loop.run_until_complete( env._upload_file_to_vk("nice_url", {"nice": "data"})) self.assertEqual(result, None)
def test_vk_upload_photo_w_peer_id(self): actions = [] env = VKEnvironment(self.get_fake_manager(actions), None) async def _upload_file_to_vk(self, url, data): return {"doc": "upload"} env.replace_method("_upload_file_to_vk", _upload_file_to_vk) attachment = self.loop.run_until_complete( env.upload_photo(b"file", peer_id=2)) self.assertEqual(attachment, "file") self.assertEqual(len(actions), 3) self.assertEqual(actions[0], ('photos.getMessagesUploadServer', { 'peer_id': 2 })) self.assertEqual(actions[1], ('photos.saveMessagesPhoto', { 'doc': 'upload' })) self.assertEqual(actions[2], ('attachment', 'photo'))
def test_vk_upload_doc(self): actions = [] env = VKEnvironment(self.get_fake_manager(actions), None) async def _upload_file_to_vk(self, url, data): return {"doc": "upload"} env.replace_method("_upload_file_to_vk", _upload_file_to_vk) attachment = self.loop.run_until_complete( env.upload_doc(b"file", "filename")) self.assertEqual(attachment, "file") self.assertEqual(len(actions), 3) self.assertEqual(actions[0], ('docs.getWallUploadServer', { 'group_id': 10, 'type': 'doc' })) self.assertEqual(actions[1], ('docs.save', {'doc': 'upload'})) self.assertEqual(actions[2], ('attachment', 'doc'))
def test_vk_conversation(self): class FakeManager(VKManager): def __init__(self): pass async def request(self, method, **kwargs): return VKResponse(False, (), {"object_id": 1}) env = VKEnvironment(FakeManager(), peer_id=0) loop = asyncio.get_event_loop() message = loop.run_until_complete( env.manager.create_message({ "object": { "date": 1, "random_id": 0, "fwd_messages": [], "important": False, "peer_id": 1, "text": "echo [club1|\u0421\u043e] 123", "attachments": [], "conversation_message_id": 1411, "out": 0, "from_id": 1, "id": 0, "is_hidden": False }, "group_id": 1, "type": "message_new" })) self.assertEqual(message.text, "echo 123") self.assertEqual(message.attachments, ()) message = loop.run_until_complete( env.manager.create_message({ "object": { "date": 1, "random_id": 0, "fwd_messages": [], "important": False, "peer_id": 1, "text": "echo [club1|\u0421\u043e] 123", "attachments": [], "conversation_message_id": 1411, "out": 0, "from_id": 1, "id": 0, "is_hidden": False }, "group_id": 2, "type": "message_new" })) self.assertEqual(message.text, "echo [club1|\u0421\u043e] 123") self.assertEqual(message.attachments, ())
async def test(): mngr.session = aiohttp.ClientSession() env = VKEnvironment(mngr) res = await env.get_file_from_attachment(None) self.assertEqual(res, None) res = await env.get_file_from_attachment( Attachment("photo", 13, 1, None, None, {})) self.assertEqual(res, None)
def test_concrete_environments(self): env1 = VKEnvironment("vk") env2 = TGEnvironment("tg") env1.replace_method("reply", print) env1.a = 10 child1 = env1.spawn() self.assertEqual(child1.a, 10) self.assertEqual(child1.reply.func, print) env2.replace_method("reply", print) env2.a = 10 child2 = env2.spawn() self.assertEqual(child2.a, 10) self.assertEqual(child2.reply.func, print)
def test_vk_no_peer_id(self): env = VKEnvironment(VKManager("token"), None) res = self.loop.run_until_complete(env.reply("message")) self.assertEqual(res, ())