diff --git a/zulip_bots/MANIFEST.in b/zulip_bots/MANIFEST.in index 5f32508..3ec94ef 100644 --- a/zulip_bots/MANIFEST.in +++ b/zulip_bots/MANIFEST.in @@ -1,17 +1,26 @@ include zulip_bots/bots/giphy/fixtures/test_1.json + include zulip_bots/bots/github_detail/fixtures/test_404.json include zulip_bots/bots/github_detail/fixtures/test_issue.json include zulip_bots/bots/github_detail/fixtures/test_pull.json + include zulip_bots/bots/define/fixtures/test_single_type_word.json include zulip_bots/bots/define/fixtures/test_multi_type_word.json include zulip_bots/bots/define/fixtures/test_incorrect_word.json + include zulip_bots/bots/wikipedia/fixtures/test_single_word.json include zulip_bots/bots/wikipedia/fixtures/test_multi_word.json include zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json include zulip_bots/bots/wikipedia/fixtures/test_number_query.json + include zulip_bots/bots/xkcd/fixtures/test_latest.json include zulip_bots/bots/xkcd/fixtures/test_random.json include zulip_bots/bots/xkcd/fixtures/test_specific_id.json include zulip_bots/bots/xkcd/fixtures/test_not_existing_id.json include zulip_bots/bots/xkcd/fixtures/test_not_existing_id_2.json +include zulip_bots/bots/weather/fixtures/test_only_city.json +include zulip_bots/bots/weather/fixtures/test_only_country.json +include zulip_bots/bots/weather/fixtures/test_city_with_country.json +include zulip_bots/bots/weather/fixtures/test_city_not_found.json + diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json new file mode 100644 index 0000000..9965b20 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_not_found.json @@ -0,0 +1,14 @@ +{ + "request":{ + "api_url":"http://api.openweathermap.org/data/2.5/weather?q=fghjklasdfgh&APPID=123456" + }, + "response":{ + "cod":"404", + "message":"city not found" + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json new file mode 100644 index 0000000..1bacfea --- /dev/null +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_city_with_country.json @@ -0,0 +1,52 @@ +{ + "request":{ + "api_url":"http://api.openweathermap.org/data/2.5/weather?q=New Delhi, India&APPID=123456" + }, + "response":{ + "coord":{ + "lon":77.23, + "lat":28.61 + }, + "weather":[ + { + "id":701, + "main":"Mist", + "description":"mist", + "icon":"50n" + } + ], + "base":"stations", + "main":{ + "temp":300.15, + "pressure":998, + "humidity":100, + "temp_min":300.15, + "temp_max":300.15 + }, + "visibility":2500, + "wind":{ + "speed":3.1, + "deg":90 + }, + "clouds":{ + "all":90 + }, + "dt":1500822000, + "sys":{ + "type":1, + "id":7809, + "message":0.0159, + "country":"IN", + "sunrise":1500768469, + "sunset":1500817624 + }, + "id":1261481, + "name":"New Delhi", + "cod":200 + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json new file mode 100644 index 0000000..9e5c1dc --- /dev/null +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_city.json @@ -0,0 +1,64 @@ +{ + "request":{ + "api_url":"http://api.openweathermap.org/data/2.5/weather?q=New York&APPID=123456" + }, + "response":{ + "coord":{ + "lon":-74.01, + "lat":40.71 + }, + "weather":[ + { + "id":701, + "main":"Mist", + "description":"mist", + "icon":"50d" + }, + { + "id":721, + "main":"Haze", + "description":"haze", + "icon":"50d" + }, + { + "id":500, + "main":"Rain", + "description":"light rain", + "icon":"10d" + } + ], + "base":"stations", + "main":{ + "temp":295.6, + "pressure":1008, + "humidity":94, + "temp_min":294.15, + "temp_max":297.15 + }, + "visibility":12874, + "wind":{ + "speed":2.56, + "deg":56.5027 + }, + "clouds":{ + "all":90 + }, + "dt":1500810900, + "sys":{ + "type":1, + "id":1975, + "message":0.0161, + "country":"US", + "sunrise":1500803105, + "sunset":1500855570 + }, + "id":5128581, + "name":"New York", + "cod":200 + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json new file mode 100644 index 0000000..5d27801 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/weather/fixtures/test_only_country.json @@ -0,0 +1,56 @@ +{ + "request":{ + "api_url":"http://api.openweathermap.org/data/2.5/weather?q=United Kingdom&APPID=123456" + }, + "response":{ + "coord":{ + "lon":77.23, + "lat":28.61 + }, + "coord":{ + "lon":-0.13, + "lat":51.51 + }, + "weather":[ + { + "id":521, + "main":"Rain", + "description":"shower rain", + "icon":"09n" + } + ], + "base":"stations", + "main":{ + "temp":288.85, + "pressure":1011, + "humidity":82, + "temp_min":287.15, + "temp_max":290.15 + }, + "visibility":10000, + "wind":{ + "speed":4.1, + "deg":240 + }, + "clouds":{ + "all":20 + }, + "dt":1500841200, + "sys":{ + "type":1, + "id":5091, + "message":0.0097, + "country":"GB", + "sunrise":1500783154, + "sunset":1500840036 + }, + "id":2643743, + "name":"London", + "cod":200 + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/weather/test_weather.py b/zulip_bots/zulip_bots/bots/weather/test_weather.py new file mode 100644 index 0000000..bfbb259 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/weather/test_weather.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +from __future__ import absolute_import +from __future__ import print_function + +from zulip_bots.test_lib import BotTestCase + +class TestWeatherBot(BotTestCase): + bot_name = "weather" + + def test_bot(self): + + # City query + bot_response = "Weather in New York, US:\n71.33 F / 21.85 C\nMist" + with self.mock_config_info({'key': '123456'}), \ + self.mock_http_conversation('test_only_city'): + self.initialize_bot() + self.assert_bot_response( + message = {'content': 'New York'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # City with country query + bot_response = "Weather in New Delhi, IN:\n80.33 F / 26.85 C\nMist" + with self.mock_config_info({'key': '123456'}), \ + self.mock_http_conversation('test_city_with_country'): + self.initialize_bot() + self.assert_bot_response( + message = {'content': 'New Delhi, India'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # Only country query: returns the weather of the capital city + bot_response = "Weather in London, GB:\n58.33 F / 14.85 C\nShower Rain" + with self.mock_config_info({'key': '123456'}), \ + self.mock_http_conversation('test_only_country'): + self.initialize_bot() + self.assert_bot_response( + message = {'content': 'United Kingdom'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # City not found query + bot_response = "Sorry, city not found" + with self.mock_config_info({'key': '123456'}), \ + self.mock_http_conversation('test_city_not_found'): + self.initialize_bot() + self.assert_bot_response( + message = {'content': 'fghjklasdfgh'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + help_content = ''' + This bot returns weather info for specified city. + You specify city in the following format: + city, state/country + state and country parameter is optional(useful when there are many cities with the same name) + For example: + @**Weather Bot** Portland + @**Weather Bot** Portland, Me + '''.strip() + + # help message + bot_response = help_content + self.assert_bot_response( + message = {'content': 'help'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # empty message + bot_response = help_content + self.assert_bot_response( + message = {'content': ''}, + response = {'content': bot_response}, + expected_method='send_reply' + ) diff --git a/zulip_bots/zulip_bots/bots/weather/weather.py b/zulip_bots/zulip_bots/bots/weather/weather.py index bc5891a..d641eb9 100644 --- a/zulip_bots/zulip_bots/bots/weather/weather.py +++ b/zulip_bots/zulip_bots/bots/weather/weather.py @@ -29,16 +29,16 @@ class WeatherHandler(object): else: url = 'http://api.openweathermap.org/data/2.5/weather?q=' + message['content'] + '&APPID=' r = requests.get(url + self.api_key) - if "city not found" in r.text: + if r.json()['cod'] == "404": response = "Sorry, city not found" else: - response = format_response(r.text, message['content'], self.response_pattern) + response = format_response(r, message['content'], self.response_pattern) bot_handler.send_reply(message, response) def format_response(text, city, response_pattern): - j = json.loads(text) + j = text.json() city = j['name'] country = j['sys']['country'] fahrenheit = to_fahrenheit(j['main']['temp'])