test_bing_images.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import bing_images
  5. from searx.testing import SearxTestCase
  6. class TestBingImagesEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['language'] = 'fr_FR'
  12. dicto['safesearch'] = 1
  13. dicto['time_range'] = ''
  14. params = bing_images.request(query, dicto)
  15. self.assertTrue('url' in params)
  16. self.assertTrue(query in params['url'])
  17. self.assertTrue('bing.com' in params['url'])
  18. self.assertTrue('SRCHHPGUSR' in params['cookies'])
  19. self.assertTrue('fr' in params['cookies']['SRCHHPGUSR'])
  20. dicto['language'] = 'all'
  21. params = bing_images.request(query, dicto)
  22. self.assertIn('SRCHHPGUSR', params['cookies'])
  23. self.assertIn('en', params['cookies']['SRCHHPGUSR'])
  24. def test_response(self):
  25. self.assertRaises(AttributeError, bing_images.response, None)
  26. self.assertRaises(AttributeError, bing_images.response, [])
  27. self.assertRaises(AttributeError, bing_images.response, '')
  28. self.assertRaises(AttributeError, bing_images.response, '[]')
  29. response = mock.Mock(text='<html></html>')
  30. self.assertEqual(bing_images.response(response), [])
  31. response = mock.Mock(text='<html></html>')
  32. self.assertEqual(bing_images.response(response), [])
  33. html = """
  34. <div id="mmComponent_images_1">
  35. <ul>
  36. <li>
  37. <div>
  38. <div class="imgpt">
  39. <a m='{"purl":"page_url","murl":"img_url"}' mad='{"turl":"thumb_url"}'>
  40. <img src="" alt="alt text" />
  41. </a>
  42. </div>
  43. <div></div>
  44. </div>
  45. <div>
  46. <div class="imgpt">
  47. <a m='{"purl":"page_url2","murl":"img_url2"}' mad='{"turl":"thumb_url2"}'>
  48. <img src="" alt="alt text 2" />
  49. </a>
  50. </div>
  51. </div>
  52. </li>
  53. </ul>
  54. <ul>
  55. <li>
  56. <div>
  57. <div class="imgpt">
  58. <a m='{"purl":"page_url3","murl":"img_url3"}' mad='{"turl":"thumb_url3"}'>
  59. <img src="" alt="alt text 3" />
  60. </a>
  61. </div>
  62. </div>
  63. </li>
  64. </ul>
  65. </div>
  66. """
  67. html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
  68. response = mock.Mock(text=html)
  69. results = bing_images.response(response)
  70. self.assertEqual(type(results), list)
  71. self.assertEqual(len(results), 3)
  72. self.assertEqual(results[0]['title'], 'alt text')
  73. self.assertEqual(results[0]['url'], 'page_url')
  74. self.assertEqual(results[0]['content'], '')
  75. self.assertEqual(results[0]['thumbnail_src'], 'thumb_url')
  76. self.assertEqual(results[0]['img_src'], 'img_url')