From e32336eb6eb4911109ef9fc83758f35253c8f315 Mon Sep 17 00:00:00 2001 From: Viraat Chandra Date: Sat, 6 Jan 2018 21:20:50 +0530 Subject: [PATCH] Improve test coverage for Yoda bot. --- .../bots/yoda/fixtures/test_api_key_error.json | 14 ++++++++++++++ .../test_service_unavailable_error.json | 14 ++++++++++++++ .../bots/yoda/fixtures/test_unknown_error.json | 16 ++++++++++++++++ zulip_bots/zulip_bots/bots/yoda/test_yoda.py | 17 +++++++++++++++++ zulip_bots/zulip_bots/bots/yoda/yoda.py | 2 +- 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 zulip_bots/zulip_bots/bots/yoda/fixtures/test_api_key_error.json create mode 100644 zulip_bots/zulip_bots/bots/yoda/fixtures/test_service_unavailable_error.json create mode 100644 zulip_bots/zulip_bots/bots/yoda/fixtures/test_unknown_error.json diff --git a/zulip_bots/zulip_bots/bots/yoda/fixtures/test_api_key_error.json b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_api_key_error.json new file mode 100644 index 0000000..352d7f7 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_api_key_error.json @@ -0,0 +1,14 @@ +{ + "request": { + "api_url": "https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.", + "headers": { + "X-Mashape-Key": "12345678", + "Accept": "text/plain" + } + }, + "response": {}, + "response-headers": { + "status": 403, + "content-type": "text/html; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/yoda/fixtures/test_service_unavailable_error.json b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_service_unavailable_error.json new file mode 100644 index 0000000..245f369 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_service_unavailable_error.json @@ -0,0 +1,14 @@ +{ + "request": { + "api_url": "https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.", + "headers": { + "X-Mashape-Key": "12345678", + "Accept": "text/plain" + } + }, + "response": {}, + "response-headers": { + "status": 503, + "content-type": "text/html; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/yoda/fixtures/test_unknown_error.json b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_unknown_error.json new file mode 100644 index 0000000..43c8b4c --- /dev/null +++ b/zulip_bots/zulip_bots/bots/yoda/fixtures/test_unknown_error.json @@ -0,0 +1,16 @@ +{ + "request": { + "api_url": "https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.", + "headers": { + "X-Mashape-Key": "12345678", + "Accept": "text/plain" + } + }, + "response": { + "message": "Unknown Error." + }, + "response-headers": { + "status": 123, + "content-type": "text/html; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py index c516db7..fee9931 100644 --- a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py @@ -1,3 +1,4 @@ +from zulip_bots.bots.yoda.yoda import ServiceUnavailableError from zulip_bots.test_lib import BotTestCase class TestYodaBot(BotTestCase): @@ -51,3 +52,19 @@ class TestYodaBot(BotTestCase): self._test('@#$%^&*', "Invalid input, please check the sentence you have entered.", 'test_invalid_input') + + # Test 403 response. + self._test('You will learn how to speak like me someday.', + "Invalid Api Key. Did you follow the instructions in the `readme.md` file?", + 'test_api_key_error') + + # Test 503 response. + with self.assertRaises(ServiceUnavailableError): + self._test('You will learn how to speak like me someday.', + "The service is temporarily unavailable, please try again.", + 'test_service_unavailable_error') + + # Test unknown response. + self._test('You will learn how to speak like me someday.', + "Unknown Error.Error code: 123 Did you follow the instructions in the `readme.md` file?", + 'test_unknown_error') diff --git a/zulip_bots/zulip_bots/bots/yoda/yoda.py b/zulip_bots/zulip_bots/bots/yoda/yoda.py index d5fd9aa..5e77f53 100644 --- a/zulip_bots/zulip_bots/bots/yoda/yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/yoda.py @@ -72,7 +72,7 @@ class YodaSpeakHandler(object): error_message = response.json()['message'] logging.error(error_message) error_code = response.status_code - error_message = error_message + 'Error code: ' + error_code +\ + error_message = error_message + 'Error code: ' + str(error_code) +\ ' Did you follow the instructions in the `readme.md` file?' return error_message