test_yahoo_news.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_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. </div>
  54. """
  55. response = mock.Mock(text=html)
  56. results = yahoo_news.response(response)
  57. self.assertEqual(type(results), list)
  58. self.assertEqual(len(results), 1)
  59. self.assertEqual(results[0]['title'], 'This is the title...')
  60. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  61. self.assertEqual(results[0]['content'], 'This is the content')
  62. html = """
  63. <ol class=" reg searchCenterMiddle">
  64. <li class="first">
  65. <div class="compTitle">
  66. <h3>
  67. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  68. This is
  69. the <b>title</b>...
  70. </a>
  71. </h3>
  72. </div>
  73. <div>
  74. <span class="cite">Business via Yahoo!</span>
  75. <span class="tri fc-2nd ml-10">2 hours, 22 minutes ago</span>
  76. </div>
  77. <div class="compText">
  78. This is the content
  79. </div>
  80. </li>
  81. <li>
  82. <div class="compTitle">
  83. <h3>
  84. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  85. This is
  86. the <b>title</b>...
  87. </a>
  88. </h3>
  89. </div>
  90. <div>
  91. <span class="cite">Business via Yahoo!</span>
  92. <span class="tri fc-2nd ml-10">22 minutes ago</span>
  93. </div>
  94. <div class="compText">
  95. This is the content
  96. </div>
  97. </li>
  98. <li>
  99. <div class="compTitle">
  100. <h3>
  101. <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
  102. This is
  103. the <b>title</b>...
  104. </a>
  105. </h3>
  106. </div>
  107. <div>
  108. <span class="cite">Business via Yahoo!</span>
  109. <span class="tri fc-2nd ml-10">Feb 03 09:45AM 1900</span>
  110. </div>
  111. <div class="compText">
  112. This is the content
  113. </div>
  114. </li>
  115. </ol>
  116. """
  117. response = mock.Mock(text=html)
  118. results = yahoo_news.response(response)
  119. self.assertEqual(type(results), list)
  120. self.assertEqual(len(results), 3)
  121. self.assertEqual(results[0]['title'], 'This is the title...')
  122. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  123. self.assertEqual(results[0]['content'], 'This is the content')
  124. self.assertEqual(results[2]['publishedDate'].year, datetime.now().year)