Exemple #1
0
    def test_de_json(self, bot):
        json_dict = {
            'id': self.id_,
            'question': self.question,
            'options': [o.to_dict() for o in self.options],
            'total_voter_count': self.total_voter_count,
            'is_closed': self.is_closed,
            'is_anonymous': self.is_anonymous,
            'type': self.type,
            'allows_multiple_answers': self.allows_multiple_answers,
            'explanation': self.explanation,
            'explanation_entities': [self.explanation_entities[0].to_dict()],
            'open_period': self.open_period,
            'close_date': to_timestamp(self.close_date),
        }
        poll = Poll.de_json(json_dict, bot)

        assert poll.id == self.id_
        assert poll.question == self.question
        assert poll.options == self.options
        assert poll.options[0].text == self.options[0].text
        assert poll.options[0].voter_count == self.options[0].voter_count
        assert poll.options[1].text == self.options[1].text
        assert poll.options[1].voter_count == self.options[1].voter_count
        assert poll.total_voter_count == self.total_voter_count
        assert poll.is_closed == self.is_closed
        assert poll.is_anonymous == self.is_anonymous
        assert poll.type == self.type
        assert poll.allows_multiple_answers == self.allows_multiple_answers
        assert poll.explanation == self.explanation
        assert poll.explanation_entities == self.explanation_entities
        assert poll.open_period == self.open_period
        assert pytest.approx(poll.close_date == self.close_date)
        assert to_timestamp(poll.close_date) == to_timestamp(self.close_date)
Exemple #2
0
    def test_de_json(self, bot, time):
        json_dict = {
            'chat': self.chat.to_dict(),
            'from': self.from_user.to_dict(),
            'date': to_timestamp(time),
        }
        chat_join_request = ChatJoinRequest.de_json(json_dict, bot)

        assert chat_join_request.chat == self.chat
        assert chat_join_request.from_user == self.from_user
        assert pytest.approx(chat_join_request.date == time)
        assert to_timestamp(chat_join_request.date) == to_timestamp(time)

        json_dict.update({
            'bio': self.bio,
            'invite_link': self.invite_link.to_dict()
        })
        chat_join_request = ChatJoinRequest.de_json(json_dict, bot)

        assert chat_join_request.chat == self.chat
        assert chat_join_request.from_user == self.from_user
        assert pytest.approx(chat_join_request.date == time)
        assert to_timestamp(chat_join_request.date) == to_timestamp(time)
        assert chat_join_request.bio == self.bio
        assert chat_join_request.invite_link == self.invite_link
Exemple #3
0
    def to_dict(self):
        data = super(Message, self).to_dict()

        # Required
        data['from'] = data.pop('from_user', None)
        data['date'] = to_timestamp(self.date)
        # Optionals
        if self.forward_date:
            data['forward_date'] = to_timestamp(self.forward_date)
        if self.edit_date:
            data['edit_date'] = to_timestamp(self.edit_date)
        if self.photo:
            data['photo'] = [p.to_dict() for p in self.photo]
        if self.entities:
            data['entities'] = [e.to_dict() for e in self.entities]
        if self.caption_entities:
            data['caption_entities'] = [
                e.to_dict() for e in self.caption_entities
            ]
        if self.new_chat_photo:
            data['new_chat_photo'] = [p.to_dict() for p in self.new_chat_photo]
        data['new_chat_member'] = data.pop('_new_chat_member', None)
        if self.new_chat_members:
            data['new_chat_members'] = [
                u.to_dict() for u in self.new_chat_members
            ]

        return data
    def to_dict(self) -> JSONDict:
        """See :meth:`telegram.TelegramObject.to_dict`."""
        data = super().to_dict()

        data['until_date'] = to_timestamp(self.until_date)

        return data
