test_bing_videos.py 4.9KB

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