Added more googlesearch tests
This commit is contained in:
parent
841efcde80
commit
a026c48278
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"request": {
|
||||||
|
"api_url": "http://www.google.com/search",
|
||||||
|
"params": {
|
||||||
|
"q": "test"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": "<head></head><body><div id='foo'></div></body>",
|
||||||
|
"response-headers": {
|
||||||
|
"status": 200,
|
||||||
|
"content-type": "text/html; charset=utf-8"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"request": {
|
||||||
|
"api_url": "http://www.google.com/search",
|
||||||
|
"params": {
|
||||||
|
"q": "zulip"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"response": "<head></head><body><div id='search'><a href='/url?url=webcache.googleusercontent.com/foo/bar'>Cached</a><a href='/?url'></a><a href='foo bar'></a><a></a><a href='/url?url=https%3A%2F%2Fzulipchat.com%2F'>Zulip</a></div></body>",
|
||||||
|
"response-headers": {
|
||||||
|
"status": 200,
|
||||||
|
"content-type": "text/html; charset=utf-8"
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,12 +27,10 @@ def google_search(keywords: str) -> List[Dict[str, str]]:
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
# Link must start with '/url?', as these are the search result links
|
# Link must start with '/url?', as these are the search result links
|
||||||
if (not link.startswith('/url?')):
|
if not link.startswith('/url?'):
|
||||||
continue
|
continue
|
||||||
# Makes sure a hidden 'cached' result isn't displayed
|
# Makes sure a hidden 'cached' result isn't displayed
|
||||||
if (a.text.strip() == 'Cached' and 'webcache.googleusercontent.com'):
|
if a.text.strip() == 'Cached' and 'webcache.googleusercontent.com' in a['href']:
|
||||||
continue
|
|
||||||
if (a.text.strip() == ''):
|
|
||||||
continue
|
continue
|
||||||
# a.text: The name of the page
|
# a.text: The name of the page
|
||||||
result = {'url': "https://www.google.com{}".format(link),
|
result = {'url': "https://www.google.com{}".format(link),
|
||||||
|
@ -62,20 +60,8 @@ def get_google_result(search_keywords: str) -> str:
|
||||||
if (len(results) == 0):
|
if (len(results) == 0):
|
||||||
return "Found no results."
|
return "Found no results."
|
||||||
return "Found Result: [{}]({})".format(results[0]['name'], results[0]['url'])
|
return "Found Result: [{}]({})".format(results[0]['name'], results[0]['url'])
|
||||||
except ConnectionError as c_err:
|
|
||||||
return "Error: Failed to connect. {}.".format(c_err)
|
|
||||||
except AttributeError as a_err:
|
|
||||||
# google.search query failed and urls is of object
|
|
||||||
# 'NoneType'
|
|
||||||
logging.exception(str(a_err))
|
|
||||||
return "Error: Google search failed with a NoneType result. {}.".format(a_err)
|
|
||||||
except TypeError as t_err:
|
|
||||||
# google.search query failed and returned None
|
|
||||||
# This technically should not happen but the prior
|
|
||||||
# error check assumed this behavior
|
|
||||||
logging.exception(str(t_err))
|
|
||||||
return "Error: Google search function failed. {}.".format(t_err)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logging.exception(str(e))
|
||||||
return 'Error: Search failed. {}.'.format(e)
|
return 'Error: Search failed. {}.'.format(e)
|
||||||
|
|
||||||
class GoogleSearchHandler(object):
|
class GoogleSearchHandler(object):
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
from zulip_bots.test_lib import StubBotTestCase
|
from zulip_bots.test_lib import StubBotTestCase
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
class TestGoogleSearchBot(StubBotTestCase):
|
class TestGoogleSearchBot(StubBotTestCase):
|
||||||
bot_name = 'googlesearch'
|
bot_name = 'googlesearch'
|
||||||
|
|
||||||
|
@ -28,3 +30,19 @@ class TestGoogleSearchBot(StubBotTestCase):
|
||||||
def test_bot_no_results(self) -> None:
|
def test_bot_no_results(self) -> None:
|
||||||
with self.mock_http_conversation('test_no_result'):
|
with self.mock_http_conversation('test_no_result'):
|
||||||
self.verify_reply('no res', 'Found no results.')
|
self.verify_reply('no res', 'Found no results.')
|
||||||
|
|
||||||
|
def test_attribute_error(self) -> None:
|
||||||
|
with self.mock_http_conversation('test_attribute_error'), \
|
||||||
|
patch('logging.exception'):
|
||||||
|
self.verify_reply('test', 'Error: Search failed. \'NoneType\' object has no attribute \'findAll\'.')
|
||||||
|
|
||||||
|
# Makes sure cached results, irrelevant links, or empty results are not displayed
|
||||||
|
def test_ignore_links(self) -> None:
|
||||||
|
with self.mock_http_conversation('test_ignore_links'):
|
||||||
|
# The bot should ignore all links, apart from the zulip link at the end (googlesearch.py lines 23-38)
|
||||||
|
# Then it should send the zulip link
|
||||||
|
# See test_ignore_links.json
|
||||||
|
self.verify_reply(
|
||||||
|
'zulip',
|
||||||
|
'Found Result: [Zulip](https://www.google.com/url?url=https%3A%2F%2Fzulipchat.com%2F)'
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue