def test_equality(self):
        a = ChatInviteLink("link", User(1, "", False), True, True, True)
        b = ChatInviteLink("link", User(1, "", False), True, True, True)
        c = ChatInviteLink("link", User(2, "", False), True, True, True)
        d1 = ChatInviteLink("link", User(1, "", False), False, True, True)
        d2 = ChatInviteLink("link", User(1, "", False), True, False, True)
        d3 = ChatInviteLink("link", User(1, "", False), True, True, False)
        e = ChatInviteLink("notalink", User(1, "", False), True, False, True)
        f = ChatInviteLink("notalink", User(1, "", False), True, True, True)
        g = User(1, "", False)

        assert a == b
        assert hash(a) == hash(b)
        assert a is not b

        assert a != c
        assert hash(a) != hash(c)

        assert a != d1
        assert hash(a) != hash(d1)

        assert a != d2
        assert hash(a) != hash(d2)

        assert d2 != d3
        assert hash(d2) != hash(d3)

        assert a != e
        assert hash(a) != hash(e)

        assert a != f
        assert hash(a) != hash(f)

        assert a != g
        assert hash(a) != hash(g)
    def test_de_json_all_args(self, bot, creator):
        json_dict = {
            "invite_link": self.link,
            "creator": creator.to_dict(),
            "creates_join_request": self.creates_join_request,
            "is_primary": self.primary,
            "is_revoked": self.revoked,
            "expire_date": to_timestamp(self.expire_date),
            "member_limit": self.member_limit,
            "name": self.name,
            "pending_join_request_count": str(self.pending_join_request_count),
        }

        invite_link = ChatInviteLink.de_json(json_dict, bot)

        assert invite_link.invite_link == self.link
        assert invite_link.creator == creator
        assert invite_link.creates_join_request == self.creates_join_request
        assert invite_link.is_primary == self.primary
        assert invite_link.is_revoked == self.revoked
        assert abs(invite_link.expire_date -
                   self.expire_date) < datetime.timedelta(seconds=1)
        assert to_timestamp(invite_link.expire_date) == to_timestamp(
            self.expire_date)
        assert invite_link.member_limit == self.member_limit
        assert invite_link.name == self.name
        assert invite_link.pending_join_request_count == self.pending_join_request_count
Пример #3
0
def invite_link(creator):
    return ChatInviteLink(
        TestChatInviteLink.link,
        creator,
        TestChatInviteLink.primary,
        TestChatInviteLink.revoked,
        expire_date=TestChatInviteLink.expire_date,
        member_limit=TestChatInviteLink.member_limit,
    )
def invite_link(creator):
    return ChatInviteLink(
        TestChatInviteLink.link,
        creator,
        TestChatInviteLink.primary,
        TestChatInviteLink.revoked,
        expire_date=TestChatInviteLink.expire_date,
        member_limit=TestChatInviteLink.member_limit,
        name=TestChatInviteLink.name,
        pending_join_request_count=TestChatInviteLink.pending_join_request_count,
    )
    def test_de_json_required_args(self, bot, creator):
        json_dict = {
            'invite_link': self.link,
            'creator': creator.to_dict(),
            'is_primary': self.primary,
            'is_revoked': self.revoked,
        }

        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
    def test_equality(self):
        a = ChatInviteLink("link", User(1, '', False), True, True)
        b = ChatInviteLink("link", User(1, '', False), True, True)
        d = ChatInviteLink("link", User(2, '', False), False, True)
        d2 = ChatInviteLink("notalink", User(1, '', False), False, True)
        d3 = ChatInviteLink("notalink", User(1, '', False), True, True)
        e = User(1, '', False)

        assert a == b
        assert hash(a) == hash(b)
        assert a is not b

        assert a != d
        assert hash(a) != hash(d)

        assert a != d2
        assert hash(a) != hash(d2)

        assert d2 != d3
        assert hash(d2) != hash(d3)

        assert a != e
        assert hash(a) != hash(e)
def chat_join_request(time, bot):
    return ChatJoinRequest(
        chat=Chat(1, Chat.SUPERGROUP),
        from_user=User(2, 'first_name', False),
        date=time,
        bio='bio',
        invite_link=ChatInviteLink(
            'https://invite.link',
            User(42, 'creator', False),
            name='InviteLink',
            is_revoked=False,
            is_primary=False,
        ),
        bot=bot,
    )
    def de_json(cls, data: Optional[JSONDict],
                bot: 'Bot') -> Optional['ChatJoinRequest']:
        """See :meth:`telegram.TelegramObject.de_json`."""
        data = cls._parse_data(data)

        if not data:
            return None

        data['chat'] = Chat.de_json(data.get('chat'), bot)
        data['from_user'] = User.de_json(data.get('from'), bot)
        data['date'] = from_timestamp(data.get('date', None))
        data['invite_link'] = ChatInviteLink.de_json(data.get('invite_link'),
                                                     bot)

        return cls(bot=bot, **data)
    def test_de_json_required_args(self, bot, creator):
        json_dict = {
            "invite_link": self.link,
            "creator": creator.to_dict(),
            "creates_join_request": self.creates_join_request,
            "is_primary": self.primary,
            "is_revoked": self.revoked,
        }

        invite_link = ChatInviteLink.de_json(json_dict, bot)

        assert invite_link.invite_link == self.link
        assert invite_link.creator == creator
        assert invite_link.creates_join_request == self.creates_join_request
        assert invite_link.is_primary == self.primary
        assert invite_link.is_revoked == self.revoked
