test_yahoo_news.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. from datetime import datetime
  4. import mock
  5. from searx.engines import yahoo_news
  6. from searx.testing import SearxTestCase
  7. class TestYahooNewsEngine(SearxTestCase):
  8. def test_request(self):
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['pageno'] = 1
  12. dicto['language'] = 'fr_FR'
  13. params = yahoo_news.request(query, dicto)
  14. self.assertIn('url', params)
  15. self.assertIn(query, params['url'])
  16. self.assertIn('news.search.yahoo.com', params['url'])
  17. self.assertIn('fr', params['url'])
  18. self.assertIn('cookies', params)
  19. self.assertIn('sB', params['cookies'])
  20. self.assertIn('fr', params['cookies']['sB'])
  21. dicto['language'] = 'all'
  22. params = yahoo_news.request(query, dicto)
  23. self.assertIn('cookies', params)
  24. self.assertIn('sB', params['cookies'])
  25. self.assertIn('en', params['cookies']['sB'])
  26. self.assertIn('en', params['url'])
  27. def test_sanitize_url(self):
  28. url = "test.url"
  29. self.assertEqual(url, yahoo_news.sanitize_url(url))
  30. url = "www.yahoo.com/;_ylt=test"
  31. self.assertEqual("www.yahoo.com/", yahoo_news.sanitize_url(url))
  32. def test_response(self):
  33. self.assertRaises(AttributeError, yahoo_news.response, None)
  34. self.assertRaises(AttributeError, yahoo_news.response, [])
  35. self.assertRaises(AttributeError, yahoo_news.response, '')
  36. self.assertRaises(AttributeError, yahoo_news.response, '[]')
  37. response = mock.Mock(text='<html></html>')
  38. self.assertEqual(yahoo_news.response(response), [])
  39. html = """
  40. <ol class=" reg searchCenterMiddle">
  41. <li class="first">
  42. <div class="compTitle">
  43. <h3>
  44. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  45. This is
  46. the <b>title</b>...
  47. </a>
  48. </h3>
  49. </div>
  50. <div>
  51. <span class="cite">Business via Yahoo!</span>
  52. <span class="tri fc-2nd ml-10">May 01 10:00 AM</span>
  53. </div>
  54. <div class="compText">
  55. This is the content
  56. </div>
  57. </li>
  58. <li class="first">
  59. <div class="compTitle">
  60. <h3>
  61. <a class="yschttl spt" target="_blank">
  62. </a>
  63. </h3>
  64. </div>
  65. <div class="compText">
  66. </div>
  67. </li>
  68. </ol>
  69. """
  70. response = mock.Mock(text=html)
  71. results = yahoo_news.response(response)
  72. self.assertEqual(type(results), list)
  73. self.assertEqual(len(results), 1)
  74. self.assertEqual(results[0]['title'], 'This is the title...')
  75. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  76. self.assertEqual(results[0]['content'], 'This is the content')
  77. html = """
  78. <ol class=" reg searchCenterMiddle">
  79. <li class="first">
  80. <div class="compTitle">
  81. <h3>
  82. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  83. This is
  84. the <b>title</b>...
  85. </a>
  86. </h3>
  87. </div>
  88. <div>
  89. <span class="cite">Business via Yahoo!</span>
  90. <span class="tri fc-2nd ml-10">2 hours, 22 minutes ago</span>
  91. </div>
  92. <div class="compText">
  93. This is the content
  94. </div>
  95. </li>
  96. <li>
  97. <div class="compTitle">
  98. <h3>
  99. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  100. This is
  101. the <b>title</b>...
  102. </a>
  103. </h3>
  104. </div>
  105. <div>
  106. <span class="cite">Business via Yahoo!</span>
  107. <span class="tri fc-2nd ml-10">22 minutes ago</span>
  108. </div>
  109. <div class="compText">
  110. This is the content
  111. </div>
  112. </li>
  113. <li>
  114. <div class="compTitle">
  115. <h3>
  116. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  117. This is
  118. the <b>title</b>...
  119. </a>
  120. </h3>
  121. </div>
  122. <div>
  123. <span class="cite">Business via Yahoo!</span>
  124. <span class="tri fc-2nd ml-10">Feb 03 09:45AM 1900</span>
  125. </div>
  126. <div class="compText">
  127. This is the content
  128. </div>
  129. </li>
  130. </ol>
  131. """
  132. response = mock.Mock(text=html)
  133. results = yahoo_news.response(response)
  134. self.assertEqual(type(results), list)
  135. self.assertEqual(len(results), 3)
  136. self.assertEqual(results[0]['title'], 'This is the title...')
  137. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  138. self.assertEqual(results[0]['content'], 'This is the content')
  139. self.assertEqual(results[2]['publishedDate'].year, datetime.now().year)