예제 #1
0
    def test_formatter_get_chat(self):
        """
        Ensures that the BaseFormatter is able to fetch the expected
        entities when using a date parameter.
        """
        chat = types.Chat(
            id=123,
            title='Some title',
            photo=types.ChatPhotoEmpty(),
            participants_count=7,
            date=datetime.now(),
            version=1
        )
        dumper = Dumper(self.dumper_config)

        fmt = BaseFormatter(dumper.conn)
        for month in range(1, 13):
            await dumper.dump_chat(chat, None, timestamp=int(datetime(
                year=2010, month=month, day=1
            ).timestamp()))
        dumper.commit()
        cid = tl_utils.get_peer_id(chat)
        # Default should get the most recent version
        date = fmt.get_chat(cid).date_updated
        assert date == datetime(year=2010, month=12, day=1)

        # Expected behaviour is to get the previous available date
        target = datetime(year=2010, month=6, day=29)
        date = fmt.get_chat(cid, target).date_updated
        assert date == datetime(year=2010, month=6, day=1)

        # Expected behaviour is to get the next date if previous unavailable
        target = datetime(year=2009, month=12, day=1)
        date = fmt.get_chat(cid, target).date_updated
        assert date == datetime(year=2010, month=1, day=1)
예제 #2
0
    def test_dump_msg_entities(self):
        """Show that entities are correctly parsed and stored"""
        message = types.Message(
            id=1,
            to_id=types.PeerUser(321),
            date=datetime.now(),
            message='No entities'
        )
        dumper = Dumper(self.dumper_config)
        fmt = BaseFormatter(dumper.conn)

        # Test with no entities
        await dumper.dump_message(message, 123, None, None)
        dumper.commit()
        assert not next(fmt.get_messages_from_context(123, order='DESC')).formatting

        # Test with many entities
        text, entities = markdown.parse(
            'Testing message with __italic__, **bold**, inline '
            '[links](https://example.com) and [mentions](@hi), '
            'as well as `code` and ``pre`` blocks.'
        )
        entities[3] = types.MessageEntityMentionName(
            entities[3].offset, entities[3].length, 123
        )
        message.id = 2
        message.date -= timedelta(days=1)
        message.message = text
        message.entities = entities
        await dumper.dump_message(message, 123, None, None)
        dumper.commit()
        msg = next(fmt.get_messages_from_context(123, order='ASC'))
        assert utils.decode_msg_entities(msg.formatting) == message.entities
예제 #3
0
    def test_formatter_get_messages(self):
        """
        Ensures that the BaseFormatter is able to correctly yield messages.
        """
        dumper = Dumper(self.dumper_config)
        msg = types.Message(
            id=1,
            to_id=123,
            date=datetime(year=2010, month=1, day=1),
            message='hi'
        )
        for _ in range(365):
            await dumper.dump_message(msg, 123, forward_id=None, media_id=None)
            msg.id += 1
            msg.date += timedelta(days=1)
            msg.to_id = 300 - msg.to_id  # Flip between two IDs
        dumper.commit()
        fmt = BaseFormatter(dumper.conn)

        # Assert all messages are returned
        assert len(list(fmt.get_messages_from_context(123))) == 365

        # Assert only messages after a date are returned
        min_date = datetime(year=2010, month=4, day=1)
        assert all(m.date >= min_date for m in fmt.get_messages_from_context(
            123, start_date=min_date
        ))

        # Assert only messages before a date are returned
        max_date = datetime(year=2010, month=4, day=1)
        assert all(m.date <= max_date for m in fmt.get_messages_from_context(
            123, end_date=max_date
        ))

        # Assert messages are returned in a range
        assert all(min_date <= m.date <= max_date for m in
                   fmt.get_messages_from_context(
                       123, start_date=min_date, end_date=max_date
                   ))

        # Assert messages are returned in the correct order
        desc = list(fmt.get_messages_from_context(123, order='DESC'))
        assert all(desc[i - 1] > desc[i] for i in range(1, len(desc)))

        asc = list(fmt.get_messages_from_context(123, order='ASC'))
        assert all(asc[i - 1] < asc[i] for i in range(1, len(asc)))
예제 #4
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()