From 35353f6d7f93f36c9cfbc053978873eaddc1db57 Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Sun, 23 Jul 2017 02:22:09 +0530 Subject: [PATCH] bots: Add complete tests for offline testing of wikipedia bot. --- zulip_bots/MANIFEST.in | 4 ++ .../fixtures/test_incorrect_query.json | 19 +++++++ .../wikipedia/fixtures/test_multi_word.json | 22 ++++++++ .../wikipedia/fixtures/test_number_query.json | 22 ++++++++ .../wikipedia/fixtures/test_single_word.json | 22 ++++++++ .../bots/wikipedia/test_wikipedia.py | 56 +++++++++++++++---- 6 files changed, 133 insertions(+), 12 deletions(-) create mode 100644 zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json create mode 100644 zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json create mode 100644 zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json create mode 100644 zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json diff --git a/zulip_bots/MANIFEST.in b/zulip_bots/MANIFEST.in index 4d76de2..9afc7d6 100644 --- a/zulip_bots/MANIFEST.in +++ b/zulip_bots/MANIFEST.in @@ -5,4 +5,8 @@ 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 diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json new file mode 100644 index 0000000..46ba8c2 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json @@ -0,0 +1,19 @@ +{ + "request": { + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=sssssss kkkkk&format=json" + }, + "response": { + "data": { + "status_code":200 + }, + "query": { + "search": [ + ] + } + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json new file mode 100644 index 0000000..948463d --- /dev/null +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json @@ -0,0 +1,22 @@ +{ + "request": { + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=The sky is blue&format=json" + }, + "response": { + "data": { + "status_code":200 + }, + "query": { + "search": [ + { + "title":"Sky blue" + } + ] + } + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json new file mode 100644 index 0000000..b8dfdf4 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json @@ -0,0 +1,22 @@ +{ + "request": { + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=123&format=json" + }, + "response": { + "data": { + "status_code":200 + }, + "query": { + "search": [ + { + "title":"123" + } + ] + } + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json new file mode 100644 index 0000000..a407ed9 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json @@ -0,0 +1,22 @@ +{ + "request": { + "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=happy&format=json" + }, + "response": { + "data": { + "status_code":200 + }, + "query": { + "search": [ + { + "title":"Happiness" + } + ] + } + }, + "response-headers":{ + "status":200, + "ok":true, + "content-type":"application/json; charset=utf-8" + } +} diff --git a/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py index 1c433b4..799bb2c 100755 --- a/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py @@ -9,15 +9,47 @@ class TestWikipediaBot(BotTestCase): bot_name = "wikipedia" def test_bot(self): - expected = { - "": 'Please enter your message after @mention-bot', - "sssssss kkkkk": ('I am sorry. The search term you provided ' - 'is not found :slightly_frowning_face:'), - "foo": ('For search term "foo", ' - 'https://en.wikipedia.org/wiki/Foobar'), - "123": ('For search term "123", ' - 'https://en.wikipedia.org/wiki/123'), - "laugh": ('For search term "laugh", ' - 'https://en.wikipedia.org/wiki/Laughter'), - } - self.check_expected_responses(expected) + + # Single-word query + bot_response = "For search term \"happy\", https://en.wikipedia.org/wiki/Happiness" + with self.mock_http_conversation('test_single_word'): + self.assert_bot_response( + message = {'content': 'happy'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # Multi-word query + bot_response = "For search term \"The sky is blue\", https://en.wikipedia.org/wiki/Sky_blue" + with self.mock_http_conversation('test_multi_word'): + self.assert_bot_response( + message = {'content': 'The sky is blue'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # Number query + bot_response = "For search term \"123\", https://en.wikipedia.org/wiki/123" + with self.mock_http_conversation('test_number_query'): + self.assert_bot_response( + message = {'content': '123'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # Incorrect word + bot_response = "I am sorry. The search term you provided is not found :slightly_frowning_face:" + with self.mock_http_conversation('test_incorrect_query'): + self.assert_bot_response( + message = {'content': 'sssssss kkkkk'}, + response = {'content': bot_response}, + expected_method='send_reply' + ) + + # Empty query, no request made to the Internet. + bot_response = "Please enter your message after @mention-bot" + self.assert_bot_response( + message = {'content': ''}, + response = {'content': bot_response}, + expected_method='send_reply' + )