test_bing_images.py 3.4KB

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