bots: Simplify test_weather.py.
This commit is contained in:
parent
84d92337be
commit
20cb236fdd
|
@ -1,13 +1,8 @@
|
||||||
#!/usr/bin/env python
|
from zulip_bots.test_lib import StubBotTestCase
|
||||||
|
|
||||||
from __future__ import absolute_import
|
from typing import Optional
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
from zulip_bots.test_lib import BotTestCase
|
class TestWeatherBot(StubBotTestCase):
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
class TestWeatherBot(BotTestCase):
|
|
||||||
bot_name = "weather"
|
bot_name = "weather"
|
||||||
|
|
||||||
help_content = '''
|
help_content = '''
|
||||||
|
@ -20,65 +15,35 @@ class TestWeatherBot(BotTestCase):
|
||||||
@**Weather Bot** Portland, Me
|
@**Weather Bot** Portland, Me
|
||||||
'''.strip()
|
'''.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
|
# Override default function in StubBotTestCase
|
||||||
def test_bot_responds_to_empty_message(self) -> None:
|
def test_bot_responds_to_empty_message(self) -> None:
|
||||||
bot_response = self.help_content
|
self._test('', self.help_content)
|
||||||
self.assert_bot_response(
|
|
||||||
message = {'content': ''},
|
|
||||||
response = {'content': bot_response},
|
|
||||||
expected_method='send_reply'
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_bot(self) -> None:
|
def test_bot(self) -> None:
|
||||||
|
|
||||||
# City query
|
# City query
|
||||||
bot_response = "Weather in New York, US:\n71.33 F / 21.85 C\nMist"
|
bot_response = "Weather in New York, US:\n71.33 F / 21.85 C\nMist"
|
||||||
with self.mock_config_info({'key': '123456'}), \
|
self._test('New York', bot_response, 'test_only_city')
|
||||||
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
|
# City with country query
|
||||||
bot_response = "Weather in New Delhi, IN:\n80.33 F / 26.85 C\nMist"
|
bot_response = "Weather in New Delhi, IN:\n80.33 F / 26.85 C\nMist"
|
||||||
with self.mock_config_info({'key': '123456'}), \
|
self._test('New Delhi, India', bot_response, 'test_city_with_country')
|
||||||
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
|
# Only country query: returns the weather of the capital city
|
||||||
bot_response = "Weather in London, GB:\n58.73 F / 14.85 C\nShower Rain"
|
bot_response = "Weather in London, GB:\n58.73 F / 14.85 C\nShower Rain"
|
||||||
with self.mock_config_info({'key': '123456'}), \
|
self._test('United Kingdom', bot_response, 'test_only_country')
|
||||||
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
|
# City not found query
|
||||||
bot_response = "Sorry, city not found"
|
bot_response = "Sorry, city not found"
|
||||||
with self.mock_config_info({'key': '123456'}), \
|
self._test('fghjklasdfgh', bot_response, 'test_city_not_found')
|
||||||
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 message
|
# help message
|
||||||
bot_response = self.help_content
|
self._test('help', self.help_content)
|
||||||
self.assert_bot_response(
|
|
||||||
message = {'content': 'help'},
|
|
||||||
response = {'content': bot_response},
|
|
||||||
expected_method='send_reply'
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in a new issue