Skip to content
This repository has been archived by the owner on Oct 23, 2019. It is now read-only.

Commit

Permalink
brew.py: Handle empty search results from API
Browse files Browse the repository at this point in the history
Fixes #559
  • Loading branch information
linuxdaemon committed Jul 3, 2019
1 parent 0e72b3a commit 470e6db
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ exclude_lines =
[run]
omit =
tests/data/*
tests/util/*
3 changes: 3 additions & 0 deletions plugins/brew.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def brew(text, reply):

try:
if 'totalResults' in response:
if response['totalResults'] == 0:
return output

beer = response['data'][0]
brewery = beer['breweries'][0]

Expand Down
24 changes: 24 additions & 0 deletions tests/plugin_tests/test_brew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from mock import MagicMock

from tests.util.mock_bot import MockBot


def test_no_results(mock_requests, unset_bot):
from cloudbot.bot import bot

bot.set(MockBot({"api_keys": {"brewerydb": "APIKEY"}}))
mock_requests.add(
'GET',
'http://api.brewerydb.com/v2/search'
'?format=json&key=APIKEY&type=beer&withBreweries=Y&q=some+text',
match_querystring=True,
json={"totalResults": 0},
)
from plugins import brew

reply = MagicMock()
result = brew.brew('some text', reply)

reply.assert_not_called()

assert result == 'No results found.'
Empty file added tests/util/__init__.py
Empty file.
17 changes: 17 additions & 0 deletions tests/util/mock_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import asyncio

from cloudbot.config import Config


class MockConfig(Config):
def load_config(self):
self._api_keys.clear()


class MockBot:
def __init__(self, config, loop=None):
if loop is None:
loop = asyncio.get_event_loop()

self.loop = loop
self.config = MockConfig(self, config)

0 comments on commit 470e6db

Please sign in to comment.