Пример #1
0
    def test_interrupted_dump(self):
        """
        This method will ensure that all messages are retrieved even
        on weird conditions.
        """
        if not ALLOW_NETWORK:
            raise unittest.SkipTest('Network tests are disabled')

        dumper = Dumper(self.dumper_config)
        dumper.chunk_size = 1
        SEND, DUMP = True, False
        actions = (
            (3, SEND),
            (2, DUMP),
            (2, SEND),
            (2, DUMP),  # Actually one will be dumped then back to start
            (1, SEND),
            (2, DUMP),
            (1, SEND),
            (2, DUMP),  # Actually one will be saved and the other updated
            (2, SEND),
            (3, DUMP),
            (1, SEND),
            (1, DUMP),
            (1, DUMP),
        )

        self.client(functions.messages.DeleteHistoryRequest('me', 0))
        downloader = Downloader(self.client, self.dumper_config, dumper,
                                loop=asyncio.get_event_loop())

        which = 1
        for amount, what in actions:
            if what is SEND:
                print('Sending', amount, 'messages...')
                for _ in range(amount):
                    self.client.send_message('me', str(which))
                    which += 1
                    time.sleep(1)
            else:
                print('Dumping', amount, 'messages...')
                chunks = (amount + dumper.chunk_size - 1) // dumper.chunk_size
                dumper.max_chunks = chunks
                downloader.start('me')

        messages = self.client.get_message_history('me', limit=None)
        print('Full history')
        for msg in reversed(messages):
            print('ID:', msg.id, '; Message:', msg.message)

        print('Dumped history')
        fmt = BaseFormatter(dumper.conn)
        my_id = self.client.get_me().id
        dumped = list(fmt.get_messages_from_context(my_id, order='DESC'))
        for msg in dumped:
            print('ID:', msg.id, '; Message:', msg.text)

        print('Asserting dumped history matches...')
        assert len(messages) == len(dumped), 'Not all messages were dumped'
        assert all(a.id == b.id and a.message == b.text
                   for a, b in zip(messages, dumped)),\
            'Dumped messages do not match'

        print('All good! Test passed!')
        self.client.disconnect()