test_bing_videos.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import bing_videos
  5. from searx.testing import SearxTestCase
  6. class TestBingVideosEngine(SearxTestCase):
  7. def test_request(self):
  8. bing_videos.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'] = 0
  14. dicto['time_range'] = ''
  15. params = bing_videos.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('OFF' in params['cookies']['SRCHHPGUSR'])
  21. self.assertTrue('_EDGE_S' in params['cookies'])
  22. self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
  23. dicto['pageno'] = 2
  24. dicto['time_range'] = 'day'
  25. dicto['safesearch'] = 2
  26. params = bing_videos.request(query, dicto)
  27. self.assertTrue('first=11' in params['url'])
  28. self.assertTrue('1440' in params['url'])
  29. self.assertIn('SRCHHPGUSR', params['cookies'])
  30. self.assertTrue('STRICT' in params['cookies']['SRCHHPGUSR'])
  31. def test_response(self):
  32. self.assertRaises(AttributeError, bing_videos.response, None)
  33. self.assertRaises(AttributeError, bing_videos.response, [])
  34. self.assertRaises(AttributeError, bing_videos.response, '')
  35. self.assertRaises(AttributeError, bing_videos.response, '[]')
  36. response = mock.Mock(text='<html></html>')
  37. self.assertEqual(bing_videos.response(response), [])
  38. response = mock.Mock(text='<html></html>')
  39. self.assertEqual(bing_videos.response(response), [])
  40. html = """
  41. <div>
  42. <div class="dg_u">
  43. <a class="dv_i" href="/videos/search?abcde">
  44. <div class="vthblock">
  45. <div class="vthumb">
  46. <img src="thumb_1.jpg" />
  47. </div>
  48. <div>
  49. <div class="tl">
  50. Title 1
  51. </div>
  52. </div>
  53. </div>
  54. <div class="videoInfoPanel">
  55. <div class="pubInfo">
  56. <div>Content 1</div>
  57. </div>
  58. </div>
  59. </a>
  60. <div class="sa_wrapper"
  61. data-eventpayload="{&quot;purl&quot;: &quot;https://url.com/1&quot;}">
  62. </div>
  63. </div>
  64. </div>
  65. """
  66. response = mock.Mock(text=html)
  67. results = bing_videos.response(response)
  68. self.assertEqual(type(results), list)
  69. self.assertEqual(len(results), 1)
  70. self.assertEqual(results[0]['title'], 'Title 1')
  71. self.assertEqual(results[0]['url'], 'https://url.com/1')
  72. self.assertEqual(results[0]['content'], 'Content 1')
  73. self.assertEqual(results[0]['thumbnail'], 'thumb_1.jpg')
  74. html = """
  75. <div>
  76. <div class="dg_u">
  77. <a class="dv_i" href="https://url.com/1">
  78. <div class="vthblock">
  79. <div class="vthumb">
  80. <img src="thumb_1.jpg" />
  81. </div>
  82. <div>
  83. <div class="tl">
  84. Title 1
  85. </div>
  86. </div>
  87. </div>
  88. <div class="videoInfoPanel">
  89. <div class="pubInfo">
  90. <div>Content 1</div>
  91. </div>
  92. </div>
  93. </a>
  94. </div>
  95. <div class="dg_u">
  96. <a class="dv_i" href="/videos/search?abcde">
  97. <div class="vthblock">
  98. <div class="vthumb">
  99. <img src="thumb_2.jpg" />
  100. </div>
  101. <div>
  102. <div class="tl">
  103. Title 2
  104. </div>
  105. </div>
  106. </div>
  107. <div class="videoInfoPanel">
  108. <div class="pubInfo">
  109. <div>Content 2</div>
  110. </div>
  111. </div>
  112. </a>
  113. </div>
  114. </div>
  115. """
  116. response = mock.Mock(text=html)
  117. results = bing_videos.response(response)
  118. self.assertEqual(type(results), list)
  119. self.assertEqual(len(results), 1)
  120. self.assertEqual(results[0]['title'], 'Title 1')
  121. self.assertEqual(results[0]['url'], 'https://url.com/1')
  122. self.assertEqual(results[0]['content'], 'Content 1')
  123. self.assertEqual(results[0]['thumbnail'], 'thumb_1.jpg')