ogn-python/tests/test_utils.py

56 wiersze
2.0 KiB
Python
Czysty Zwykły widok Historia

2017-10-03 10:49:36 +00:00
import os
2015-11-06 21:34:19 +00:00
import unittest
2015-11-20 10:36:42 +00:00
2016-06-02 18:32:04 +00:00
from ogn.model import AircraftType
2017-10-03 10:49:36 +00:00
from ogn.utils import get_ddb, get_trackable, get_country_code
import unittest.mock as mock
2015-11-06 21:34:19 +00:00
class TestStringMethods(unittest.TestCase):
2015-11-11 07:03:42 +00:00
def test_get_devices(self):
devices = get_ddb()
2015-11-06 21:34:19 +00:00
self.assertGreater(len(devices), 1000)
2015-11-11 07:03:42 +00:00
def test_get_ddb_from_file(self):
2017-10-03 10:49:36 +00:00
devices = get_ddb(os.path.dirname(__file__) + '/custom_ddb.txt')
2015-12-01 18:11:31 +00:00
self.assertEqual(len(devices), 6)
2015-11-11 07:03:42 +00:00
device = devices[0]
self.assertEqual(device.address, 'DD4711')
self.assertEqual(device.aircraft, 'HK36 TTC')
self.assertEqual(device.registration, 'D-EULE')
2015-11-20 10:36:42 +00:00
self.assertEqual(device.competition, 'CU')
2015-11-11 07:03:42 +00:00
self.assertTrue(device.tracked)
self.assertTrue(device.identified)
2016-06-02 18:32:04 +00:00
self.assertEqual(device.aircraft_type, AircraftType.glider_or_motor_glider)
2015-11-11 07:03:42 +00:00
2015-12-01 18:11:31 +00:00
def test_get_trackable(self):
2017-10-03 10:49:36 +00:00
devices = get_ddb(os.path.dirname(__file__) + '/custom_ddb.txt')
2015-12-01 18:11:31 +00:00
trackable = get_trackable(devices)
self.assertEqual(len(trackable), 4)
self.assertIn('FLRDD4711', trackable)
self.assertIn('FLRDD0815', trackable)
self.assertIn('OGNDEADBE', trackable)
self.assertIn('ICA999999', trackable)
2015-11-06 21:34:19 +00:00
def test_get_country_code(self):
latitude = 48.0
longitude = 11.0
country_code = get_country_code(latitude, longitude)
self.assertEquals(country_code, 'de')
2015-11-12 07:38:43 +00:00
def test_get_country_code_bad(self):
latitude = 0.0002274
longitude = -0.0009119
country_code = get_country_code(latitude, longitude)
self.assertEqual(country_code, None)
2016-02-03 22:11:54 +00:00
@mock.patch('ogn.utils.Nominatim')
def test_gec_country_code_exception(self, nominatim_mock):
from geopy.exc import GeocoderTimedOut
instance = nominatim_mock.return_value
instance.reverse.side_effect = GeocoderTimedOut('Too busy')
country_code = get_country_code(0, 0)
self.assertIsNone(country_code)