Exemple #5
0
    def test_to_dict(self):
        voice_chat_scheduled = VoiceChatScheduled(self.start_date)
        voice_chat_scheduled_dict = voice_chat_scheduled.to_dict()

        assert isinstance(voice_chat_scheduled_dict, dict)
        assert voice_chat_scheduled_dict["start_date"] == to_timestamp(
            self.start_date)
 def test_de_json_subclass(self, chat_member_class_and_status, bot, chat_id, user):
     """This makes sure that e.g. ChatMemberAdministrator(data, bot) never returns a
     ChatMemberKicked instance."""
     cls = chat_member_class_and_status[0]
     time = datetime.datetime.utcnow()
     json_dict = {
         'user': user.to_dict(),
         'status': 'status',
         'custom_title': 'PTB',
         'is_anonymous': True,
         'until_date': to_timestamp(time),
         'can_be_edited': False,
         'can_change_info': True,
         'can_post_messages': False,
         'can_edit_messages': True,
         'can_delete_messages': True,
         'can_invite_users': False,
         'can_restrict_members': True,
         'can_pin_messages': False,
         'can_promote_members': True,
         'can_send_messages': False,
         'can_send_media_messages': True,
         'can_send_polls': False,
         'can_send_other_messages': True,
         'can_add_web_page_previews': False,
         'can_manage_chat': True,
         'can_manage_voice_chats': True,
     }
     assert type(cls.de_json(json_dict, bot)) is cls
Exemple #7
0
    def to_dict(self) -> JSONDict:
        data = super().to_dict()

        # Required
        data['date'] = to_timestamp(self.date)

        return data
    def to_dict(self) -> JSONDict:
        """See :meth:`telegram.TelegramObject.to_dict`."""
        data = super().to_dict()

        # Required
        data['start_date'] = to_timestamp(self.start_date)

        return data
Exemple #9
0
    def test_de_json(self, bot):
        assert VoiceChatScheduled.de_json({}, bot=bot) is None

        json_dict = {'start_date': to_timestamp(self.start_date)}
        voice_chat_scheduled = VoiceChatScheduled.de_json(json_dict, bot)

        assert pytest.approx(
            voice_chat_scheduled.start_date == self.start_date)
 def test_to_dict(self, invite_link):
     invite_link_dict = invite_link.to_dict()
     assert isinstance(invite_link_dict, dict)
     assert invite_link_dict['creator'] == invite_link.creator.to_dict()
     assert invite_link_dict['invite_link'] == invite_link.invite_link
     assert invite_link_dict['is_primary'] == self.primary
     assert invite_link_dict['is_revoked'] == self.revoked
     assert invite_link_dict['expire_date'] == to_timestamp(self.expire_date)
     assert invite_link_dict['member_limit'] == self.member_limit
Exemple #11
0
    def to_dict(self) -> JSONDict:
        data = super().to_dict()

        data['options'] = [x.to_dict() for x in self.options]
        if self.explanation_entities:
            data['explanation_entities'] = [e.to_dict() for e in self.explanation_entities]
        data['close_date'] = to_timestamp(data.get('close_date'))

        return data
Exemple #12
0
    def test_to_dict(self, chat_join_request, time):
        chat_join_request_dict = chat_join_request.to_dict()

        assert isinstance(chat_join_request_dict, dict)
        assert chat_join_request_dict['chat'] == chat_join_request.chat.to_dict()
        assert chat_join_request_dict['from'] == chat_join_request.from_user.to_dict()
        assert chat_join_request_dict['date'] == to_timestamp(chat_join_request.date)
        assert chat_join_request_dict['bio'] == chat_join_request.bio
        assert chat_join_request_dict['invite_link'] == chat_join_request.invite_link.to_dict()
    def test_de_json_required_args(self, bot, user, chat, old_chat_member, new_chat_member, time):
        json_dict = {
            'chat': chat.to_dict(),
            'from': user.to_dict(),
            'date': to_timestamp(time),
            'old_chat_member': old_chat_member.to_dict(),
            'new_chat_member': new_chat_member.to_dict(),
        }

        chat_member_updated = ChatMemberUpdated.de_json(json_dict, bot)

        assert chat_member_updated.chat == chat
        assert chat_member_updated.from_user == user
        assert pytest.approx(chat_member_updated.date == time)
        assert to_timestamp(chat_member_updated.date) == to_timestamp(time)
        assert chat_member_updated.old_chat_member == old_chat_member
        assert chat_member_updated.new_chat_member == new_chat_member
        assert chat_member_updated.invite_link is None
Exemple #14
0
    def to_dict(self) -> JSONDict:
        """See :meth:`telegram.TelegramObject.to_dict`."""
        data = super().to_dict()

        data['options'] = [x.to_dict() for x in self.options]
        if self.explanation_entities:
            data['explanation_entities'] = [e.to_dict() for e in self.explanation_entities]
        data['close_date'] = to_timestamp(data.get('close_date'))

        return data
