test_yahoo_news.py 5.2KB

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