python-telegram-bot/tests/test_location.py

119 lines
4 KiB
Python
Raw Normal View History

2015-09-07 19:31:08 +02:00
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
2015-09-07 19:31:08 +02:00
#
# 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/].
2016-10-17 00:22:40 +02:00
"""This module contains an object that represents Tests for Telegram Location"""
2015-09-07 19:31:08 +02:00
2016-04-24 04:11:25 +02:00
import unittest
2015-09-07 19:31:08 +02:00
import sys
from flaky import flaky
2015-09-07 19:31:08 +02:00
sys.path.append('.')
import telegram
from tests.base import BaseTest
class LocationTest(BaseTest, unittest.TestCase):
"""This object represents Tests for Telegram Location."""
def setUp(self):
2015-09-10 21:59:08 +02:00
self.latitude = -23.691288
self.longitude = -46.788279
2015-09-07 19:31:08 +02:00
2016-05-15 02:52:35 +02:00
self.json_dict = {'latitude': self.latitude, 'longitude': self.longitude}
2015-09-07 19:31:08 +02:00
def test_send_location_implicit_args(self):
2016-05-15 02:52:35 +02:00
message = self._bot.sendLocation(self._chat_id, self.latitude, self.longitude)
2015-09-07 19:31:08 +02:00
location = message.location
self.assertEqual(location.latitude, self.latitude)
self.assertEqual(location.longitude, self.longitude)
def test_send_location_explicit_args(self):
2016-08-26 11:17:05 +02:00
message = self._bot.sendLocation(
chat_id=self._chat_id, latitude=self.latitude, longitude=self.longitude)
2015-09-07 19:31:08 +02:00
location = message.location
self.assertEqual(location.latitude, self.latitude)
self.assertEqual(location.longitude, self.longitude)
def test_location_de_json(self):
location = telegram.Location.de_json(self.json_dict, self._bot)
2015-09-07 19:31:08 +02:00
self.assertEqual(location.latitude, self.latitude)
self.assertEqual(location.longitude, self.longitude)
def test_location_to_json(self):
location = telegram.Location.de_json(self.json_dict, self._bot)
2015-09-07 19:31:08 +02:00
self.assertTrue(self.is_json(location.to_json()))
def test_location_to_dict(self):
location = telegram.Location.de_json(self.json_dict, self._bot)
2015-09-07 19:31:08 +02:00
self.assertEqual(location['latitude'], self.latitude)
self.assertEqual(location['longitude'], self.longitude)
def test_error_send_location_empty_args(self):
json_dict = self.json_dict
json_dict['latitude'] = ''
json_dict['longitude'] = ''
self.assertRaises(telegram.TelegramError,
lambda: self._bot.sendLocation(chat_id=self._chat_id,
**json_dict))
def test_error_location_without_required_args(self):
json_dict = self.json_dict
2016-04-24 04:11:25 +02:00
del (json_dict['latitude'])
del (json_dict['longitude'])
2015-09-07 19:31:08 +02:00
self.assertRaises(TypeError,
lambda: self._bot.sendLocation(chat_id=self._chat_id,
**json_dict))
@flaky(3, 1)
def test_reply_location(self):
"""Test for Message.reply_location"""
message = self._bot.sendMessage(self._chat_id, '.')
message = message.reply_location(self.latitude, self.longitude)
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)
d = telegram.Location(0, self.latitude)
self.assertEqual(a, b)
self.assertEqual(hash(a), hash(b))
self.assertIsNot(a, b)
self.assertNotEqual(a, d)
self.assertNotEqual(hash(a), hash(d))
2016-04-24 04:11:25 +02:00
2015-09-07 19:31:08 +02:00
if __name__ == '__main__':
unittest.main()