Пример #10
0
def chat_join_request(time, bot):
    return ChatJoinRequest(
        chat=Chat(1, Chat.SUPERGROUP),
        from_user=User(2, "first_name", False),
        date=time,
        bio="bio",
        invite_link=ChatInviteLink(
            "https://invite.link",
            User(42, "creator", False),
            creates_join_request=False,
            name="InviteLink",
            is_revoked=False,
            is_primary=False,
        ),
        bot=bot,
    )
Пример #11
0
    def de_json(cls, data: Optional[JSONDict],
                bot: 'Bot') -> Optional['ChatMemberUpdated']:
        data = cls.parse_data(data)

        if not data:
            return None

        data['chat'] = Chat.de_json(data.get('chat'), bot)
        data['from_user'] = User.de_json(data.get('from'), bot)
        data['date'] = from_timestamp(data.get('date'))
        data['old_chat_member'] = ChatMember.de_json(
            data.get('old_chat_member'), bot)
        data['new_chat_member'] = ChatMember.de_json(
            data.get('new_chat_member'), bot)
        data['invite_link'] = ChatInviteLink.de_json(data.get('invite_link'),
                                                     bot)

        return cls(**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
def invite_link(user):
    return ChatInviteLink('link', user, True, True)
Пример #14
0
class TestChatJoinRequest:
    chat = Chat(1, Chat.SUPERGROUP)
    from_user = User(2, 'first_name', False)
    bio = 'bio'
    invite_link = ChatInviteLink(
        'https://invite.link',
        User(42, 'creator', False),
        name='InviteLink',
        is_revoked=False,
        is_primary=False,
    )

    def test_slot_behaviour(self, chat_join_request, recwarn, mro_slots):
        inst = chat_join_request
        for attr in inst.__slots__:
            assert getattr(inst, attr,
                           'err') != 'err', f"got extra slot '{attr}'"
        assert not inst.__dict__, f"got missing slot(s): {inst.__dict__}"
        assert len(mro_slots(inst)) == len(set(
            mro_slots(inst))), "duplicate slot"
        inst.custom, inst.bio = 'should give warning', self.bio
        assert len(recwarn) == 1 and 'custom' in str(
            recwarn[0].message), recwarn.list

    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

    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_equality(self, chat_join_request, time):
        a = chat_join_request
        b = ChatJoinRequest(self.chat, self.from_user, time)
        c = ChatJoinRequest(self.chat, self.from_user, time, bio='bio')
        d = ChatJoinRequest(self.chat, self.from_user,
                            time + datetime.timedelta(1))
        e = ChatJoinRequest(self.chat, User(-1, 'last_name', True), time)
        f = User(456, '', False)

        assert a == b
        assert hash(a) == hash(b)
        assert a is not b

        assert a == c
        assert hash(a) == hash(c)

        assert a != d
        assert hash(a) != hash(d)

        assert a != e
        assert hash(a) != hash(e)

        assert a != f
        assert hash(a) != hash(f)

    def test_approve(self, monkeypatch, chat_join_request):
        def make_assertion(*_, **kwargs):
            chat_id_test = kwargs['chat_id'] == chat_join_request.chat.id
            user_id_test = kwargs['user_id'] == chat_join_request.from_user.id

            return chat_id_test and user_id_test

        assert check_shortcut_signature(ChatJoinRequest.approve,
                                        Bot.approve_chat_join_request,
                                        ['chat_id', 'user_id'], [])
        assert check_shortcut_call(chat_join_request.approve,
                                   chat_join_request.bot,
                                   'approve_chat_join_request')
        assert check_defaults_handling(chat_join_request.approve,
                                       chat_join_request.bot)

        monkeypatch.setattr(chat_join_request.bot, 'approve_chat_join_request',
                            make_assertion)
        assert chat_join_request.approve()

    def test_decline(self, monkeypatch, chat_join_request):
        def make_assertion(*_, **kwargs):
            chat_id_test = kwargs['chat_id'] == chat_join_request.chat.id
            user_id_test = kwargs['user_id'] == chat_join_request.from_user.id

            return chat_id_test and user_id_test

        assert check_shortcut_signature(ChatJoinRequest.decline,
                                        Bot.decline_chat_join_request,
                                        ['chat_id', 'user_id'], [])
        assert check_shortcut_call(chat_join_request.decline,
                                   chat_join_request.bot,
                                   'decline_chat_join_request')
        assert check_defaults_handling(chat_join_request.decline,
                                       chat_join_request.bot)

        monkeypatch.setattr(chat_join_request.bot, 'decline_chat_join_request',
                            make_assertion)
        assert chat_join_request.decline()
def invite_link(user):
    return ChatInviteLink("link", user, False, True, True)