Example #1
0
    def de_json(cls, data, bot):
        if not data:
            return None

        data = super(Game, cls).de_json(data, bot)

        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['text_entities'] = MessageEntity.de_list(data.get('text_entities'), bot)
        data['animation'] = Animation.de_json(data.get('animation'), bot)

        return cls(**data)
    def de_json(data, bot):
        """
        Args:
            data (dict):
            bot (telegram.Bot):

        Returns:
            telegram.Game:

        """
        if not data:
            return None

        data['photo'] = PhotoSize.de_list(data.get('photo'), bot)
        data['text_entities'] = MessageEntity.de_list(data.get('text_entities'), bot)
        data['animation'] = Animation.de_json(data.get('animation'), bot)

        return Game(**data)
Example #3
0
 }, {
     'text': 'a text message',
     'enitites': [MessageEntity('bold', 10, 4),
                  MessageEntity('italic', 16, 7)]
 }, {
     'caption': 'A message caption',
     'caption_entities': [MessageEntity('bold', 1, 1),
                          MessageEntity('text_link', 4, 3)]
 }, {
     'audio': Audio('audio_id', 12),
     'caption': 'audio_file'
 }, {
     'document': Document('document_id'),
     'caption': 'document_file'
 }, {
     'animation': Animation('animation_id', 30, 30, 1),
     'caption': 'animation_file'
 }, {
     'game': Game('my_game', 'just my game', [
         PhotoSize('game_photo_id', 30, 30),
     ])
 }, {
     'photo': [PhotoSize('photo_id', 50, 50)],
     'caption': 'photo_file'
 }, {
     'sticker': Sticker('sticker_id', 50, 50)
 }, {
     'video': Video('video_id', 12, 12, 12),
     'caption': 'video_file'
 }, {
     'voice': Voice('voice_id', 5)
Example #4
0
class TestGame(object):
    title = 'Python-telegram-bot Test Game'
    description = 'description'
    photo = [PhotoSize('Blah', 640, 360, file_size=0)]
    text = (b'\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467'
            b'\\u200d\\U0001f467\\U0001f431http://google.com').decode('unicode-escape')
    text_entities = [MessageEntity(13, 17, MessageEntity.URL)]
    animation = Animation('blah', 320, 180, 1)

    def test_de_json_required(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo

    def test_de_json_all(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
            'text': self.text,
            'text_entities': [self.text_entities[0].to_dict()],
            'animation': self.animation.to_dict()
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo
        assert game.text == self.text
        assert game.text_entities == self.text_entities
        assert game.animation == self.animation

    def test_to_dict(self, game):
        game_dict = game.to_dict()

        assert isinstance(game_dict, dict)
        assert game_dict['title'] == game.title
        assert game_dict['description'] == game.description
        assert game_dict['photo'] == [game.photo[0].to_dict()]
        assert game_dict['text'] == game.text
        assert game_dict['text_entities'] == [game.text_entities[0].to_dict()]
        assert game_dict['animation'] == game.animation.to_dict()

    def test_parse_entity(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        game.text_entities = [entity]

        assert game.parse_text_entity(entity) == 'http://google.com'

    def test_parse_entities(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        entity_2 = MessageEntity(type=MessageEntity.BOLD, offset=13, length=1)
        game.text_entities = [entity_2, entity]

        assert game.parse_text_entities(MessageEntity.URL) == {entity: 'http://google.com'}
        assert game.parse_text_entities() == {entity: 'http://google.com', entity_2: 'h'}
 {'forward_from_chat': Chat(-23, 'channel'),
  'forward_from_message_id': 101,
  'forward_date': datetime.utcnow()},
 {'reply_to_message': Message(50, None, None, None)},
 {'edit_date': datetime.utcnow()},
 {'text': 'a text message',
  'enitites': [MessageEntity('bold', 10, 4),
               MessageEntity('italic', 16, 7)]},
 {'caption': 'A message caption',
  'caption_entities': [MessageEntity('bold', 1, 1),
                       MessageEntity('text_link', 4, 3)]},
 {'audio': Audio('audio_id', 'unique_id', 12),
  'caption': 'audio_file'},
 {'document': Document('document_id', 'unique_id'),
  'caption': 'document_file'},
 {'animation': Animation('animation_id', 'unique_id', 30, 30, 1),
  'caption': 'animation_file'},
 {'game': Game('my_game', 'just my game',
               [PhotoSize('game_photo_id', 'unique_id', 30, 30), ])},
 {'photo': [PhotoSize('photo_id', 'unique_id', 50, 50)],
  'caption': 'photo_file'},
 {'sticker': Sticker('sticker_id', 'unique_id', 50, 50, True)},
 {'video': Video('video_id', 'unique_id', 12, 12, 12),
  'caption': 'video_file'},
 {'voice': Voice('voice_id', 'unique_id', 5)},
 {'video_note': VideoNote('video_note_id', 'unique_id', 20, 12)},
 {'new_chat_members': [User(55, 'new_user', False)]},
 {'contact': Contact('phone_numner', 'contact_name')},
 {'location': Location(-23.691288, 46.788279)},
 {'venue': Venue(Location(-23.691288, 46.788279),
                 'some place', 'right here')},
 def test_parse_file_input_tg_object(self):
     animation = Animation('file_id', 'unique_id', 1, 1, 1)
     assert helpers.parse_file_input(animation, Animation) == 'file_id'
     assert helpers.parse_file_input(animation, MessageEntity) is animation
Example #7
0
def animation(thumb, bot):
    return Animation(TestAnimation.animation_file_id, thumb=thumb.to_dict(),
                     file_name=TestAnimation.file_name, mime_type=TestAnimation.mime_type,
                     file_size=TestAnimation.file_size, bot=bot)
class TestGame:
    title = 'Python-telegram-bot Test Game'
    description = 'description'
    photo = [PhotoSize('Blah', 'ElseBlah', 640, 360, file_size=0)]
    text = (b'\\U0001f469\\u200d\\U0001f469\\u200d\\U0001f467'
            b'\\u200d\\U0001f467\\U0001f431http://google.com'
            ).decode('unicode-escape')
    text_entities = [MessageEntity(13, 17, MessageEntity.URL)]
    animation = Animation('blah', 'unique_id', 320, 180, 1)

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

    def test_de_json_required(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo

    def test_de_json_all(self, bot):
        json_dict = {
            'title': self.title,
            'description': self.description,
            'photo': [self.photo[0].to_dict()],
            'text': self.text,
            'text_entities': [self.text_entities[0].to_dict()],
            'animation': self.animation.to_dict(),
        }
        game = Game.de_json(json_dict, bot)

        assert game.title == self.title
        assert game.description == self.description
        assert game.photo == self.photo
        assert game.text == self.text
        assert game.text_entities == self.text_entities
        assert game.animation == self.animation

    def test_to_dict(self, game):
        game_dict = game.to_dict()

        assert isinstance(game_dict, dict)
        assert game_dict['title'] == game.title
        assert game_dict['description'] == game.description
        assert game_dict['photo'] == [game.photo[0].to_dict()]
        assert game_dict['text'] == game.text
        assert game_dict['text_entities'] == [game.text_entities[0].to_dict()]
        assert game_dict['animation'] == game.animation.to_dict()

    def test_parse_entity(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        game.text_entities = [entity]

        assert game.parse_text_entity(entity) == 'http://google.com'

    def test_parse_entities(self, game):
        entity = MessageEntity(type=MessageEntity.URL, offset=13, length=17)
        entity_2 = MessageEntity(type=MessageEntity.BOLD, offset=13, length=1)
        game.text_entities = [entity_2, entity]

        assert game.parse_text_entities(MessageEntity.URL) == {
            entity: 'http://google.com'
        }
        assert game.parse_text_entities() == {
            entity: 'http://google.com',
            entity_2: 'h'
        }

    def test_equality(self):
        a = Game('title', 'description',
                 [PhotoSize('Blah', 'unique_id', 640, 360, file_size=0)])
        b = Game(
            'title',
            'description',
            [PhotoSize('Blah', 'unique_id', 640, 360, file_size=0)],
            text='Here is a text',
        )
        c = Game(
            'eltit',
            'description',
            [PhotoSize('Blah', 'unique_id', 640, 360, file_size=0)],
            animation=Animation('blah', 'unique_id', 320, 180, 1),
        )
        d = Animation('blah', 'unique_id', 320, 180, 1)

        assert a == b
        assert hash(a) == hash(b)

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

        assert a != d
        assert hash(a) != hash(d)
 def test_parse_file_input_tg_object(self):
     animation = Animation("file_id", "unique_id", 1, 1, 1)
     assert telegram._utils.files.parse_file_input(animation,
                                                   Animation) == "file_id"
     assert telegram._utils.files.parse_file_input(
         animation, MessageEntity) is animation