diff --git a/searx/engines/digg.py b/searx/engines/digg.py index 8c457d6b9..1b5f2c8e4 100644 --- a/searx/engines/digg.py +++ b/searx/engines/digg.py @@ -44,7 +44,7 @@ def response(resp): search_result = loads(resp.text) - if search_result['html'] == '': + if 'html' not in search_result or search_result['html'] == '': return results dom = html.fromstring(search_result['html']) diff --git a/searx/tests/engines/test_digg.py b/searx/tests/engines/test_digg.py new file mode 100644 index 000000000..7e9006c0d --- /dev/null +++ b/searx/tests/engines/test_digg.py @@ -0,0 +1,57 @@ +from collections import defaultdict +import mock +from searx.engines import digg +from searx.testing import SearxTestCase + + +class TestDiggEngine(SearxTestCase): + + def test_request(self): + query = 'test_query' + dicto = defaultdict(dict) + dicto['pageno'] = 1 + params = digg.request(query, dicto) + self.assertIn('url', params) + self.assertIn(query, params['url']) + self.assertIn('digg.com', params['url']) + + def test_response(self): + self.assertRaises(AttributeError, digg.response, None) + self.assertRaises(AttributeError, digg.response, []) + self.assertRaises(AttributeError, digg.response, '') + self.assertRaises(AttributeError, digg.response, '[]') + + response = mock.Mock(text='{}') + self.assertEqual(digg.response(response), []) + + response = mock.Mock(text='{"data": []}') + self.assertEqual(digg.response(response), []) + + json = """ + { + "status": "ok", + "num": 10, + "next_position": 20, + "html": "
\\"\\"
Kicker

Title of article

" + } + """ + response = mock.Mock(text=json) + results = digg.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 1) + self.assertEqual(results[0]['title'], 'Title of article') + self.assertEqual(results[0]['url'], 'http://url.of.link') + self.assertEqual(results[0]['thumbnail'], 'http://url.of.image.jpeg') + self.assertEqual(results[0]['content'], '') + + json = """ + { + "status": "error", + "num": 10, + "next_position": 20 + } + """ + response = mock.Mock(text=json) + results = digg.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 0) diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py index 64d220bcd..309e83f16 100644 --- a/searx/tests/test_engines.py +++ b/searx/tests/test_engines.py @@ -1,6 +1,7 @@ from searx.tests.engines.test_bing import * # noqa from searx.tests.engines.test_dailymotion import * # noqa from searx.tests.engines.test_deezer import * # noqa +from searx.tests.engines.test_digg import * # noqa from searx.tests.engines.test_dummy import * # noqa from searx.tests.engines.test_flickr import * # noqa from searx.tests.engines.test_github import * # noqa