Пример #1
0
    def test_vk_receiver(self):
        mngr = VKManager("token")

        async def prepare():
            mngr.longpoll = {"ts": 0, "server": "server", "key": "key"}

            class FakeSession:
                def post(self, url):
                    class FakePost:
                        def __init__(self, url):
                            pass

                        async def __aenter__(self):
                            class FakeResponse:
                                async def json(self):
                                    return {
                                        "ts":
                                        "4",
                                        "updates": [{
                                            "type": "type",
                                            "object": "object"
                                        }, "update2"]
                                    }

                            return FakeResponse()

                        async def __aexit__(self, exc_type, exc, tb):
                            pass

                    return FakePost(url)

                async def close(self):
                    pass

            mngr.session = FakeSession()

        self.loop.run_until_complete(prepare())

        updates = self.loop.run_until_complete(mngr.receiver())

        self.assertEqual(updates, [{"type": "type", "object": "object"}])
        self.assertEqual(mngr.longpoll["ts"], "4")

        self.loop.run_until_complete(mngr.dispose())
Пример #2
0
    def test_vk_receiver_failed(self):
        mngr = VKManager("token")

        async def prepare():
            mngr.longpoll = {"ts": 0, "server": "server", "key": "key"}

            class FakeSession:
                def post(self, url):
                    class FakePost:
                        def __init__(self, url):
                            pass

                        async def __aenter__(self):
                            class FakeResponse:
                                async def json(self):
                                    return {"failed": 2}

                            return FakeResponse()

                        async def __aexit__(self, exc_type, exc, tb):
                            pass

                    return FakePost(url)

                async def close(self):
                    pass

            mngr.session = FakeSession()

            async def update_longpoll_data(self):
                self.longpoll = "updated"

            mngr.update_longpoll_data = types.MethodType(
                update_longpoll_data, mngr)

        self.loop.run_until_complete(prepare())

        updates = self.loop.run_until_complete(mngr.receiver())

        self.assertEqual(updates, ())
        self.assertEqual(mngr.longpoll, "updated")

        self.loop.run_until_complete(mngr.dispose())