test_yahoo.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import yahoo
  5. from searx.testing import SearxTestCase
  6. class TestYahooEngine(SearxTestCase):
  7. def test_parse_url(self):
  8. test_url = 'http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;_ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb' +\
  9. '2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10/RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=' +\
  10. 'dtcJsfP4mEeBOjnVfUQ-'
  11. url = yahoo.parse_url(test_url)
  12. self.assertEqual('https://this.is.the.url/', url)
  13. test_url = 'http://r.search.yahoo.com/_ylt=A0LElb9JUSKcAEGRXNyoA;_ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb' +\
  14. '2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10/RU=https%3a%2f%2fthis.is.the.url%2f/RS=' +\
  15. 'dtcJsfP4mEeBOjnVfUQ-'
  16. url = yahoo.parse_url(test_url)
  17. self.assertEqual('https://this.is.the.url/', url)
  18. test_url = 'https://this.is.the.url/'
  19. url = yahoo.parse_url(test_url)
  20. self.assertEqual('https://this.is.the.url/', url)
  21. def test_request(self):
  22. query = 'test_query'
  23. dicto = defaultdict(dict)
  24. dicto['pageno'] = 1
  25. dicto['time_range'] = ''
  26. dicto['language'] = 'fr_FR'
  27. params = yahoo.request(query, dicto)
  28. self.assertIn('url', params)
  29. self.assertIn(query, params['url'])
  30. self.assertIn('search.yahoo.com', params['url'])
  31. self.assertIn('fr', params['url'])
  32. self.assertIn('cookies', params)
  33. self.assertIn('sB', params['cookies'])
  34. self.assertIn('fr', params['cookies']['sB'])
  35. dicto['language'] = 'all'
  36. params = yahoo.request(query, dicto)
  37. self.assertIn('cookies', params)
  38. self.assertIn('sB', params['cookies'])
  39. self.assertIn('en', params['cookies']['sB'])
  40. self.assertIn('en', params['url'])
  41. def test_no_url_in_request_year_time_range(self):
  42. dicto = defaultdict(dict)
  43. query = 'test_query'
  44. dicto['time_range'] = 'year'
  45. params = yahoo.request(query, dicto)
  46. self.assertEqual({}, params['url'])
  47. def test_response(self):
  48. self.assertRaises(AttributeError, yahoo.response, None)
  49. self.assertRaises(AttributeError, yahoo.response, [])
  50. self.assertRaises(AttributeError, yahoo.response, '')
  51. self.assertRaises(AttributeError, yahoo.response, '[]')
  52. response = mock.Mock(text='<html></html>')
  53. self.assertEqual(yahoo.response(response), [])
  54. html = """
  55. <ol class="reg mb-15 searchCenterMiddle">
  56. <li class="first">
  57. <div class="dd algo fst Sr">
  58. <div class="compTitle">
  59. <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
  60. _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
  61. /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
  62. target="_blank" data-bid="54e712e13671c">
  63. <b><b>This is the title</b></b></a>
  64. </h3>
  65. </div>
  66. <div class="compText aAbs">
  67. <p class="lh-18"><b><b>This is the </b>content</b>
  68. </p>
  69. </div>
  70. </div>
  71. </li>
  72. <li>
  73. <div class="dd algo lst Sr">
  74. <div class="compTitle">
  75. </div>
  76. <div class="compText aAbs">
  77. <p class="lh-18">This is the second content</p>
  78. </div>
  79. </div>
  80. </li>
  81. </ol>
  82. <div class="dd assist fst lst AlsoTry" data-bid="54e712e138d04">
  83. <div class="compTitle mb-4 h-17">
  84. <h3 class="title">Also Try</h3> </div>
  85. <table class="compTable m-0 ac-1st td-u fz-ms">
  86. <tbody>
  87. <tr>
  88. <td class="w-50p pr-28"><a href="https://search.yahoo.com/"><B>This is the </B>suggestion<B></B></a>
  89. </td>
  90. </tr>
  91. </table>
  92. </div>
  93. """
  94. response = mock.Mock(text=html)
  95. results = yahoo.response(response)
  96. self.assertEqual(type(results), list)
  97. self.assertEqual(len(results), 2)
  98. self.assertEqual(results[0]['title'], 'This is the title')
  99. self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
  100. self.assertEqual(results[0]['content'], 'This is the content')
  101. self.assertEqual(results[1]['suggestion'], 'This is the suggestion')
  102. html = """
  103. <ol class="reg mb-15 searchCenterMiddle">
  104. <li class="first">
  105. <div class="dd algo fst Sr">
  106. <div class="compTitle">
  107. <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
  108. _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
  109. /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
  110. target="_blank" data-bid="54e712e13671c">
  111. <b><b>This is the title</b></b></a>
  112. </h3>
  113. </div>
  114. <div class="compText aAbs">
  115. <p class="lh-18"><b><b>This is the </b>content</b>
  116. </p>
  117. </div>
  118. </div>
  119. </li>
  120. </ol>
  121. """
  122. response = mock.Mock(text=html)
  123. results = yahoo.response(response)
  124. self.assertEqual(type(results), list)
  125. self.assertEqual(len(results), 1)
  126. self.assertEqual(results[0]['title'], 'This is the title')
  127. self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
  128. self.assertEqual(results[0]['content'], 'This is the content')
  129. html = """
  130. <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
  131. </li>
  132. """
  133. response = mock.Mock(text=html)
  134. results = yahoo.response(response)
  135. self.assertEqual(type(results), list)
  136. self.assertEqual(len(results), 0)
  137. def test_fetch_supported_languages(self):
  138. html = """<html></html>"""
  139. response = mock.Mock(text=html)
  140. results = yahoo._fetch_supported_languages(response)
  141. self.assertEqual(type(results), list)
  142. self.assertEqual(len(results), 0)
  143. html = """
  144. <html>
  145. <div>
  146. <div id="yschlang">
  147. <span>
  148. <label><input value="lang_ar"></input></label>
  149. </span>
  150. <span>
  151. <label><input value="lang_zh_chs"></input></label>
  152. <label><input value="lang_zh_cht"></input></label>
  153. </span>
  154. </div>
  155. </div>
  156. </html>
  157. """
  158. response = mock.Mock(text=html)
  159. languages = yahoo._fetch_supported_languages(response)
  160. self.assertEqual(type(languages), list)
  161. self.assertEqual(len(languages), 3)
  162. self.assertIn('ar', languages)
  163. self.assertIn('zh-chs', languages)
  164. self.assertIn('zh-cht', languages)