From cf53da9d6eda38a08b2953133f856d8baace4331 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Thu, 7 Jun 2012 18:17:55 -0700 Subject: [PATCH] fix wiktionary no results error handling --- modules/test/test_wiktionary.py | 14 ++++++++++++++ modules/wiktionary.py | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/test/test_wiktionary.py b/modules/test/test_wiktionary.py index cf526ce..c318e2e 100644 --- a/modules/test/test_wiktionary.py +++ b/modules/test/test_wiktionary.py @@ -20,6 +20,12 @@ class TestWiktionary(unittest.TestCase): assert len(w[0]) > 0 assert len(w[1]) > 0 + def test_wiktionary_none(self): + w = wiktionary.wiktionary('Hell!') + + assert len(w[0]) == 0 + assert len(w[1]) == 0 + def test_w(self): input = Mock(group=lambda x: 'test') wiktionary.w(self.phenny, input) @@ -27,3 +33,11 @@ class TestWiktionary(unittest.TestCase): out = self.phenny.say.call_args[0][0] m = re.match('^test — noun: .*$', out, flags=re.UNICODE) self.assertTrue(m) + + def test_w_none(self): + word = 'Hell!' + input = Mock(group=lambda x: word) + wiktionary.w(self.phenny, input) + + self.phenny.say.assert_called_once_with( + "Couldn't get any definitions for {0}.".format(word)) diff --git a/modules/wiktionary.py b/modules/wiktionary.py index b1e56b9..6f08bae 100644 --- a/modules/wiktionary.py +++ b/modules/wiktionary.py @@ -38,7 +38,11 @@ def wiktionary(word): pages = json.loads(bytes) pages = pages['query']['pages'] pg = next(iter(pages)) - result = pages[pg]['revisions'][0]['*'] + + try: + result = pages[pg]['revisions'][0]['*'] + except KeyError: + return '', '' mode = None etymology = None