Skip to content

Commit

Permalink
answer.py: Add tests for error conditions
Browse files Browse the repository at this point in the history
Related to coala#611
  • Loading branch information
jayvdb committed Aug 9, 2018
1 parent 6d07480 commit 7da2912
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions plugins/answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ def construct_link(text):
text = text.split('documentation/')[-1]
return 'https://docs.coala.io/en/latest/' + text

raise ValueError('Unrecognised answer: {}'.format(text))

@botcmd
def answer(self, msg, arg):
try:
answers = requests.get(urljoin(os.environ['ANSWER_END'], 'answer'),
params={'question': arg}).json()
except json.JSONDecodeError: # pragma: no cover # for logging
except json.JSONDecodeError:
self.log.exception('something went wrong while fetching answer for'
'question: {}'.format(arg))
yield 'Something went wrong, please check logs'.format()
if answers:
reply = 'Please checkout the following links: \n- '
reply += '- '.join(map(lambda x:
Expand Down
21 changes: 21 additions & 0 deletions tests/answer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from errbot.backends.test import FullStackTest
import vcr
import requests_mock

import plugins.answer

Expand All @@ -26,3 +27,23 @@ def test_answer(self):
self.assertIn('Please checkout the following links', self.pop_message())
self.push_message('!answer shell autocompletion')
self.assertIn('Please checkout the following links', self.pop_message())

def test_invalid_json(self):
os.environ['ANSWER_END'] = 'http://0.0.0.0:8000'
with requests_mock.Mocker() as m:
m.get('http://0.0.0.0:8000/answer?question=foo', text='invalid')
self.assertCommand(
'!answer foo',
'Computer says nooo. See logs for details:\n'
'Expecting value: line 1 column 1 (char 0)')

def test_invalid_repo(self):
os.environ['ANSWER_END'] = 'http://0.0.0.0:8000'
with requests_mock.Mocker() as m:
m.get('http://0.0.0.0:8000/answer?question=foo',
text='[["Wrong answer\\n/wrong/link\\n"]]')
self.assertCommand(
'!answer foo',
'Computer says nooo. See logs for details:\n'
'Unrecognised answer: /wrong/link')

0 comments on commit 7da2912

Please sign in to comment.