From 2b28f12a9ba6ab51ed3ef8e0817aef62dfcaec1f Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Fri, 12 May 2017 19:41:17 +0200 Subject: [PATCH 01/19] Add equality rich comparision operator to base class --- telegram/base.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/telegram/base.py b/telegram/base.py index abe63560392..52ae7d2f785 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -80,3 +80,23 @@ def to_dict(self): data[key] = value return data + + def _get_id(self): + for x in ['id', 'file_id', 'message_id', 'result_id', 'update_id', 'phone_number']: + if hasattr(self, x): + return self.__getattribute__(x) + raise NotImplementedError + + def __eq__(self, other): + if isinstance(other, TelegramObject): + try: + return self._get_id() == other._get_id() + except NotImplementedError: + pass # return NotImplemented + return NotImplemented + + def __hash__(self): + try: + return hash(self._get_id()) + except NotImplementedError: + return super().__hash__() From 63763c65dffa4cfcd56c741abd8234e479a272b2 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 14:47:30 +0200 Subject: [PATCH 02/19] Use self.__class__ instead of "hardcoding" TelegramObject --- telegram/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/base.py b/telegram/base.py index 52ae7d2f785..df0f87c3454 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -88,7 +88,7 @@ def _get_id(self): raise NotImplementedError def __eq__(self, other): - if isinstance(other, TelegramObject): + if isinstance(other, self.__class__): try: return self._get_id() == other._get_id() except NotImplementedError: From 83e3de17fbc8efe78cba95a63a1e57039a2589a7 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 14:49:36 +0200 Subject: [PATCH 03/19] Contact.phone_number really isn't an id imo --- telegram/base.py | 2 +- telegram/contact.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/telegram/base.py b/telegram/base.py index df0f87c3454..3f7170ada38 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -82,7 +82,7 @@ def to_dict(self): return data def _get_id(self): - for x in ['id', 'file_id', 'message_id', 'result_id', 'update_id', 'phone_number']: + for x in ['id', 'file_id', 'message_id', 'result_id', 'update_id']: if hasattr(self, x): return self.__getattribute__(x) raise NotImplementedError diff --git a/telegram/contact.py b/telegram/contact.py index a54870f416b..69ebadbd3c6 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -61,3 +61,11 @@ def de_json(data, bot): return None return Contact(**data) + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.phone_number == self.phone_number + return NotImplemented + + def __hash__(self): + return hash(self.phone_number) From c2ac3a87dbf1cba2a83a0cd6d61015098d3dc0e5 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 16:39:13 +0200 Subject: [PATCH 04/19] Add equality check and hash to ChatMember, Location and Venue. --- telegram/chatmember.py | 8 ++++++++ telegram/location.py | 8 ++++++++ telegram/venue.py | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/telegram/chatmember.py b/telegram/chatmember.py index 7b228710bac..560359c5a2e 100644 --- a/telegram/chatmember.py +++ b/telegram/chatmember.py @@ -64,3 +64,11 @@ def de_json(data, bot): data['user'] = User.de_json(data.get('user'), bot) return ChatMember(**data) + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.user == other.user and self.status == other.status + return NotImplemented + + def __hash__(self): + return hash((self.user, self.status)) diff --git a/telegram/location.py b/telegram/location.py index 7263a6e566f..8493bc7392d 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -52,3 +52,11 @@ def de_json(data, bot): return None return Location(**data) + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.longitude == other.longitude and self.latitude == other.latitude + return NotImplemented + + def __hash__(self): + return hash((self.longitude, self.latitude)) diff --git a/telegram/venue.py b/telegram/venue.py index e066da0b705..b42466c572d 100644 --- a/telegram/venue.py +++ b/telegram/venue.py @@ -50,3 +50,11 @@ def de_json(data, bot): data['location'] = Location.de_json(data.get('location'), bot) return Venue(**data) + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.location == other.location and self.title == other.title + return NotImplemented + + def __hash__(self): + return hash((self.__class__, self.location, self.title)) From 6693054cddc88927e071b57d7c129b97316e243c Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 16:46:48 +0200 Subject: [PATCH 05/19] Hash based on self.__class__ --- telegram/base.py | 4 +++- telegram/chatmember.py | 2 +- telegram/location.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/telegram/base.py b/telegram/base.py index 3f7170ada38..ab93786000a 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -97,6 +97,8 @@ def __eq__(self, other): def __hash__(self): try: - return hash(self._get_id()) + return hash(( + self.__class__, + self._get_id(),)) except NotImplementedError: return super().__hash__() diff --git a/telegram/chatmember.py b/telegram/chatmember.py index 560359c5a2e..73f6de83976 100644 --- a/telegram/chatmember.py +++ b/telegram/chatmember.py @@ -71,4 +71,4 @@ def __eq__(self, other): return NotImplemented def __hash__(self): - return hash((self.user, self.status)) + return hash((self.__class__, self.user, self.status)) diff --git a/telegram/location.py b/telegram/location.py index 8493bc7392d..958c62e9f80 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -59,4 +59,4 @@ def __eq__(self, other): return NotImplemented def __hash__(self): - return hash((self.longitude, self.latitude)) + return hash((self.__class__, self.longitude, self.latitude)) From cd0b9960721f1ac6b798cea90a81c8c884d36ab0 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 17:18:36 +0200 Subject: [PATCH 06/19] Equality tests part 1 Audio, Document, File, Location, Photo, Sticker, Venue, Video and Voice --- tests/test_audio.py | 20 ++++++++++++++++++++ tests/test_document.py | 16 ++++++++++++++++ tests/test_file.py | 20 ++++++++++++++++++++ tests/test_location.py | 16 ++++++++++++++++ tests/test_photo.py | 20 ++++++++++++++++++++ tests/test_sticker.py | 20 ++++++++++++++++++++ tests/test_venue.py | 20 ++++++++++++++++++++ tests/test_video.py | 20 ++++++++++++++++++++ tests/test_voice.py | 20 ++++++++++++++++++++ 9 files changed, 172 insertions(+) diff --git a/tests/test_audio.py b/tests/test_audio.py index edcae5f9b25..6eb74bb7fe1 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -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() diff --git a/tests/test_document.py b/tests/test_document.py index 394571704a1..ec908b0039b 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -178,6 +178,22 @@ def test_reply_document(self): self.assertNotEqual(message.document.file_id, '') + def test_equality(self): + a = telegram.Document(self.document_file_id) + b = telegram.Document(self.document_file_id) + d = telegram.Document("") + e = telegram.Voice(self.document_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)) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_file.py b/tests/test_file.py index af5adf291c9..8443dece7ff 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -137,6 +137,26 @@ def test_error_file_without_required_args(self): self.assertRaises(TypeError, lambda: self._bot.getFile(**json_dict)) + def test_equality(self): + a = telegram.File(self.audio_file_id, self._bot) + b = telegram.File(self.audio_file_id, self._bot) + c = telegram.File(self.audio_file_id, None) + d = telegram.File(self.document_file_id, self._bot) + e = telegram.Voice(self.audio_file_id, 0) + + 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() diff --git a/tests/test_location.py b/tests/test_location.py index b98cc791c7f..f32823469a9 100644 --- a/tests/test_location.py +++ b/tests/test_location.py @@ -101,6 +101,22 @@ def test_reply_location(self): self.assertEqual(message.location.latitude, self.latitude) self.assertEqual(message.location.longitude, self.longitude) + def test_equality(self): + a = telegram.Location(self.longitude, self.latitude) + b = telegram.Location(self.longitude, self.latitude) + c = telegram.Location(self.longitude, 0) + d = telegram.Location(0, self.latitude) + + 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)) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_photo.py b/tests/test_photo.py index 77969078f0f..7844efd10d0 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -209,6 +209,26 @@ def test_reply_photo(self): self.assertNotEqual(message.photo[0].file_id, '') + def test_equality(self): + a = telegram.PhotoSize(self.photo_file_id, self.width, self.height) + b = telegram.PhotoSize(self.photo_file_id, self.width, self.height) + c = telegram.PhotoSize(self.photo_file_id, 0, 0) + d = telegram.PhotoSize("", self.width, self.height) + e = telegram.Sticker(self.photo_file_id, self.width, self.height) + + 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() diff --git a/tests/test_sticker.py b/tests/test_sticker.py index 12ec25a5a40..c43a6aaeb7e 100644 --- a/tests/test_sticker.py +++ b/tests/test_sticker.py @@ -147,6 +147,26 @@ def test_reply_sticker(self): self.assertNotEqual(message.sticker.file_id, '') + def test_equality(self): + a = telegram.Sticker(self.sticker_file_id, self.width, self.height) + b = telegram.Sticker(self.sticker_file_id, self.width, self.height) + c = telegram.Sticker(self.sticker_file_id, 0, 0) + d = telegram.Sticker("", self.width, self.height) + e = telegram.PhotoSize(self.sticker_file_id, self.width, self.height) + + 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() diff --git a/tests/test_venue.py b/tests/test_venue.py index 4fc3df6ac94..1b08311a644 100644 --- a/tests/test_venue.py +++ b/tests/test_venue.py @@ -74,6 +74,26 @@ def test_reply_venue(self): self.assertAlmostEqual(message.venue.location.latitude, self.location.latitude, 2) self.assertAlmostEqual(message.venue.location.longitude, self.location.longitude, 2) + def test_equality(self): + a = telegram.Venue(telegram.Location(0, 0), "Title", "Address") + b = telegram.Venue(telegram.Location(0, 0), "Title", "Address") + c = telegram.Venue(telegram.Location(0, 0), "Title", "Not Address") + d = telegram.Venue(telegram.Location(0, 1), "Title", "Address") + d2 = telegram.Venue(telegram.Location(0, 0), "Not Title", "Address") + + 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, d2) + self.assertNotEqual(hash(a), hash(d2)) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_video.py b/tests/test_video.py index a704c5279ad..dfc6370c10e 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -270,6 +270,26 @@ def test_reply_video(self): self.assertNotEqual(message.video.file_id, None) + def test_equality(self): + a = telegram.Video(self.video_file_id, self.width, self.height, self.duration) + b = telegram.Video(self.video_file_id, self.width, self.height, self.duration) + c = telegram.Video(self.video_file_id, 0, 0, 0) + d = telegram.Video("", self.width, self.height, self.duration) + e = telegram.Voice(self.video_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() diff --git a/tests/test_voice.py b/tests/test_voice.py index 75ac2033174..344f5e44951 100644 --- a/tests/test_voice.py +++ b/tests/test_voice.py @@ -240,6 +240,26 @@ def test_reply_voice(self): self.assertNotEqual(message.voice.file_id, '') + def test_equality(self): + a = telegram.Voice(self.voice_file_id, self.duration) + b = telegram.Voice(self.voice_file_id, self.duration) + c = telegram.Voice(self.voice_file_id, 0) + d = telegram.Voice("", self.duration) + e = telegram.Audio(self.voice_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() From 34ac66319fc50e415c0b09954d223c3b9967fba3 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 17:44:15 +0200 Subject: [PATCH 07/19] Move AnimationTest and give it some proper values --- tests/test_animation.py | 75 +++++++++++++++++++++++++++++++++++++++++ tests/test_game.py | 33 ------------------ 2 files changed, 75 insertions(+), 33 deletions(-) create mode 100644 tests/test_animation.py diff --git a/tests/test_animation.py b/tests/test_animation.py new file mode 100644 index 00000000000..73ee8c4f1ea --- /dev/null +++ b/tests/test_animation.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2016 +# Leandro Toledo de Souza +# +# 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) diff --git a/tests/test_game.py b/tests/test_game.py index 838dea88f91..2ee54063185 100644 --- a/tests/test_game.py +++ b/tests/test_game.py @@ -99,36 +99,3 @@ def test_parse_entities(self): self.assertDictEqual(game.parse_text_entities(), {entity: 'http://google.com', entity_2: 'h'}) - - -class AnimationTest(BaseTest, unittest.TestCase): - """This object represents Tests for Telegram Animatiion.""" - - def setUp(self): - self.file_id = 'thisisafileid' - self.thumb = {'width': 640, 'height': 360, 'file_id': 'Blah', 'file_size': 0} - self.file_name = 'File name' - self.mime_type = 'something/gif' - self.file_size = 42 - - self.json_dict = { - 'file_id': self.file_id, - 'thumb': self.thumb, - '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.file_id) - self.assertTrue(isinstance(animation.thumb, telegram.PhotoSize)) - 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_game_to_json(self): - animation = telegram.Animation.de_json(self.json_dict, self._bot) - - self.assertTrue(self.is_json(animation.to_json())) From 65b108c67cf4ef2e4c1fc19408d84ef7eca4ec36 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 18:06:48 +0200 Subject: [PATCH 08/19] Equality tests for Animation, Contact and uncached inlinequeryresults --- tests/test_animation.py | 16 ++++++++++++++++ tests/test_contact.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultarticle.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultaudio.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultcontact.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultdocument.py | 22 ++++++++++++++++++++++ tests/test_inlinequeryresultgif.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultlocation.py | 24 ++++++++++++++++++++++-- tests/test_inlinequeryresultmpeg4gif.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultphoto.py | 20 ++++++++++++++++++++ tests/test_inlinequeryresultvenue.py | 23 +++++++++++++++++++++++ tests/test_inlinequeryresultvideo.py | 24 ++++++++++++++++++++++++ tests/test_inlinequeryresultvoice.py | 20 ++++++++++++++++++++ 13 files changed, 267 insertions(+), 2 deletions(-) diff --git a/tests/test_animation.py b/tests/test_animation.py index 73ee8c4f1ea..4c664aa1899 100644 --- a/tests/test_animation.py +++ b/tests/test_animation.py @@ -73,3 +73,19 @@ def test_animation_to_dict(self): 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)) diff --git a/tests/test_contact.py b/tests/test_contact.py index 34047fa3658..f3a7c4338cb 100644 --- a/tests/test_contact.py +++ b/tests/test_contact.py @@ -67,6 +67,26 @@ def test_contact_to_dict(self): self.assertEqual(contact['last_name'], self.last_name) self.assertEqual(contact['user_id'], self.user_id) + def test_equality(self): + a = telegram.Contact(self.phone_number, self.first_name) + b = telegram.Contact(self.phone_number, self.first_name) + c = telegram.Contact(self.phone_number, "") + d = telegram.Contact("", self.first_name) + e = telegram.Voice("", 0) + + 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)) + ''' Commented out, because it would cause "Too Many Requests (429)" errors. @flaky(3, 1) diff --git a/tests/test_inlinequeryresultarticle.py b/tests/test_inlinequeryresultarticle.py index 5fb7b9360f9..18e5aaa3902 100644 --- a/tests/test_inlinequeryresultarticle.py +++ b/tests/test_inlinequeryresultarticle.py @@ -86,6 +86,26 @@ def test_article_to_dict(self): self.assertTrue(self.is_dict(article)) self.assertDictEqual(self.json_dict, article) + def test_equality(self): + a = telegram.InlineQueryResultArticle(self._id, self.title, self.input_message_content) + b = telegram.InlineQueryResultArticle(self._id, self.title, self.input_message_content) + c = telegram.InlineQueryResultArticle(self._id, "", self.input_message_content) + d = telegram.InlineQueryResultArticle("", self.title, self.input_message_content) + e = telegram.InlineQueryResultAudio(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() diff --git a/tests/test_inlinequeryresultaudio.py b/tests/test_inlinequeryresultaudio.py index 4a8bc405bd5..3f008ea3939 100644 --- a/tests/test_inlinequeryresultaudio.py +++ b/tests/test_inlinequeryresultaudio.py @@ -80,6 +80,26 @@ def test_audio_to_dict(self): self.assertTrue(self.is_dict(audio)) self.assertDictEqual(self.json_dict, audio) + def test_equality(self): + a = telegram.InlineQueryResultAudio(self._id, self.audio_url, self.title) + b = telegram.InlineQueryResultAudio(self._id, self.title, self.title) + c = telegram.InlineQueryResultAudio(self._id, "", self.title) + d = telegram.InlineQueryResultAudio("", self.audio_url, self.title) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultcontact.py b/tests/test_inlinequeryresultcontact.py index 2e66c5b2356..28bb5d208a7 100644 --- a/tests/test_inlinequeryresultcontact.py +++ b/tests/test_inlinequeryresultcontact.py @@ -82,6 +82,26 @@ def test_contact_to_dict(self): self.assertTrue(self.is_dict(contact)) self.assertDictEqual(self.json_dict, contact) + def test_equality(self): + a = telegram.InlineQueryResultContact(self._id, self.phone_number, self.first_name) + b = telegram.InlineQueryResultContact(self._id, self.phone_number, self.first_name) + c = telegram.InlineQueryResultContact(self._id, "", self.first_name) + d = telegram.InlineQueryResultContact("", self.phone_number, self.first_name) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultdocument.py b/tests/test_inlinequeryresultdocument.py index 82547159a7a..4cf4fcf237c 100644 --- a/tests/test_inlinequeryresultdocument.py +++ b/tests/test_inlinequeryresultdocument.py @@ -88,6 +88,28 @@ def test_document_to_dict(self): self.assertTrue(self.is_dict(document)) self.assertDictEqual(self.json_dict, document) + def test_equality(self): + a = telegram.InlineQueryResultDocument(self._id, self.document_url, self.title, + self.mime_type) + b = telegram.InlineQueryResultDocument(self._id, self.document_url, self.title, + self.mime_type) + c = telegram.InlineQueryResultDocument(self._id, "", self.title, self.mime_type) + d = telegram.InlineQueryResultDocument("", self.document_url, self.title, self.mime_type) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultgif.py b/tests/test_inlinequeryresultgif.py index 0320aa0f9c8..6df8618205d 100644 --- a/tests/test_inlinequeryresultgif.py +++ b/tests/test_inlinequeryresultgif.py @@ -83,6 +83,26 @@ def test_gif_to_dict(self): self.assertTrue(self.is_dict(gif)) self.assertDictEqual(self.json_dict, gif) + def test_equality(self): + a = telegram.InlineQueryResultGif(self._id, self.gif_url, self.thumb_url) + b = telegram.InlineQueryResultGif(self._id, self.gif_url, self.thumb_url) + c = telegram.InlineQueryResultGif(self._id, "", self.thumb_url) + d = telegram.InlineQueryResultGif("", self.gif_url, self.thumb_url) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultlocation.py b/tests/test_inlinequeryresultlocation.py index e547706f6fe..642594425f0 100644 --- a/tests/test_inlinequeryresultlocation.py +++ b/tests/test_inlinequeryresultlocation.py @@ -34,8 +34,8 @@ class InlineQueryResultLocationTest(BaseTest, unittest.TestCase): def setUp(self): self._id = 'id' self.type = 'location' - self.latitude = 'latitude' - self.longitude = 'longitude' + self.latitude = 0.0 + self.longitude = 0.0 self.title = 'title' self.thumb_url = 'thumb url' self.thumb_width = 10 @@ -82,6 +82,26 @@ def test_location_to_dict(self): self.assertTrue(self.is_dict(location)) self.assertDictEqual(self.json_dict, location) + def test_equality(self): + a = telegram.InlineQueryResultLocation(self._id, self.longitude, self.latitude, self.title) + b = telegram.InlineQueryResultLocation(self._id, self.longitude, self.latitude, self.title) + c = telegram.InlineQueryResultLocation(self._id, 0, self.latitude, self.title) + d = telegram.InlineQueryResultLocation("", self.longitude, self.latitude, self.title) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultmpeg4gif.py b/tests/test_inlinequeryresultmpeg4gif.py index 3b363944f3f..61ea994621f 100644 --- a/tests/test_inlinequeryresultmpeg4gif.py +++ b/tests/test_inlinequeryresultmpeg4gif.py @@ -83,6 +83,26 @@ def test_mpeg4_to_dict(self): self.assertTrue(self.is_dict(mpeg4)) self.assertDictEqual(self.json_dict, mpeg4) + def test_equality(self): + a = telegram.InlineQueryResultMpeg4Gif(self._id, self.mpeg4_url, self.thumb_url) + b = telegram.InlineQueryResultMpeg4Gif(self._id, self.mpeg4_url, self.thumb_url) + c = telegram.InlineQueryResultMpeg4Gif(self._id, "", self.thumb_url) + d = telegram.InlineQueryResultMpeg4Gif("", self.mpeg4_url, self.thumb_url) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultphoto.py b/tests/test_inlinequeryresultphoto.py index 39528a07e42..286b1826365 100644 --- a/tests/test_inlinequeryresultphoto.py +++ b/tests/test_inlinequeryresultphoto.py @@ -86,6 +86,26 @@ def test_photo_to_dict(self): self.assertTrue(self.is_dict(photo)) self.assertDictEqual(self.json_dict, photo) + def test_equality(self): + a = telegram.InlineQueryResultPhoto(self._id, self.photo_url, self.thumb_url) + b = telegram.InlineQueryResultPhoto(self._id, self.photo_url, self.thumb_url) + c = telegram.InlineQueryResultPhoto(self._id, "", self.thumb_url) + d = telegram.InlineQueryResultPhoto("", self.photo_url, self.thumb_url) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultvenue.py b/tests/test_inlinequeryresultvenue.py index ff0eb88b93b..b4ef2618ae0 100644 --- a/tests/test_inlinequeryresultvenue.py +++ b/tests/test_inlinequeryresultvenue.py @@ -88,6 +88,29 @@ def test_venue_to_dict(self): self.assertTrue(self.is_dict(venue)) self.assertDictEqual(self.json_dict, venue) + def test_equality(self): + a = telegram.InlineQueryResultVenue(self._id, self.longitude, self.latitude, self.title, + self._address) + b = telegram.InlineQueryResultVenue(self._id, self.longitude, self.latitude, self.title, + self._address) + c = telegram.InlineQueryResultVenue(self._id, "", self.latitude, self.title, self._address) + d = telegram.InlineQueryResultVenue("", self.longitude, self.latitude, self.title, + self._address) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultvideo.py b/tests/test_inlinequeryresultvideo.py index aba5a977ab1..8ed33762ad2 100644 --- a/tests/test_inlinequeryresultvideo.py +++ b/tests/test_inlinequeryresultvideo.py @@ -92,6 +92,30 @@ def test_video_to_dict(self): self.assertTrue(self.is_dict(video)) self.assertDictEqual(self.json_dict, video) + def test_equality(self): + a = telegram.InlineQueryResultVideo(self._id, self.video_url, self.mime_type, + self.thumb_url, self.title) + b = telegram.InlineQueryResultVideo(self._id, self.video_url, self.mime_type, + self.thumb_url, self.title) + c = telegram.InlineQueryResultVideo(self._id, "", self.mime_type, self.thumb_url, + self.title) + d = telegram.InlineQueryResultVideo("", self.video_url, self.mime_type, self.thumb_url, + self.title) + e = telegram.InlineQueryResultArticle(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() diff --git a/tests/test_inlinequeryresultvoice.py b/tests/test_inlinequeryresultvoice.py index 86d7abc9e86..6ba9330e700 100644 --- a/tests/test_inlinequeryresultvoice.py +++ b/tests/test_inlinequeryresultvoice.py @@ -77,6 +77,26 @@ def test_voice_to_dict(self): self.assertTrue(self.is_dict(voice)) self.assertDictEqual(self.json_dict, voice) + def test_equality(self): + a = telegram.InlineQueryResultVoice(self._id, self.voice_url, self.title) + b = telegram.InlineQueryResultVoice(self._id, self.voice_url, self.title) + c = telegram.InlineQueryResultVoice(self._id, "", self.title) + d = telegram.InlineQueryResultVoice("", self.voice_url, self.title) + e = telegram.InlineQueryResultArticle(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() From 67e8291f2ed344327247e242ba992288dcd7cbbd Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 18:21:52 +0200 Subject: [PATCH 09/19] Equality tests for more types ChosenInlineResult, InlineQuery, Message, Update and User --- tests/test_choseninlineresult.py | 20 ++++++++++++++++++++ tests/test_inlinequery.py | 22 +++++++++++++++++++++- tests/test_message.py | 24 ++++++++++++++++++++++-- tests/test_update.py | 20 ++++++++++++++++++++ tests/test_user.py | 20 ++++++++++++++++++++ 5 files changed, 103 insertions(+), 3 deletions(-) diff --git a/tests/test_choseninlineresult.py b/tests/test_choseninlineresult.py index db639dbebee..130057ced0e 100644 --- a/tests/test_choseninlineresult.py +++ b/tests/test_choseninlineresult.py @@ -64,6 +64,26 @@ def test_choseninlineresult_to_dict(self): self.assertEqual(result['from'], self.from_user.to_dict()) self.assertEqual(result['query'], self.query) + def test_equality(self): + a = telegram.ChosenInlineResult(self.result_id, None, "Query", "") + b = telegram.ChosenInlineResult(self.result_id, None, "Query", "") + c = telegram.ChosenInlineResult(self.result_id, None, "", "") + d = telegram.ChosenInlineResult("", None, "Query", "") + e = telegram.Voice(self.result_id, 0) + + 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() diff --git a/tests/test_inlinequery.py b/tests/test_inlinequery.py index 349cee48ceb..b13bce90a24 100644 --- a/tests/test_inlinequery.py +++ b/tests/test_inlinequery.py @@ -35,7 +35,7 @@ def setUp(self): user = telegram.User(1, 'First name') location = telegram.Location(8.8, 53.1) - self._id = 'id' + self._id = 1234 self.from_user = user self.query = 'query text' self.offset = 'offset' @@ -69,6 +69,26 @@ def test_inlinequery_to_dict(self): self.assertTrue(self.is_dict(inlinequery)) self.assertDictEqual(inlinequery, self.json_dict) + def test_equality(self): + a = telegram.InlineQuery(self._id, telegram.User(1, ""), "", "") + b = telegram.InlineQuery(self._id, telegram.User(1, ""), "", "") + c = telegram.InlineQuery(self._id, telegram.User(0, ""), "", "") + d = telegram.InlineQuery(0, telegram.User(1, ""), "", "") + e = telegram.Update(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() diff --git a/tests/test_message.py b/tests/test_message.py index 8b8b4870c26..e1cceda2c52 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -99,8 +99,7 @@ def test_parse_entities(self): entity_2: 'h'}) def test_text_html(self): - test_html_string = 'Test for bold, italic, code, ' \ - 'links and
pre
.' + test_html_string = 'Test for bold, italic, code, ' 'links and
pre
.' text_html = self.test_message.text_html self.assertEquals(test_html_string, text_html) @@ -156,6 +155,27 @@ def test_delete2(self): # NOTE: This behaviour can be changed in future. See `tests/test_bot.py` for more info message.reply_to_message.delete() + def test_equality(self): + _id = 1 + a = telegram.Message(_id, telegram.User(1, ""), None, None) + b = telegram.Message(_id, telegram.User(1, ""), None, None) + c = telegram.Message(_id, telegram.User(0, ""), None, None) + d = telegram.Message(0, telegram.User(1, ""), None, None) + e = telegram.Update(_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() diff --git a/tests/test_update.py b/tests/test_update.py index b326d9ecc28..05b8e6052a3 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -91,6 +91,26 @@ def test_effective_message(self): message = update.effective_message self.assertEqual(update.message.text, message.text) + def test_equality(self): + a = telegram.Update(self.update_id, message=self.message) + b = telegram.Update(self.update_id, message=self.message) + c = telegram.Update(self.update_id) + d = telegram.Update(0, message=self.message) + e = telegram.User(self.update_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() diff --git a/tests/test_user.py b/tests/test_user.py index 86cfe9e1b0d..dca5ca9d6c5 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -111,6 +111,26 @@ def test_get_profile_photos(self): self.assertNotEquals(result, None) + def test_equality(self): + a = telegram.User(self._id, self.first_name, self.last_name) + b = telegram.User(self._id, self.first_name, self.last_name) + c = telegram.User(self._id, self.first_name) + d = telegram.User(0, self.first_name, self.last_name) + e = telegram.Update(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() From bfe808c263e1db93f69a1c06f5c097927708f933 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 18:31:51 +0200 Subject: [PATCH 10/19] Equality tests for cached inlinequeryresults --- tests/test_inlinequeryresultcachedaudio.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcacheddocument.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcachedgif.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcachedmpeg4gif.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcachedphoto.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcachedsticker.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcachedvideo.py | 20 +++++++++++++++++++ tests/test_inlinequeryresultcachedvoice.py | 20 +++++++++++++++++++ 8 files changed, 160 insertions(+) diff --git a/tests/test_inlinequeryresultcachedaudio.py b/tests/test_inlinequeryresultcachedaudio.py index 48fc5533eea..5d797006a6f 100644 --- a/tests/test_inlinequeryresultcachedaudio.py +++ b/tests/test_inlinequeryresultcachedaudio.py @@ -72,6 +72,26 @@ def test_audio_to_dict(self): self.assertTrue(self.is_dict(audio)) self.assertDictEqual(self.json_dict, audio) + def test_equality(self): + a = telegram.InlineQueryResultCachedAudio(self._id, self.audio_file_id) + b = telegram.InlineQueryResultCachedAudio(self._id, self.audio_file_id) + c = telegram.InlineQueryResultCachedAudio(self._id, "") + d = telegram.InlineQueryResultCachedAudio("", self.audio_file_id) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcacheddocument.py b/tests/test_inlinequeryresultcacheddocument.py index 732b8e3a2d4..8ff83c82034 100644 --- a/tests/test_inlinequeryresultcacheddocument.py +++ b/tests/test_inlinequeryresultcacheddocument.py @@ -78,6 +78,26 @@ def test_document_to_dict(self): self.assertTrue(self.is_dict(document)) self.assertDictEqual(self.json_dict, document) + def test_equality(self): + a = telegram.InlineQueryResultCachedDocument(self._id, self.title, self.document_file_id) + b = telegram.InlineQueryResultCachedDocument(self._id, self.title, self.document_file_id) + c = telegram.InlineQueryResultCachedDocument(self._id, self.title, "") + d = telegram.InlineQueryResultCachedDocument("", self.title, self.document_file_id) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcachedgif.py b/tests/test_inlinequeryresultcachedgif.py index 3e8452d2bd4..c3e2166b1f3 100644 --- a/tests/test_inlinequeryresultcachedgif.py +++ b/tests/test_inlinequeryresultcachedgif.py @@ -74,6 +74,26 @@ def test_gif_to_dict(self): self.assertTrue(self.is_dict(gif)) self.assertDictEqual(self.json_dict, gif) + def test_equality(self): + a = telegram.InlineQueryResultCachedGif(self._id, self.gif_file_id) + b = telegram.InlineQueryResultCachedGif(self._id, self.gif_file_id) + c = telegram.InlineQueryResultCachedGif(self._id, "") + d = telegram.InlineQueryResultCachedGif("", self.gif_file_id) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcachedmpeg4gif.py b/tests/test_inlinequeryresultcachedmpeg4gif.py index 27b70615858..1e4a21ed641 100644 --- a/tests/test_inlinequeryresultcachedmpeg4gif.py +++ b/tests/test_inlinequeryresultcachedmpeg4gif.py @@ -76,6 +76,26 @@ def test_mpeg4_to_dict(self): self.assertTrue(self.is_dict(mpeg4)) self.assertDictEqual(self.json_dict, mpeg4) + def test_equality(self): + a = telegram.InlineQueryResultCachedMpeg4Gif(self._id, self.mpeg4_file_id) + b = telegram.InlineQueryResultCachedMpeg4Gif(self._id, self.mpeg4_file_id) + c = telegram.InlineQueryResultCachedMpeg4Gif(self._id, "") + d = telegram.InlineQueryResultCachedMpeg4Gif("", self.mpeg4_file_id) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcachedphoto.py b/tests/test_inlinequeryresultcachedphoto.py index de19443d32b..8606afa6ad8 100644 --- a/tests/test_inlinequeryresultcachedphoto.py +++ b/tests/test_inlinequeryresultcachedphoto.py @@ -78,6 +78,26 @@ def test_photo_to_dict(self): self.assertTrue(self.is_dict(photo)) self.assertDictEqual(self.json_dict, photo) + def test_equality(self): + a = telegram.InlineQueryResultCachedPhoto(self._id, self.photo_file_id) + b = telegram.InlineQueryResultCachedPhoto(self._id, self.photo_file_id) + c = telegram.InlineQueryResultCachedPhoto(self._id, "") + d = telegram.InlineQueryResultCachedPhoto("", self.photo_file_id) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcachedsticker.py b/tests/test_inlinequeryresultcachedsticker.py index 8ec44d10c4c..8d956e46064 100644 --- a/tests/test_inlinequeryresultcachedsticker.py +++ b/tests/test_inlinequeryresultcachedsticker.py @@ -70,6 +70,26 @@ def test_sticker_to_dict(self): self.assertTrue(self.is_dict(sticker)) self.assertDictEqual(self.json_dict, sticker) + def test_equality(self): + a = telegram.InlineQueryResultCachedSticker(self._id, self.sticker_file_id) + b = telegram.InlineQueryResultCachedSticker(self._id, self.sticker_file_id) + c = telegram.InlineQueryResultCachedSticker(self._id, "") + d = telegram.InlineQueryResultCachedSticker("", self.sticker_file_id) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcachedvideo.py b/tests/test_inlinequeryresultcachedvideo.py index a98eedb826f..5cdcc0e2a8a 100644 --- a/tests/test_inlinequeryresultcachedvideo.py +++ b/tests/test_inlinequeryresultcachedvideo.py @@ -78,6 +78,26 @@ def test_video_to_dict(self): self.assertTrue(self.is_dict(video)) self.assertDictEqual(self.json_dict, video) + def test_equality(self): + a = telegram.InlineQueryResultCachedVideo(self._id, self.video_file_id, self.title) + b = telegram.InlineQueryResultCachedVideo(self._id, self.video_file_id, self.title) + c = telegram.InlineQueryResultCachedVideo(self._id, "", self.title) + d = telegram.InlineQueryResultCachedVideo("", self.video_file_id, self.title) + e = telegram.InlineQueryResultCachedVoice(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() diff --git a/tests/test_inlinequeryresultcachedvoice.py b/tests/test_inlinequeryresultcachedvoice.py index 4e24b526992..dca554d80b5 100644 --- a/tests/test_inlinequeryresultcachedvoice.py +++ b/tests/test_inlinequeryresultcachedvoice.py @@ -75,6 +75,26 @@ def test_voice_to_dict(self): self.assertTrue(self.is_dict(voice)) self.assertDictEqual(self.json_dict, voice) + def test_equality(self): + a = telegram.InlineQueryResultCachedVoice(self._id, self.voice_file_id, self.title) + b = telegram.InlineQueryResultCachedVoice(self._id, self.voice_file_id, self.title) + c = telegram.InlineQueryResultCachedVoice(self._id, "", self.title) + d = telegram.InlineQueryResultCachedVoice("", self.voice_file_id, self.title) + e = telegram.InlineQueryResultCachedVoice(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() From faa1e9845c94bebb690f0a3346514c1b6ca39869 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 18:35:30 +0200 Subject: [PATCH 11/19] Fix equality tests for photosize and location --- telegram/photosize.py | 6 ------ tests/test_location.py | 4 ---- 2 files changed, 10 deletions(-) diff --git a/telegram/photosize.py b/telegram/photosize.py index 5bd2760a0d7..2270a16822a 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -48,12 +48,6 @@ 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) - @staticmethod def de_json(data, bot): """ diff --git a/tests/test_location.py b/tests/test_location.py index f32823469a9..b903bb4d03a 100644 --- a/tests/test_location.py +++ b/tests/test_location.py @@ -104,16 +104,12 @@ def test_reply_location(self): def test_equality(self): a = telegram.Location(self.longitude, self.latitude) b = telegram.Location(self.longitude, self.latitude) - c = telegram.Location(self.longitude, 0) d = telegram.Location(0, self.latitude) 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)) From ce25412a42dcc056191d4e74a4f95e7c63ad50ea Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 18:39:26 +0200 Subject: [PATCH 12/19] Whoops, compare with other not self --- telegram/contact.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/contact.py b/telegram/contact.py index 69ebadbd3c6..b9f23225afc 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -64,7 +64,7 @@ def de_json(data, bot): def __eq__(self, other): if isinstance(other, self.__class__): - return self.phone_number == self.phone_number + return self.phone_number == other.phone_number return NotImplemented def __hash__(self): From 1419561577aca9b74702271510018a83b8f04a7a Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 18:40:15 +0200 Subject: [PATCH 13/19] Whoops, compare with another type --- tests/test_inlinequeryresultcachedvoice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_inlinequeryresultcachedvoice.py b/tests/test_inlinequeryresultcachedvoice.py index dca554d80b5..6ca9783d6b4 100644 --- a/tests/test_inlinequeryresultcachedvoice.py +++ b/tests/test_inlinequeryresultcachedvoice.py @@ -80,7 +80,7 @@ def test_equality(self): b = telegram.InlineQueryResultCachedVoice(self._id, self.voice_file_id, self.title) c = telegram.InlineQueryResultCachedVoice(self._id, "", self.title) d = telegram.InlineQueryResultCachedVoice("", self.voice_file_id, self.title) - e = telegram.InlineQueryResultCachedVoice(self._id, "", "") + e = telegram.InlineQueryResultCachedAudio(self._id, "", "") self.assertEqual(a, b) self.assertEqual(hash(a), hash(b)) From e596fce74e19937b89245a1a390022a003672253 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 19:28:09 +0200 Subject: [PATCH 14/19] Add ChatMember test --- tests/test_chatmember.py | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/test_chatmember.py diff --git a/tests/test_chatmember.py b/tests/test_chatmember.py new file mode 100644 index 00000000000..0d01895a213 --- /dev/null +++ b/tests/test_chatmember.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2016 +# Leandro Toledo de Souza +# +# 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 ChatMember""" + +import unittest +import sys + +sys.path.append('.') + +import telegram +from tests.base import BaseTest + + +class ChatMemberTest(BaseTest, unittest.TestCase): + """This object represents Tests for Telegram ChatMember.""" + + def setUp(self): + self.user = {'id': 1, 'first_name': 'User'} + self.status = telegram.ChatMember.CREATOR + + self.json_dict = {'user': self.user, 'status': self.status} + + def test_chatmember_de_json(self): + chatmember = telegram.ChatMember.de_json(self.json_dict, self._bot) + + self.assertEqual(chatmember.user.to_dict(), self.user) + self.assertEqual(chatmember.status, self.status) + + def test_chatmember_to_json(self): + chatmember = telegram.ChatMember.de_json(self.json_dict, self._bot) + + self.assertTrue(self.is_json(chatmember.to_json())) + + def test_chatmember_to_dict(self): + chatmember = telegram.ChatMember.de_json(self.json_dict, self._bot) + + self.assertTrue(self.is_dict(chatmember.to_dict())) + self.assertEqual(chatmember['user'].to_dict(), self.user) + self.assertEqual(chatmember['status'], self.status) + + def test_equality(self): + a = telegram.ChatMember(telegram.User(1, ""), telegram.ChatMember.ADMINISTRATOR) + b = telegram.ChatMember(telegram.User(1, ""), telegram.ChatMember.ADMINISTRATOR) + d = telegram.ChatMember(telegram.User(2, ""), telegram.ChatMember.ADMINISTRATOR) + d2 = telegram.ChatMember(telegram.User(1, ""), telegram.ChatMember.CREATOR) + + 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, d2) + self.assertNotEqual(hash(a), hash(d2)) From 90a0a59040ae93e98ee8cd6d4b73215ce363f019 Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 19:31:26 +0200 Subject: [PATCH 15/19] Fix super() call on pu2 --- telegram/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/base.py b/telegram/base.py index ab93786000a..a8bfe336860 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -101,4 +101,4 @@ def __hash__(self): self.__class__, self._get_id(),)) except NotImplementedError: - return super().__hash__() + return super(TelegramObject, self).__hash__() From 590f6f5009a3cc1d0dee138e63b895285f153c2d Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 20:44:00 +0200 Subject: [PATCH 16/19] Use getattr instead of __getattribute__ --- telegram/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/base.py b/telegram/base.py index a8bfe336860..7a0e120d42e 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -84,7 +84,7 @@ def to_dict(self): def _get_id(self): for x in ['id', 'file_id', 'message_id', 'result_id', 'update_id']: if hasattr(self, x): - return self.__getattribute__(x) + return getattr(self, x) raise NotImplementedError def __eq__(self, other): From 2bb3ab004a3e03c289ab42a02f6d2ebec438e79f Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sat, 13 May 2017 23:57:50 +0200 Subject: [PATCH 17/19] Do equality checks via _id_keys Also add forgotten equality check in test_chat --- telegram/animation.py | 2 ++ telegram/audio.py | 2 ++ telegram/base.py | 22 ++++++++-------------- telegram/chat.py | 2 ++ telegram/chatmember.py | 10 ++-------- telegram/choseninlineresult.py | 2 ++ telegram/contact.py | 10 ++-------- telegram/document.py | 2 ++ telegram/file.py | 2 ++ telegram/inlinequery.py | 2 ++ telegram/inlinequeryresult.py | 2 ++ telegram/location.py | 10 ++-------- telegram/message.py | 2 ++ telegram/photosize.py | 2 ++ telegram/sticker.py | 2 ++ telegram/update.py | 2 ++ telegram/user.py | 2 ++ telegram/venue.py | 10 ++-------- telegram/video.py | 2 ++ telegram/voice.py | 2 ++ tests/test_chat.py | 20 ++++++++++++++++++++ 21 files changed, 66 insertions(+), 46 deletions(-) diff --git a/telegram/animation.py b/telegram/animation.py index 0e54b91c07f..f67ceb0f0bc 100644 --- a/telegram/animation.py +++ b/telegram/animation.py @@ -35,6 +35,8 @@ class Animation(TelegramObject): """ + _id_keys = ('file_id',) + def __init__(self, file_id, thumb=None, diff --git a/telegram/audio.py b/telegram/audio.py index 9885cdf9636..520cdc42358 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -43,6 +43,8 @@ class Audio(TelegramObject): """ + _id_keys = ('file_id',) + def __init__(self, file_id, duration, diff --git a/telegram/base.py b/telegram/base.py index 7a0e120d42e..15583ada21c 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -81,24 +81,18 @@ def to_dict(self): return data + _id_keys = () + def _get_id(self): - for x in ['id', 'file_id', 'message_id', 'result_id', 'update_id']: - if hasattr(self, x): - return getattr(self, x) - raise NotImplementedError + return tuple(getattr(self, x) for x in self._id_keys) def __eq__(self, other): if isinstance(other, self.__class__): - try: - return self._get_id() == other._get_id() - except NotImplementedError: - pass # return NotImplemented - return NotImplemented + return self._get_id() == other._get_id() + return super(TelegramObject, self).__eq__(other) def __hash__(self): - try: - return hash(( - self.__class__, - self._get_id(),)) - except NotImplementedError: + if self._id_keys: + return hash((self.__class__, self._get_id())) + else: return super(TelegramObject, self).__hash__() diff --git a/telegram/chat.py b/telegram/chat.py index cebe7edcf4d..528c77bf40c 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -50,6 +50,8 @@ class Chat(TelegramObject): SUPERGROUP = 'supergroup' CHANNEL = 'channel' + _id_keys = ('id',) + def __init__(self, id, type, diff --git a/telegram/chatmember.py b/telegram/chatmember.py index 73f6de83976..26de2505ed5 100644 --- a/telegram/chatmember.py +++ b/telegram/chatmember.py @@ -41,6 +41,8 @@ class ChatMember(TelegramObject): LEFT = 'left' KICKED = 'kicked' + _id_keys = ('user', 'status') + def __init__(self, user, status, **kwargs): # Required self.user = user @@ -64,11 +66,3 @@ def de_json(data, bot): data['user'] = User.de_json(data.get('user'), bot) return ChatMember(**data) - - def __eq__(self, other): - if isinstance(other, self.__class__): - return self.user == other.user and self.status == other.status - return NotImplemented - - def __hash__(self): - return hash((self.__class__, self.user, self.status)) diff --git a/telegram/choseninlineresult.py b/telegram/choseninlineresult.py index d779676d05b..b8efdadc49d 100644 --- a/telegram/choseninlineresult.py +++ b/telegram/choseninlineresult.py @@ -46,6 +46,8 @@ class ChosenInlineResult(TelegramObject): """ + _id_keys = ('result_id',) + def __init__(self, result_id, from_user, diff --git a/telegram/contact.py b/telegram/contact.py index b9f23225afc..65df18689ff 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -39,6 +39,8 @@ class Contact(TelegramObject): """ + _id_keys = ('phone_number',) + def __init__(self, phone_number, first_name, last_name=None, user_id=None, **kwargs): # Required self.phone_number = str(phone_number) @@ -61,11 +63,3 @@ def de_json(data, bot): return None return Contact(**data) - - def __eq__(self, other): - if isinstance(other, self.__class__): - return self.phone_number == other.phone_number - return NotImplemented - - def __hash__(self): - return hash(self.phone_number) diff --git a/telegram/document.py b/telegram/document.py index 59fb6141e94..26e09a68b4a 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -41,6 +41,8 @@ class Document(TelegramObject): """ + _id_keys = ('file_id',) + def __init__(self, file_id, thumb=None, diff --git a/telegram/file.py b/telegram/file.py index 01c7ad8baf3..15c9cbfc9ad 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -40,6 +40,8 @@ class File(TelegramObject): """ + _id_keys = ('file_id',) + def __init__(self, file_id, bot, file_size=None, file_path=None, **kwargs): # Required self.file_id = str(file_id) diff --git a/telegram/inlinequery.py b/telegram/inlinequery.py index dbc4a07e1de..b3f198bd83e 100644 --- a/telegram/inlinequery.py +++ b/telegram/inlinequery.py @@ -44,6 +44,8 @@ class InlineQuery(TelegramObject): """ + _id_keys = ('id',) + def __init__(self, id, from_user, query, offset, location=None, bot=None, **kwargs): # Required self.id = id diff --git a/telegram/inlinequeryresult.py b/telegram/inlinequeryresult.py index b8afa219a3d..4f60d4c4fa4 100644 --- a/telegram/inlinequeryresult.py +++ b/telegram/inlinequeryresult.py @@ -36,6 +36,8 @@ class InlineQueryResult(TelegramObject): """ + _id_keys = ('id',) + def __init__(self, type, id, **kwargs): # Required self.type = str(type) diff --git a/telegram/location.py b/telegram/location.py index 958c62e9f80..d661bd0d746 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -33,6 +33,8 @@ class Location(TelegramObject): latitude (float): """ + _id_keys = ('longitude', 'latitude') + def __init__(self, longitude, latitude, **kwargs): # Required self.longitude = float(longitude) @@ -52,11 +54,3 @@ def de_json(data, bot): return None return Location(**data) - - def __eq__(self, other): - if isinstance(other, self.__class__): - return self.longitude == other.longitude and self.latitude == other.latitude - return NotImplemented - - def __hash__(self): - return hash((self.__class__, self.longitude, self.latitude)) diff --git a/telegram/message.py b/telegram/message.py index c69292e3636..46e68915c33 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -106,6 +106,8 @@ class Message(TelegramObject): bot (Optional[Bot]): The Bot to use for instance methods """ + _id_keys = ('message_id',) + def __init__(self, message_id, from_user, diff --git a/telegram/photosize.py b/telegram/photosize.py index 2270a16822a..78055e4a703 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -40,6 +40,8 @@ class PhotoSize(TelegramObject): file_size (Optional[int]): """ + _id_keys = ('file_id',) + def __init__(self, file_id, width, height, file_size=None, **kwargs): # Required self.file_id = str(file_id) diff --git a/telegram/sticker.py b/telegram/sticker.py index 8a8bbb1c4db..11194e9afd5 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -44,6 +44,8 @@ class Sticker(TelegramObject): file_size (Optional[int]): """ + _id_keys = ('file_id',) + def __init__(self, file_id, width, height, thumb=None, emoji=None, file_size=None, **kwargs): # Required self.file_id = str(file_id) diff --git a/telegram/update.py b/telegram/update.py index 42668350d85..9b4856000fc 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -52,6 +52,8 @@ class Update(TelegramObject): """ + _id_keys = ('update_id',) + def __init__(self, update_id, message=None, diff --git a/telegram/user.py b/telegram/user.py index 215bb822ef5..e248486b411 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -44,6 +44,8 @@ class User(TelegramObject): bot (Optional[Bot]): The Bot to use for instance methods """ + _id_keys = ('id',) + def __init__(self, id, first_name, diff --git a/telegram/venue.py b/telegram/venue.py index b42466c572d..4daea9ac054 100644 --- a/telegram/venue.py +++ b/telegram/venue.py @@ -32,6 +32,8 @@ class Venue(TelegramObject): foursquare_id (Optional[str]): """ + _id_keys = ('location', 'title') + def __init__(self, location, title, address, foursquare_id=None, **kwargs): # Required self.location = location @@ -50,11 +52,3 @@ def de_json(data, bot): data['location'] = Location.de_json(data.get('location'), bot) return Venue(**data) - - def __eq__(self, other): - if isinstance(other, self.__class__): - return self.location == other.location and self.title == other.title - return NotImplemented - - def __hash__(self): - return hash((self.__class__, self.location, self.title)) diff --git a/telegram/video.py b/telegram/video.py index 0a91b515f92..986ae90aa38 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -46,6 +46,8 @@ class Video(TelegramObject): file_size (Optional[int]): """ + _id_keys = ('file_id',) + def __init__(self, file_id, width, diff --git a/telegram/voice.py b/telegram/voice.py index 0dccbaba6b1..a881d35b634 100644 --- a/telegram/voice.py +++ b/telegram/voice.py @@ -40,6 +40,8 @@ class Voice(TelegramObject): file_size (Optional[int]): """ + _id_keys = ('file_id',) + def __init__(self, file_id, duration, mime_type=None, file_size=None, **kwargs): # Required self.file_id = str(file_id) diff --git a/tests/test_chat.py b/tests/test_chat.py index 3fc5efbe53d..233be72673a 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -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() From 98974a145b00d14fd0791a71bbe7f69bd183cefc Mon Sep 17 00:00:00 2001 From: Jacob Bom Date: Sun, 14 May 2017 00:40:13 +0200 Subject: [PATCH 18/19] Do equality checks via _id_attrs --- telegram/animation.py | 4 ++-- telegram/audio.py | 4 ++-- telegram/base.py | 16 ++++++---------- telegram/chat.py | 3 +-- telegram/chatmember.py | 4 ++-- telegram/choseninlineresult.py | 4 ++-- telegram/contact.py | 4 ++-- telegram/document.py | 2 ++ telegram/file.py | 4 ++-- telegram/inlinequery.py | 3 +-- telegram/inlinequeryresult.py | 4 ++-- telegram/location.py | 4 ++-- telegram/message.py | 4 ++-- telegram/photosize.py | 4 ++-- telegram/sticker.py | 4 ++-- telegram/update.py | 4 ++-- telegram/user.py | 4 ++-- telegram/venue.py | 4 ++-- telegram/video.py | 4 ++-- telegram/voice.py | 4 ++-- 20 files changed, 42 insertions(+), 46 deletions(-) diff --git a/telegram/animation.py b/telegram/animation.py index f67ceb0f0bc..62d6a2a0630 100644 --- a/telegram/animation.py +++ b/telegram/animation.py @@ -35,8 +35,6 @@ class Animation(TelegramObject): """ - _id_keys = ('file_id',) - def __init__(self, file_id, thumb=None, @@ -50,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): """ diff --git a/telegram/audio.py b/telegram/audio.py index 520cdc42358..acfbf69a4d5 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -43,8 +43,6 @@ class Audio(TelegramObject): """ - _id_keys = ('file_id',) - def __init__(self, file_id, duration, @@ -62,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): """ diff --git a/telegram/base.py b/telegram/base.py index 15583ada21c..78dfcfd1212 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -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] @@ -81,18 +81,14 @@ def to_dict(self): return data - _id_keys = () - - def _get_id(self): - return tuple(getattr(self, x) for x in self._id_keys) + _id_attrs = () def __eq__(self, other): if isinstance(other, self.__class__): - return self._get_id() == other._get_id() + return self._id_attrs == other._id_attrs return super(TelegramObject, self).__eq__(other) def __hash__(self): - if self._id_keys: - return hash((self.__class__, self._get_id())) - else: - return super(TelegramObject, self).__hash__() + if self._id_attrs: + return hash((self.__class__, self._id_attrs)) + return super(TelegramObject, self).__hash__() diff --git a/telegram/chat.py b/telegram/chat.py index 528c77bf40c..54450be2801 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -50,8 +50,6 @@ class Chat(TelegramObject): SUPERGROUP = 'supergroup' CHANNEL = 'channel' - _id_keys = ('id',) - def __init__(self, id, type, @@ -73,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): diff --git a/telegram/chatmember.py b/telegram/chatmember.py index 26de2505ed5..f08846bff48 100644 --- a/telegram/chatmember.py +++ b/telegram/chatmember.py @@ -41,13 +41,13 @@ class ChatMember(TelegramObject): LEFT = 'left' KICKED = 'kicked' - _id_keys = ('user', 'status') - def __init__(self, user, status, **kwargs): # Required self.user = user self.status = status + self._id_attrs = (self.user, self.status) + @staticmethod def de_json(data, bot): """ diff --git a/telegram/choseninlineresult.py b/telegram/choseninlineresult.py index b8efdadc49d..df6d2ef99bd 100644 --- a/telegram/choseninlineresult.py +++ b/telegram/choseninlineresult.py @@ -46,8 +46,6 @@ class ChosenInlineResult(TelegramObject): """ - _id_keys = ('result_id',) - def __init__(self, result_id, from_user, @@ -63,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): """ diff --git a/telegram/contact.py b/telegram/contact.py index 65df18689ff..f493c5a8049 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -39,8 +39,6 @@ class Contact(TelegramObject): """ - _id_keys = ('phone_number',) - def __init__(self, phone_number, first_name, last_name=None, user_id=None, **kwargs): # Required self.phone_number = str(phone_number) @@ -49,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): """ diff --git a/telegram/document.py b/telegram/document.py index 26e09a68b4a..af64dce4c9a 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -58,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): """ diff --git a/telegram/file.py b/telegram/file.py index 15c9cbfc9ad..a1ac5c127d4 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -40,8 +40,6 @@ class File(TelegramObject): """ - _id_keys = ('file_id',) - def __init__(self, file_id, bot, file_size=None, file_path=None, **kwargs): # Required self.file_id = str(file_id) @@ -53,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): """ diff --git a/telegram/inlinequery.py b/telegram/inlinequery.py index b3f198bd83e..732e23fe8d7 100644 --- a/telegram/inlinequery.py +++ b/telegram/inlinequery.py @@ -44,8 +44,6 @@ class InlineQuery(TelegramObject): """ - _id_keys = ('id',) - def __init__(self, id, from_user, query, offset, location=None, bot=None, **kwargs): # Required self.id = id @@ -57,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): diff --git a/telegram/inlinequeryresult.py b/telegram/inlinequeryresult.py index 4f60d4c4fa4..f4898bb5b47 100644 --- a/telegram/inlinequeryresult.py +++ b/telegram/inlinequeryresult.py @@ -36,13 +36,13 @@ class InlineQueryResult(TelegramObject): """ - _id_keys = ('id',) - def __init__(self, type, id, **kwargs): # Required 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) diff --git a/telegram/location.py b/telegram/location.py index d661bd0d746..c799e32eebd 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -33,13 +33,13 @@ class Location(TelegramObject): latitude (float): """ - _id_keys = ('longitude', 'latitude') - def __init__(self, longitude, latitude, **kwargs): # Required self.longitude = float(longitude) self.latitude = float(latitude) + self._id_attrs = (self.longitude, self.latitude) + @staticmethod def de_json(data, bot): """ diff --git a/telegram/message.py b/telegram/message.py index 46e68915c33..e5f8b67f7d5 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -106,8 +106,6 @@ class Message(TelegramObject): bot (Optional[Bot]): The Bot to use for instance methods """ - _id_keys = ('message_id',) - def __init__(self, message_id, from_user, @@ -183,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`""" diff --git a/telegram/photosize.py b/telegram/photosize.py index 78055e4a703..2371913ef5f 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -40,8 +40,6 @@ class PhotoSize(TelegramObject): file_size (Optional[int]): """ - _id_keys = ('file_id',) - def __init__(self, file_id, width, height, file_size=None, **kwargs): # Required self.file_id = str(file_id) @@ -50,6 +48,8 @@ def __init__(self, file_id, width, height, file_size=None, **kwargs): # Optionals self.file_size = file_size + self._id_attrs = (self.file_id,) + @staticmethod def de_json(data, bot): """ diff --git a/telegram/sticker.py b/telegram/sticker.py index 11194e9afd5..89f27b44712 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -44,8 +44,6 @@ class Sticker(TelegramObject): file_size (Optional[int]): """ - _id_keys = ('file_id',) - def __init__(self, file_id, width, height, thumb=None, emoji=None, file_size=None, **kwargs): # Required self.file_id = str(file_id) @@ -56,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): """ diff --git a/telegram/update.py b/telegram/update.py index 9b4856000fc..e768ab223bd 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -52,8 +52,6 @@ class Update(TelegramObject): """ - _id_keys = ('update_id',) - def __init__(self, update_id, message=None, @@ -79,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): """ diff --git a/telegram/user.py b/telegram/user.py index e248486b411..48410ae222f 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -44,8 +44,6 @@ class User(TelegramObject): bot (Optional[Bot]): The Bot to use for instance methods """ - _id_keys = ('id',) - def __init__(self, id, first_name, @@ -64,6 +62,8 @@ def __init__(self, self.bot = bot + self._id_attrs = (self.id,) + @property def name(self): """str: """ diff --git a/telegram/venue.py b/telegram/venue.py index 4daea9ac054..19d5f156435 100644 --- a/telegram/venue.py +++ b/telegram/venue.py @@ -32,8 +32,6 @@ class Venue(TelegramObject): foursquare_id (Optional[str]): """ - _id_keys = ('location', 'title') - def __init__(self, location, title, address, foursquare_id=None, **kwargs): # Required self.location = location @@ -42,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) diff --git a/telegram/video.py b/telegram/video.py index 986ae90aa38..a6904215ddc 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -46,8 +46,6 @@ class Video(TelegramObject): file_size (Optional[int]): """ - _id_keys = ('file_id',) - def __init__(self, file_id, width, @@ -67,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): """ diff --git a/telegram/voice.py b/telegram/voice.py index a881d35b634..9bf5f89336d 100644 --- a/telegram/voice.py +++ b/telegram/voice.py @@ -40,8 +40,6 @@ class Voice(TelegramObject): file_size (Optional[int]): """ - _id_keys = ('file_id',) - def __init__(self, file_id, duration, mime_type=None, file_size=None, **kwargs): # Required self.file_id = str(file_id) @@ -50,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): """ From dc79f40e8bee5891274ffa8cafdfce9575ee51f0 Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Mon, 15 May 2017 00:25:58 +0300 Subject: [PATCH 19/19] base.py: It is customary to declare attributes at the head of a class --- telegram/base.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/telegram/base.py b/telegram/base.py index 78dfcfd1212..a420534b38e 100644 --- a/telegram/base.py +++ b/telegram/base.py @@ -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()) @@ -81,8 +81,6 @@ def to_dict(self): return data - _id_attrs = () - def __eq__(self, other): if isinstance(other, self.__class__): return self._id_attrs == other._id_attrs pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy