from collections import defaultdict import mock from searx.engines import duckduckgo from searx.testing import SearxTestCase class TestDuckduckgoEngine(SearxTestCase): def test_request(self): query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr_FR' params = duckduckgo.request(query, dicto) self.assertIn('url', params) self.assertIn(query, params['url']) self.assertIn('duckduckgo.com', params['url']) self.assertIn('fr-fr', params['url']) dicto['language'] = 'all' params = duckduckgo.request(query, dicto) self.assertIn('en-us', params['url']) def test_response(self): self.assertRaises(AttributeError, duckduckgo.response, None) self.assertRaises(AttributeError, duckduckgo.response, []) self.assertRaises(AttributeError, duckduckgo.response, '') self.assertRaises(AttributeError, duckduckgo.response, '[]') response = mock.Mock(text='') self.assertEqual(duckduckgo.response(response), []) html = """ """ response = mock.Mock(text=html) results = duckduckgo.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 1) self.assertEqual(results[0]['title'], 'This is the title') self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/') self.assertEqual(results[0]['content'], 'This should be the content.') html = """ """ response = mock.Mock(text=html) results = duckduckgo.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0)