test_duckduckgo_images.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. duckduckgo_images.supported_languages = ['de-CH', 'en-US']
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['is_test'] = True
  12. dicto['pageno'] = 1
  13. dicto['safesearch'] = 0
  14. dicto['language'] = 'en-US'
  15. params = duckduckgo_images.request(query, dicto)
  16. self.assertIn('url', params)
  17. self.assertIn(query, params['url'])
  18. self.assertIn('duckduckgo.com', params['url'])
  19. self.assertIn('s=0', params['url'])
  20. self.assertIn('p=-1', params['url'])
  21. self.assertIn('vqd=12345', params['url'])
  22. # test paging, safe search and language
  23. dicto['pageno'] = 2
  24. dicto['safesearch'] = 2
  25. dicto['language'] = 'de'
  26. params = duckduckgo_images.request(query, dicto)
  27. self.assertIn('url', params)
  28. self.assertIn(query, params['url'])
  29. self.assertIn('s=50', params['url'])
  30. self.assertIn('p=1', params['url'])
  31. self.assertIn('ch-de', params['url'])
  32. def test_response(self):
  33. self.assertRaises(AttributeError, duckduckgo_images.response, None)
  34. self.assertRaises(AttributeError, duckduckgo_images.response, [])
  35. self.assertRaises(AttributeError, duckduckgo_images.response, '')
  36. self.assertRaises(AttributeError, duckduckgo_images.response, '[]')
  37. response = mock.Mock(text='If this error persists, please let us know: ops@duckduckgo.com')
  38. self.assertEqual(duckduckgo_images.response(response), [])
  39. json = u"""
  40. {
  41. "query": "test_query",
  42. "results": [
  43. {
  44. "title": "Result 1",
  45. "url": "https://site1.url",
  46. "thumbnail": "https://thumb1.nail",
  47. "image": "https://image1"
  48. },
  49. {
  50. "title": "Result 2",
  51. "url": "https://site2.url",
  52. "thumbnail": "https://thumb2.nail",
  53. "image": "https://image2"
  54. }
  55. ]
  56. }
  57. """
  58. response = mock.Mock(text=json)
  59. results = duckduckgo_images.response(response)
  60. self.assertEqual(len(results), 2)
  61. self.assertEqual(results[0]['title'], 'Result 1')
  62. self.assertEqual(results[0]['url'], 'https://site1.url')
  63. self.assertEqual(results[0]['thumbnail_src'], 'https://thumb1.nail')
  64. self.assertEqual(results[0]['img_src'], 'https://image1')
  65. self.assertEqual(results[1]['title'], 'Result 2')
  66. self.assertEqual(results[1]['url'], 'https://site2.url')
  67. self.assertEqual(results[1]['thumbnail_src'], 'https://thumb2.nail')
  68. self.assertEqual(results[1]['img_src'], 'https://image2')