bots: Simplify test_weather.py.
MODIFY
zulip_bots/zulip_bots/bots/weather/test_weather.py
zulip_bots/zulip_bots/bots/weather/test_weather.py
-#!/usr/bin/env python
+from zulip_bots.test_lib import StubBotTestCase
-from __future__ import absolute_import
-from __future__ import print_function
+from typing import Optional
-from zulip_bots.test_lib import BotTestCase
-
-from typing import Any
-
-class TestWeatherBot(BotTestCase):
+class TestWeatherBot(StubBotTestCase):
bot_name = "weather"
help_content = '''
@@ -20,65 +15,35 @@ class TestWeatherBot(BotTestCase):
@**Weather Bot** Portland, Me
'''.strip()
+ def _test(self, message: str, response: str, fixture: Optional[str]=None) -> None:
+ with self.mock_config_info({'key': '123456'}):
+ if fixture:
+ with self.mock_http_conversation(fixture):
+ self.verify_reply(message, response)
+ else:
+ self.verify_reply(message, response)
+
# Override default function in StubBotTestCase
def test_bot_responds_to_empty_message(self) -> None:
- bot_response = self.help_content
- self.assert_bot_response(
- message = {'content': ''},
- response = {'content': bot_response},
- expected_method='send_reply'
- )
+ self._test('', self.help_content)
def test_bot(self) -> None:
# 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'
- )
+ self._test('New York', bot_response, 'test_only_city')
# 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'
- )
+ self._test('New Delhi, India', bot_response, 'test_city_with_country')
# Only country query: returns the weather of the capital city
bot_response = "Weather in London, GB:\n58.73 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'
- )
+ self._test('United Kingdom', bot_response, 'test_only_country')
# 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'
- )
+ self._test('fghjklasdfgh', bot_response, 'test_city_not_found')
# help message
- bot_response = self.help_content
- self.assert_bot_response(
- message = {'content': 'help'},
- response = {'content': bot_response},
- expected_method='send_reply'
- )
+ self._test('help', self.help_content)

bots: Simplify test_weather.py.