[weather.py] Drop display_name and test coordinates

master
Robin Richtsfeld 2018-04-02 16:11:03 +02:00 committed by mutantmonkey
parent 8e984128a6
commit 07b257cf8c
2 changed files with 35 additions and 38 deletions

View File

@ -6,7 +6,7 @@ author: mutantmonkey <mutantmonkey@mutantmonkey.in>
import re import re
import unittest import unittest
from mock import MagicMock, Mock, patch from mock import MagicMock, Mock, patch
from modules.weather import location, local, code, f_weather from modules import weather
class TestWeather(unittest.TestCase): class TestWeather(unittest.TestCase):
@ -14,58 +14,55 @@ class TestWeather(unittest.TestCase):
self.phenny = MagicMock() self.phenny = MagicMock()
def test_locations(self): def test_locations(self):
def check_places(*args): def check_location(result, expected):
def validate(actual_name, actual_lat, actual_lon): self.assertAlmostEqual(result[0], expected[0], places=1)
names = [n.strip() for n in actual_name.split(',')] self.assertAlmostEqual(result[1], expected[1], places=1)
for arg in args:
self.assertIn(arg, names)
return validate
locations = [ locations = [
('92121', check_places("San Diego", "California")), ('92121', (32.9, -117.2)),
('94110', check_places("SF", "California")), ('94110', (37.8, -122.4)),
('94041', check_places("Mountain View", "California")), ('94041', (37.4, -122.1)),
('27959', check_places("Dare County", "North Carolina")), ('27959', (36.0, -75.6)),
('48067', check_places("Royal Oak", "Michigan")), ('48067', (42.5, -83.1)),
('23606', check_places("Newport News", "Virginia")), ('23606', (37.1, -76.5)),
('23113', check_places("Chesterfield County", "Virginia")), ('23113', (37.5, -77.6)),
('27517', check_places("Chapel Hill", "North Carolina")), ('27517', (35.9, -79.0)),
('15213', check_places("North Oakland", "Pennsylvania")), ('15213', (40.4, -80.0)),
('90210', check_places("LA", "California")), ('90210', (34.1, -118.4)),
('33109', check_places("Fisher Island", "Florida")), ('33109', (25.8, -80.1)),
('80201', check_places("Denver", "Colorado")), ('80201', (22.6, 120.3)),
("Berlin", check_places("Berlin", "Deutschland")), ("Berlin", (52.5, 13.4)),
("Paris", check_places("Paris", "France")), ("Paris", (48.9, 2.4)),
("Vilnius", check_places("Vilnius", "Lietuva")), ("Vilnius", (54.7, 25.3)),
('Blacksburg, VA', check_places("Blacksburg", "Virginia")), ('Blacksburg, VA', (37.2, -80.4)),
('Granger, IN', check_places("Granger", "Indiana")), ('Granger, IN', (41.8, -86.1)),
] ]
for loc, validator in locations: for query, expected in locations:
names, lat, lon = location(loc) result = weather.location(query)
validator(names, lat, lon) check_location(result, expected)
def test_code_94110(self): def test_code_94110(self):
icao = code(self.phenny, '94110') icao = weather.code(self.phenny, '94110')
self.assertEqual(icao, 'KSFO') self.assertEqual(icao, 'KSFO')
def test_airport(self): def test_airport(self):
input = Mock(group=lambda x: 'KIAD') input = Mock(group=lambda x: 'KIAD')
f_weather(self.phenny, input) weather.f_weather(self.phenny, input)
assert self.phenny.say.called is True assert self.phenny.say.called is True
def test_place(self): def test_place(self):
input = Mock(group=lambda x: 'Blacksburg') input = Mock(group=lambda x: 'Blacksburg')
f_weather(self.phenny, input) weather.f_weather(self.phenny, input)
assert self.phenny.say.called is True assert self.phenny.say.called is True
def test_notfound(self): def test_notfound(self):
input = Mock(group=lambda x: 'Hell') input = Mock(group=lambda x: 'Hell')
f_weather(self.phenny, input) weather.f_weather(self.phenny, input)
self.phenny.say.called_once_with('#phenny', self.phenny.say.called_once_with('#phenny',
"No NOAA data available for that location.") "No NOAA data available for that location.")

View File

@ -25,13 +25,13 @@ def location(q):
results = web.get(uri) results = web.get(uri)
data = json.loads(results) data = json.loads(results)
if len(data) < 1: if not data:
return '?', None, None return None, None
display_name = data[0]['display_name'] latitude = float(data[0]['lat'])
lat = float(data[0]['lat']) longitude = float(data[0]['lon'])
lon = float(data[0]['lon'])
return display_name, lat, lon return latitude, longitude
def local(icao, hour, minute): def local(icao, hour, minute):
@ -58,7 +58,7 @@ def code(phenny, search):
if search.upper() in [loc[0] for loc in data]: if search.upper() in [loc[0] for loc in data]:
return search.upper() return search.upper()
else: else:
display_name, latitude, longitude = location(search) latitude, longitude = location(search)
if not latitude or not longitude: if not latitude or not longitude:
return False return False
sumOfSquares = (99999999999999999999999999999, 'ICAO') sumOfSquares = (99999999999999999999999999999, 'ICAO')