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

" } """ json = json.replace('\r\n', '').replace('\n', '').replace('\r', '') 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)