test_duckduckgo_images.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import duckduckgo_images
  5. from searx.testing import SearxTestCase
  6. class TestDuckduckgoImagesEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['is_test'] = True
  11. dicto['pageno'] = 1
  12. dicto['safesearch'] = 0
  13. dicto['language'] = 'all'
  14. params = duckduckgo_images.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn(query, params['url'])
  17. self.assertIn('duckduckgo.com', params['url'])
  18. self.assertIn('s=0', params['url'])
  19. self.assertIn('p=-1', params['url'])
  20. self.assertIn('vqd=12345', params['url'])
  21. # test paging and safe search
  22. dicto['pageno'] = 2
  23. dicto['safesearch'] = 2
  24. params = duckduckgo_images.request(query, dicto)
  25. self.assertIn('url', params)
  26. self.assertIn(query, params['url'])
  27. self.assertIn('s=50', params['url'])
  28. self.assertIn('p=1', params['url'])
  29. def test_response(self):
  30. self.assertRaises(AttributeError, duckduckgo_images.response, None)
  31. self.assertRaises(AttributeError, duckduckgo_images.response, [])
  32. self.assertRaises(AttributeError, duckduckgo_images.response, '')
  33. self.assertRaises(AttributeError, duckduckgo_images.response, '[]')
  34. response = mock.Mock(text='If this error persists, please let us know: ops@duckduckgo.com')
  35. self.assertEqual(duckduckgo_images.response(response), [])
  36. json = u"""
  37. {
  38. "query": "test_query",
  39. "results": [
  40. {
  41. "title": "Result 1",
  42. "url": "https://site1.url",
  43. "thumbnail": "https://thumb1.nail",
  44. "image": "https://image1"
  45. },
  46. {
  47. "title": "Result 2",
  48. "url": "https://site2.url",
  49. "thumbnail": "https://thumb2.nail",
  50. "image": "https://image2"
  51. }
  52. ]
  53. }
  54. """
  55. response = mock.Mock(text=json)
  56. results = duckduckgo_images.response(response)
  57. self.assertEqual(len(results), 2)
  58. self.assertEqual(results[0]['title'], 'Result 1')
  59. self.assertEqual(results[0]['url'], 'https://site1.url')
  60. self.assertEqual(results[0]['thumbnail_src'], 'https://thumb1.nail')
  61. self.assertEqual(results[0]['img_src'], 'https://image1')
  62. self.assertEqual(results[1]['title'], 'Result 2')
  63. self.assertEqual(results[1]['url'], 'https://site2.url')
  64. self.assertEqual(results[1]['thumbnail_src'], 'https://thumb2.nail')
  65. self.assertEqual(results[1]['img_src'], 'https://image2')