Exemple #15
0
    def to_dict(self):
        """
        Returns:
            dict:
        """
        data = super(ChatMember, self).to_dict()

        data['until_date'] = to_timestamp(self.until_date)

        return data
    def test_de_json_all_args(self, bot, creator):
        json_dict = {
            'invite_link': self.link,
            'creator': creator.to_dict(),
            'is_primary': self.primary,
            'is_revoked': self.revoked,
            'expire_date': to_timestamp(self.expire_date),
            'member_limit': self.member_limit,
        }

        invite_link = ChatInviteLink.de_json(json_dict, bot)

        assert invite_link.invite_link == self.link
        assert invite_link.creator == creator
        assert invite_link.is_primary == self.primary
        assert invite_link.is_revoked == self.revoked
        assert pytest.approx(invite_link.expire_date == self.expire_date)
        assert to_timestamp(invite_link.expire_date) == to_timestamp(self.expire_date)
        assert invite_link.member_limit == self.member_limit
Exemple #17
0
    def to_dict(self):
        data = super(Message, self).to_dict()

        # Required
        data['from'] = data.pop('from_user', None)
        data['date'] = to_timestamp(self.date)
        # Optionals
        if self.forward_date:
            data['forward_date'] = to_timestamp(self.forward_date)
        if self.edit_date:
            data['edit_date'] = to_timestamp(self.edit_date)
        if self.photo:
            data['photo'] = [p.to_dict() for p in self.photo]
        if self.entities:
            data['entities'] = [e.to_dict() for e in self.entities]
        if self.new_chat_photo:
            data['new_chat_photo'] = [p.to_dict() for p in self.new_chat_photo]
        data['new_chat_member'] = data.pop('_new_chat_member', None)
        if self.new_chat_members:
            data['new_chat_members'] = [u.to_dict() for u in self.new_chat_members]

        return data
Exemple #18
0
    def test_de_json_all_args(self, bot, user):
        time = datetime.datetime.utcnow()
        custom_title = 'custom_title'

        json_dict = {
            'user': user.to_dict(),
            'status': self.status,
            'custom_title': custom_title,
            'is_anonymous': True,
            'until_date': to_timestamp(time),
            'can_be_edited': False,
            'can_change_info': True,
            'can_post_messages': False,
            'can_edit_messages': True,
            'can_delete_messages': True,
            'can_invite_users': False,
            'can_restrict_members': True,
            'can_pin_messages': False,
            'can_promote_members': True,
            'can_send_messages': False,
            'can_send_media_messages': True,
            'can_send_polls': False,
            'can_send_other_messages': True,
            'can_add_web_page_previews': False,
            'can_manage_chat': True,
            'can_manage_voice_chats': True,
        }

        chat_member = ChatMember.de_json(json_dict, bot)

        assert chat_member.user == user
        assert chat_member.status == self.status
        assert chat_member.custom_title == custom_title
        assert chat_member.is_anonymous is True
        assert chat_member.can_be_edited is False
        assert chat_member.can_change_info is True
        assert chat_member.can_post_messages is False
        assert chat_member.can_edit_messages is True
        assert chat_member.can_delete_messages is True
        assert chat_member.can_invite_users is False
        assert chat_member.can_restrict_members is True
        assert chat_member.can_pin_messages is False
        assert chat_member.can_promote_members is True
        assert chat_member.can_send_messages is False
        assert chat_member.can_send_media_messages is True
        assert chat_member.can_send_polls is False
        assert chat_member.can_send_other_messages is True
        assert chat_member.can_add_web_page_previews is False
        assert chat_member.can_manage_chat is True
        assert chat_member.can_manage_voice_chats is True
 def test_to_dict(self, chat_member_updated):
     chat_member_updated_dict = chat_member_updated.to_dict()
     assert isinstance(chat_member_updated_dict, dict)
     assert chat_member_updated_dict[
         'chat'] == chat_member_updated.chat.to_dict()
     assert chat_member_updated_dict[
         'from'] == chat_member_updated.from_user.to_dict()
     assert chat_member_updated_dict['date'] == to_timestamp(
         chat_member_updated.date)
     assert (chat_member_updated_dict['old_chat_member'] ==
             chat_member_updated.old_chat_member.to_dict())
     assert (chat_member_updated_dict['new_chat_member'] ==
             chat_member_updated.new_chat_member.to_dict())
     assert chat_member_updated_dict[
         'invite_link'] == chat_member_updated.invite_link.to_dict()
    def test_to_dict(self, poll):
        poll_dict = poll.to_dict()

        assert isinstance(poll_dict, dict)
        assert poll_dict['id'] == poll.id
        assert poll_dict['question'] == poll.question
        assert poll_dict['options'] == [o.to_dict() for o in poll.options]
        assert poll_dict['total_voter_count'] == poll.total_voter_count
        assert poll_dict['is_closed'] == poll.is_closed
        assert poll_dict['is_anonymous'] == poll.is_anonymous
        assert poll_dict['type'] == poll.type
        assert poll_dict['allows_multiple_answers'] == poll.allows_multiple_answers
        assert poll_dict['explanation'] == poll.explanation
        assert poll_dict['explanation_entities'] == [poll.explanation_entities[0].to_dict()]
        assert poll_dict['open_period'] == poll.open_period
        assert poll_dict['close_date'] == to_timestamp(poll.close_date)
