def test_vk_exec_loop(self): mngr = VKManager("token") mngr.running = True async def raw_request(_, method, **kwargs): self.assertEqual(method, "execute") self.assertEqual(kwargs["code"], "return [API.fake.method({}),];") mngr.running = False return VKResponse(False, (), {"response": ["response"]}) mngr.raw_request = types.MethodType(raw_request, mngr) req = VKRequest("fake.method", {}) mngr.requests_queue.append(req) tasks = [] def ensure(task): _task = asyncio.ensure_future(task, loop=self.loop) tasks.append(_task) return _task self.loop.run_until_complete(mngr._exec_loop()) self.loop.run_until_complete(asyncio.gather(*tasks)) response = self.loop.run_until_complete(req) self.assertFalse(response.error) self.assertEqual(response.response, "response")
def test_vk_exec_loop_error(self): mngr = VKManager("token") mngr.running = True async def raw_request(_, method, **kwargs): self.assertEqual(method, "execute") self.assertEqual( kwargs["code"], 'return [API.users.get({"user_ids": "sdfsdfsdfsdfsdf"}),];') mngr.running = False raw_response = { "response": [False], "execute_errors": [{ "method": "users.get", "error_code": 113, "error_msg": "Invalid user id" }, { "method": "execute", "error_code": 113, "error_msg": "Invalid user id" }] } return VKResponse( True, (("VK_req", raw_response.get("error", "")), ("VK_exe", raw_response.get("execute_errors", ""))), raw_response.get("response", "")) mngr.raw_request = types.MethodType(raw_request, mngr) req = VKRequest("users.get", {"user_ids": "sdfsdfsdfsdfsdf"}) mngr.requests_queue.append(req) logging.disable(logging.ERROR) tasks = [] def ensure(task): _task = asyncio.ensure_future(task, loop=self.loop) tasks.append(_task) return _task self.loop.run_until_complete(mngr._exec_loop()) self.loop.run_until_complete(asyncio.gather(*tasks)) logging.disable(logging.INFO) response = self.loop.run_until_complete(req) self.assertTrue(response.error) self.assertEqual(response.errors[0][0], "VK_req") self.assertEqual(response.response, "")
def test_vk_update_longpoll_data_exception(self): mngr = VKManager("token") async def raw_request(self, method, **kwargs): return VKResponse(True, (), "") mngr.raw_request = types.MethodType(raw_request, mngr) with self.assertRaises(ValueError): self.loop.run_until_complete(mngr.update_longpoll_data()) self.loop.run_until_complete(mngr.dispose()) self.assertEqual(mngr.longpoll, None)
def test_vk_update_longpoll_data(self): mngr = VKManager("token") async def raw_request(self, method, **kwargs): return VKResponse(False, (), { "key": "key", "server": "server", "ts": "0" }) mngr.raw_request = types.MethodType(raw_request, mngr) self.loop.run_until_complete(mngr.update_longpoll_data()) self.loop.run_until_complete(mngr.dispose()) self.assertEqual(mngr.longpoll, { "key": "key", "server": "server", "ts": "0" })
def test_vk_get_receiver_coroutine_function(self): mngr = VKManager("token") async def raw_request(self, method, **kwargs): return VKResponse(False, (), [{"id": 1, "name": "keks"}]) mngr.raw_request = types.MethodType(raw_request, mngr) async def update_longpoll_data(self): self.longpoll = "updated" mngr.update_longpoll_data = types.MethodType(update_longpoll_data, mngr) receiver = self.loop.run_until_complete( mngr.get_receiver_coroutine_function()) self.assertEqual(mngr.longpoll, "updated") self.assertEqual(mngr.receiver, receiver) self.assertEqual(mngr.group_id, 1) self.loop.run_until_complete(mngr.dispose())