|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# A library that provides a Python interface to the Telegram Bot API |
| 4 | +# Copyright (C) 2015 Leandro Toledo de Souza <leandrotoeldodesouza@gmail.com> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see [http://www.gnu.org/licenses/]. |
| 18 | + |
| 19 | +"""This module contains a object that represents Tests for Telegram Location""" |
| 20 | + |
| 21 | +import os |
| 22 | +import unittest |
| 23 | +import sys |
| 24 | +sys.path.append('.') |
| 25 | + |
| 26 | +import telegram |
| 27 | +from tests.base import BaseTest |
| 28 | + |
| 29 | + |
| 30 | +class LocationTest(BaseTest, unittest.TestCase): |
| 31 | + """This object represents Tests for Telegram Location.""" |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + self.latitude = -46.788275 |
| 35 | + self.longitude = -23.691287 |
| 36 | + |
| 37 | + self.json_dict = { |
| 38 | + 'latitude': self.latitude, |
| 39 | + 'longitude': self.longitude |
| 40 | + } |
| 41 | + |
| 42 | + def test_send_location_implicit_args(self): |
| 43 | + """Test telegram.Bot sendLocation method""" |
| 44 | + print('Testing bot.sendLocation - Implicit arguments') |
| 45 | + |
| 46 | + message = self._bot.sendLocation(self._chat_id, |
| 47 | + self.latitude, |
| 48 | + self.longitude) |
| 49 | + |
| 50 | + location = message.location |
| 51 | + |
| 52 | + self.assertEqual(location.latitude, self.latitude) |
| 53 | + self.assertEqual(location.longitude, self.longitude) |
| 54 | + |
| 55 | + def test_send_location_explicit_args(self): |
| 56 | + """Test telegram.Bot sendLocation method""" |
| 57 | + print('Testing bot.sendLocation - Explicit arguments') |
| 58 | + |
| 59 | + message = self._bot.sendLocation(chat_id=self._chat_id, |
| 60 | + latitude=self.latitude, |
| 61 | + longitude=self.longitude) |
| 62 | + |
| 63 | + location = message.location |
| 64 | + |
| 65 | + self.assertEqual(location.latitude, self.latitude) |
| 66 | + self.assertEqual(location.longitude, self.longitude) |
| 67 | + |
| 68 | + def test_location_de_json(self): |
| 69 | + """Test Location.de_json() method""" |
| 70 | + print('Testing Location.de_json()') |
| 71 | + |
| 72 | + location = telegram.Location.de_json(self.json_dict) |
| 73 | + |
| 74 | + self.assertEqual(location.latitude, self.latitude) |
| 75 | + self.assertEqual(location.longitude, self.longitude) |
| 76 | + |
| 77 | + def test_location_to_json(self): |
| 78 | + """Test Location.to_json() method""" |
| 79 | + print('Testing Location.to_json()') |
| 80 | + |
| 81 | + location = telegram.Location.de_json(self.json_dict) |
| 82 | + |
| 83 | + self.assertTrue(self.is_json(location.to_json())) |
| 84 | + |
| 85 | + def test_location_to_dict(self): |
| 86 | + """Test Location.to_dict() method""" |
| 87 | + print('Testing Location.to_dict()') |
| 88 | + |
| 89 | + location = telegram.Location.de_json(self.json_dict) |
| 90 | + |
| 91 | + self.assertEqual(location['latitude'], self.latitude) |
| 92 | + self.assertEqual(location['longitude'], self.longitude) |
| 93 | + |
| 94 | + def test_error_send_location_empty_args(self): |
| 95 | + print('Testing bot.sendLocation - Empty arguments') |
| 96 | + |
| 97 | + json_dict = self.json_dict |
| 98 | + |
| 99 | + json_dict['latitude'] = '' |
| 100 | + json_dict['longitude'] = '' |
| 101 | + |
| 102 | + self.assertRaises(telegram.TelegramError, |
| 103 | + lambda: self._bot.sendLocation(chat_id=self._chat_id, |
| 104 | + **json_dict)) |
| 105 | + |
| 106 | + def test_error_location_without_required_args(self): |
| 107 | + print('Testing bot.sendLocation - Without required arguments') |
| 108 | + |
| 109 | + json_dict = self.json_dict |
| 110 | + |
| 111 | + del(json_dict['latitude']) |
| 112 | + del(json_dict['longitude']) |
| 113 | + |
| 114 | + self.assertRaises(TypeError, |
| 115 | + lambda: self._bot.sendLocation(chat_id=self._chat_id, |
| 116 | + **json_dict)) |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + unittest.main() |
0 commit comments