Пример #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)))