test_yahoo_news.py 5.3KB

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