Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add equality rich comparision operators to telegram objects #604

Merged
merged 19 commits into from
May 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions telegram/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def __init__(self,
self.mime_type = mime_type
self.file_size = file_size

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self,
self.mime_type = mime_type
self.file_size = file_size

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
14 changes: 12 additions & 2 deletions telegram/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

class TelegramObject(object):
"""Base class for most telegram objects."""

__metaclass__ = ABCMeta
_id_attrs = ()

def __str__(self):
return str(self.to_dict())
Expand Down Expand Up @@ -69,7 +69,7 @@ def to_dict(self):
data = dict()

for key in iter(self.__dict__):
if key == 'bot':
if key in ('bot', '_id_attrs'):
continue

value = self.__dict__[key]
Expand All @@ -80,3 +80,13 @@ def to_dict(self):
data[key] = value

return data

def __eq__(self, other):
if isinstance(other, self.__class__):
return self._id_attrs == other._id_attrs
return super(TelegramObject, self).__eq__(other)

def __hash__(self):
if self._id_attrs:
return hash((self.__class__, self._id_attrs))
return super(TelegramObject, self).__hash__()
1 change: 1 addition & 0 deletions telegram/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(self,
self.all_members_are_administrators = all_members_are_administrators

self.bot = bot
self._id_attrs = (self.id,)

@staticmethod
def de_json(data, bot):
Expand Down
2 changes: 2 additions & 0 deletions telegram/chatmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(self, user, status, **kwargs):
self.user = user
self.status = status

self._id_attrs = (self.user, self.status)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/choseninlineresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def __init__(self,
self.location = location
self.inline_message_id = inline_message_id

self._id_attrs = (self.result_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(self, phone_number, first_name, last_name=None, user_id=None, **kwa
self.last_name = last_name
self.user_id = user_id

self._id_attrs = (self.phone_number,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
4 changes: 4 additions & 0 deletions telegram/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Document(TelegramObject):

"""

_id_keys = ('file_id',)

def __init__(self,
file_id,
thumb=None,
Expand All @@ -56,6 +58,8 @@ def __init__(self,
self.mime_type = mime_type
self.file_size = file_size

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def __init__(self, file_id, bot, file_size=None, file_path=None, **kwargs):

self.bot = bot

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
1 change: 1 addition & 0 deletions telegram/inlinequery.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, id, from_user, query, offset, location=None, bot=None, **kwar
self.location = location

self.bot = bot
self._id_attrs = (self.id,)

@staticmethod
def de_json(data, bot):
Expand Down
2 changes: 2 additions & 0 deletions telegram/inlinequeryresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, type, id, **kwargs):
self.type = str(type)
self.id = str(id)

self._id_attrs = (self.id,)

@staticmethod
def de_json(data, bot):
return super(InlineQueryResult, InlineQueryResult).de_json(data, bot)
2 changes: 2 additions & 0 deletions telegram/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(self, longitude, latitude, **kwargs):
self.longitude = float(longitude)
self.latitude = float(latitude)

self._id_attrs = (self.longitude, self.latitude)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ def __init__(self,

self.bot = bot

self._id_attrs = (self.message_id,)

@property
def chat_id(self):
"""int: Short for :attr:`Message.chat.id`"""
Expand Down
6 changes: 1 addition & 5 deletions telegram/photosize.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ def __init__(self, file_id, width, height, file_size=None, **kwargs):
# Optionals
self.file_size = file_size

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (self.file_id == other.file_id and self.width == other.width
and self.height == other.height and self.file_size == other.file_size)
self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
Expand Down
2 changes: 2 additions & 0 deletions telegram/sticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def __init__(self, file_id, width, height, thumb=None, emoji=None, file_size=Non
self.emoji = emoji
self.file_size = file_size

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def __init__(self,
self._effective_chat = None
self._effective_message = None

self._id_attrs = (self.update_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self,

self.bot = bot

self._id_attrs = (self.id,)

@property
def name(self):
"""str: """
Expand Down
2 changes: 2 additions & 0 deletions telegram/venue.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self, location, title, address, foursquare_id=None, **kwargs):
# Optionals
self.foursquare_id = foursquare_id

self._id_attrs = (self.location, self.title)

@staticmethod
def de_json(data, bot):
data = super(Venue, Venue).de_json(data, bot)
Expand Down
2 changes: 2 additions & 0 deletions telegram/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def __init__(self,
self.mime_type = mime_type
self.file_size = file_size

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
2 changes: 2 additions & 0 deletions telegram/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def __init__(self, file_id, duration, mime_type=None, file_size=None, **kwargs):
self.mime_type = mime_type
self.file_size = file_size

self._id_attrs = (self.file_id,)

@staticmethod
def de_json(data, bot):
"""
Expand Down
91 changes: 91 additions & 0 deletions tests/test_animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents Tests for Telegram Animation"""

import unittest
import sys

sys.path.append('.')

import telegram
from tests.base import BaseTest


class AnimationTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Animation."""

def setUp(self):
self.animation_file_id = 'CgADBAADFQEAAny4rAUgukhiTv2TWwI'
self.thumb = telegram.PhotoSize.de_json({
'height': 50,
'file_size': 1613,
'file_id': 'AAQEABPQUWQZAAT7gZuQAAH0bd93VwACAg',
'width': 90
}, self._bot)
self.file_name = "game.gif.mp4"
self.mime_type = "video/mp4"
self.file_size = 4008

self.json_dict = {
'file_id': self.animation_file_id,
'thumb': self.thumb.to_dict(),
'file_name': self.file_name,
'mime_type': self.mime_type,
'file_size': self.file_size
}

def test_animation_de_json(self):
animation = telegram.Animation.de_json(self.json_dict, self._bot)

self.assertEqual(animation.file_id, self.animation_file_id)
self.assertEqual(animation.thumb, self.thumb)
self.assertEqual(animation.file_name, self.file_name)
self.assertEqual(animation.mime_type, self.mime_type)
self.assertEqual(animation.file_size, self.file_size)

def test_animation_to_json(self):
animation = telegram.Animation.de_json(self.json_dict, self._bot)

self.assertTrue(self.is_json(animation.to_json()))

def test_animation_to_dict(self):
animation = telegram.Animation.de_json(self.json_dict, self._bot)

self.assertTrue(self.is_dict(animation.to_dict()))
self.assertEqual(animation['file_id'], self.animation_file_id)
self.assertEqual(animation['thumb'], self.thumb)
self.assertEqual(animation['file_name'], self.file_name)
self.assertEqual(animation['mime_type'], self.mime_type)
self.assertEqual(animation['file_size'], self.file_size)

def test_equality(self):
a = telegram.Animation(self.animation_file_id)
b = telegram.Animation(self.animation_file_id)
d = telegram.Animation("")
e = telegram.Voice(self.animation_file_id, 0)

self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))
self.assertIsNot(a, b)

self.assertNotEqual(a, d)
self.assertNotEqual(hash(a), hash(d))

self.assertNotEqual(a, e)
self.assertNotEqual(hash(a), hash(e))
20 changes: 20 additions & 0 deletions tests/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,26 @@ def test_reply_audio(self):

self.assertNotEqual(message.audio.file_id, None)

def test_equality(self):
a = telegram.Audio(self.audio_file_id, self.duration)
b = telegram.Audio(self.audio_file_id, self.duration)
c = telegram.Audio(self.audio_file_id, 0)
d = telegram.Audio("", self.duration)
e = telegram.Voice(self.audio_file_id, self.duration)

self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))
self.assertIsNot(a, b)

self.assertEqual(a, c)
self.assertEqual(hash(a), hash(c))

self.assertNotEqual(a, d)
self.assertNotEqual(hash(a), hash(d))

self.assertNotEqual(a, e)
self.assertNotEqual(hash(a), hash(e))


if __name__ == '__main__':
unittest.main()
20 changes: 20 additions & 0 deletions tests/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def test_send_action(self):

self.assertTrue(result)

def test_equality(self):
a = telegram.Chat(self._id, self.title, self.type)
b = telegram.Chat(self._id, self.title, self.type)
c = telegram.Chat(self._id, "", "")
d = telegram.Chat(0, self.title, self.type)
e = telegram.User(self._id, "")

self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))
self.assertIsNot(a, b)

self.assertEqual(a, c)
self.assertEqual(hash(a), hash(c))

self.assertNotEqual(a, d)
self.assertNotEqual(hash(a), hash(d))

self.assertNotEqual(a, e)
self.assertNotEqual(hash(a), hash(e))


if __name__ == '__main__':
unittest.main()
Loading