Exemple #21
0
    def test_de_json_all_args(self, bot, user):
        time = datetime.datetime.now()
        json_dict = {
            'user': user.to_dict(),
            'status': self.status,
            'until_date': to_timestamp(time),
            'can_be_edited': False,
            'can_change_info': True,
            'can_post_messages': False,
            'can_edit_messages': True,
            'can_delete_messages': True,
            'can_invite_users': False,
            'can_restrict_members': True,
            'can_pin_messages': False,
            'can_promote_members': True,
            'can_send_messages': False,
            'can_send_media_messages': True,
            'can_send_other_messages': False,
            'can_add_web_page_previews': True
        }

        chat_member = ChatMember.de_json(json_dict, bot)

        assert chat_member.user == user
        assert chat_member.status == self.status
        assert chat_member.can_be_edited is False
        assert chat_member.can_change_info is True
        assert chat_member.can_post_messages is False
        assert chat_member.can_edit_messages is True
        assert chat_member.can_delete_messages is True
        assert chat_member.can_invite_users is False
        assert chat_member.can_restrict_members is True
        assert chat_member.can_pin_messages is False
        assert chat_member.can_promote_members is True
        assert chat_member.can_send_messages is False
        assert chat_member.can_send_media_messages is True
        assert chat_member.can_send_other_messages is False
        assert chat_member.can_add_web_page_previews is True
    def test_de_json_all_args(self, bot, user):
        time = datetime.datetime.now()
        json_dict = {'user': user.to_dict(),
                     'status': self.status,
                     'until_date': to_timestamp(time),
                     'can_be_edited': False,
                     'can_change_info': True,
                     'can_post_messages': False,
                     'can_edit_messages': True,
                     'can_delete_messages': True,
                     'can_invite_users': False,
                     'can_restrict_members': True,
                     'can_pin_messages': False,
                     'can_promote_members': True,
                     'can_send_messages': False,
                     'can_send_media_messages': True,
                     'can_send_other_messages': False,
                     'can_add_web_page_previews': True}

        chat_member = ChatMember.de_json(json_dict, bot)

        assert chat_member.user == user
        assert chat_member.status == self.status
        assert chat_member.can_be_edited is False
        assert chat_member.can_change_info is True
        assert chat_member.can_post_messages is False
        assert chat_member.can_edit_messages is True
        assert chat_member.can_delete_messages is True
        assert chat_member.can_invite_users is False
        assert chat_member.can_restrict_members is True
        assert chat_member.can_pin_messages is False
        assert chat_member.can_promote_members is True
        assert chat_member.can_send_messages is False
        assert chat_member.can_send_media_messages is True
        assert chat_member.can_send_other_messages is False
        assert chat_member.can_add_web_page_previews is True
    def test_de_json_all_args(self, bot, chat_member_class_and_status, user):
        cls = chat_member_class_and_status[0]
        status = chat_member_class_and_status[1]
        time = datetime.datetime.utcnow()

        json_dict = {
            'user': user.to_dict(),
            'status': status,
            'custom_title': 'PTB',
            'is_anonymous': True,
            'until_date': to_timestamp(time),
            'can_be_edited': False,
            'can_change_info': True,
            'can_post_messages': False,
            'can_edit_messages': True,
            'can_delete_messages': True,
            'can_invite_users': False,
            'can_restrict_members': True,
            'can_pin_messages': False,
            'can_promote_members': True,
            'can_send_messages': False,
            'can_send_media_messages': True,
            'can_send_polls': False,
            'can_send_other_messages': True,
            'can_add_web_page_previews': False,
            'can_manage_chat': True,
            'can_manage_voice_chats': True,
        }
        chat_member_type = ChatMember.de_json(json_dict, bot)

        assert isinstance(chat_member_type, ChatMember)
        assert isinstance(chat_member_type, cls)
        assert chat_member_type.user == user
        assert chat_member_type.status == status
        if chat_member_type.custom_title is not None:
            assert chat_member_type.custom_title == 'PTB'
            assert type(chat_member_type) in {ChatMemberOwner, ChatMemberAdministrator}
        if chat_member_type.is_anonymous is not None:
            assert chat_member_type.is_anonymous is True
            assert type(chat_member_type) in {ChatMemberOwner, ChatMemberAdministrator}
        if chat_member_type.until_date is not None:
            assert type(chat_member_type) in {ChatMemberBanned, ChatMemberRestricted}
        if chat_member_type.can_be_edited is not None:
            assert chat_member_type.can_be_edited is False
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_change_info is not None:
            assert chat_member_type.can_change_info is True
            assert type(chat_member_type) in {ChatMemberAdministrator, ChatMemberRestricted}
        if chat_member_type.can_post_messages is not None:
            assert chat_member_type.can_post_messages is False
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_edit_messages is not None:
            assert chat_member_type.can_edit_messages is True
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_delete_messages is not None:
            assert chat_member_type.can_delete_messages is True
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_invite_users is not None:
            assert chat_member_type.can_invite_users is False
            assert type(chat_member_type) in {ChatMemberAdministrator, ChatMemberRestricted}
        if chat_member_type.can_restrict_members is not None:
            assert chat_member_type.can_restrict_members is True
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_pin_messages is not None:
            assert chat_member_type.can_pin_messages is False
            assert type(chat_member_type) in {ChatMemberAdministrator, ChatMemberRestricted}
        if chat_member_type.can_promote_members is not None:
            assert chat_member_type.can_promote_members is True
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_send_messages is not None:
            assert chat_member_type.can_send_messages is False
            assert type(chat_member_type) == ChatMemberRestricted
        if chat_member_type.can_send_media_messages is not None:
            assert chat_member_type.can_send_media_messages is True
            assert type(chat_member_type) == ChatMemberRestricted
        if chat_member_type.can_send_polls is not None:
            assert chat_member_type.can_send_polls is False
            assert type(chat_member_type) == ChatMemberRestricted
        if chat_member_type.can_send_other_messages is not None:
            assert chat_member_type.can_send_other_messages is True
            assert type(chat_member_type) == ChatMemberRestricted
        if chat_member_type.can_add_web_page_previews is not None:
            assert chat_member_type.can_add_web_page_previews is False
            assert type(chat_member_type) == ChatMemberRestricted
        if chat_member_type.can_manage_chat is not None:
            assert chat_member_type.can_manage_chat is True
            assert type(chat_member_type) == ChatMemberAdministrator
        if chat_member_type.can_manage_voice_chats is not None:
            assert chat_member_type.can_manage_voice_chats is True
            assert type(chat_member_type) == ChatMemberAdministrator
Exemple #24
0
    def to_dict(self) -> JSONDict:
        data = super().to_dict()

        data['until_date'] = to_timestamp(self.until_date)

        return data
 def test_to_timestamp_none(self):
     # this 'convenience' behaviour has been left left for backwards compatibility
     assert helpers.to_timestamp(None) is None
 def test_to_timestamp(self, time_spec):
     # delegate tests to `to_float_timestamp`
     assert helpers.to_timestamp(time_spec) == int(
         helpers.to_float_timestamp(time_spec))
Exemple #27
0
    def to_dict(self) -> JSONDict:
        data = super().to_dict()

        data['expire_date'] = to_timestamp(self.expire_date)

        return data
    def to_dict(self):
        data = super(ChatMember, self).to_dict()

        data['until_date'] = to_timestamp(self.until_date)

        return data