test_google_news.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import google_news
  5. from searx.testing import SearxTestCase
  6. class TestGoogleNewsEngine(SearxTestCase):
  7. def test_request(self):
  8. google_news.supported_languages = ['en-US', 'fr-FR']
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['pageno'] = 1
  12. dicto['language'] = 'fr-FR'
  13. dicto['time_range'] = 'w'
  14. params = google_news.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn(query, params['url'])
  17. self.assertIn('fr', params['url'])
  18. def test_response(self):
  19. self.assertRaises(AttributeError, google_news.response, None)
  20. self.assertRaises(AttributeError, google_news.response, [])
  21. self.assertRaises(AttributeError, google_news.response, '')
  22. self.assertRaises(AttributeError, google_news.response, '[]')
  23. response = mock.Mock(text='{}')
  24. self.assertEqual(google_news.response(response), [])
  25. response = mock.Mock(text='{"data": []}')
  26. self.assertEqual(google_news.response(response), [])
  27. html = u"""
  28. <h2 class="hd">Search Results</h2>
  29. <div data-async-context="query:searx" id="ires">
  30. <div eid="oC2oWcGXCafR6ASkwoCwDA" id="rso">
  31. <div class="_NId">
  32. <!--m-->
  33. <div class="g _cy">
  34. <div class="ts _JGs _JHs _tJs _KGs _jHs">
  35. <div class="_hJs">
  36. <h3 class="r _gJs">
  37. <a class="l _PMs" href="https://example.com/" onmousedown="return rwt(this,'','','','11','AFQjCNEyehpzD5cJK1KUfXBx9RmsbqqG9g','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAggiKAAwAA','','',event)">Example title</a>
  38. </h3>
  39. <div class="slp">
  40. <span class="_OHs _PHs">
  41. Mac &amp; i</span>
  42. <span class="_QGs">
  43. -</span>
  44. <span class="f nsa _QHs">
  45. Mar 21, 2016</span>
  46. </div>
  47. <div class="st">Example description</div>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="g _cy">
  52. <div class="ts _JGs _JHs _oGs _KGs _jHs">
  53. <a class="top _xGs _SHs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChC8iAEIJDAB','','',event)">
  54. <img class="th _RGs" src="https://example2.com/image.jpg" alt="Story image for searx from Golem.de" onload="typeof google==='object'&&google.aft&&google.aft(this)">
  55. </a>
  56. <div class="_hJs">
  57. <h3 class="r _gJs">
  58. <a class="l _PMs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAgglKAAwAQ','','',event)">Example title 2</a>
  59. </h3>
  60. <div class="slp">
  61. <span class="_OHs _PHs">
  62. Golem.de</span>
  63. <span class="_QGs">
  64. -</span>
  65. <span class="f nsa _QHs">
  66. Oct 4, 2016</span>
  67. </div>
  68. <div class="st">Example description 2</div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. """ # noqa
  76. response = mock.Mock(text=html)
  77. results = google_news.response(response)
  78. self.assertEqual(type(results), list)
  79. self.assertEqual(len(results), 2)
  80. self.assertEqual(results[0]['title'], u'Example title')
  81. self.assertEqual(results[0]['url'], 'https://example.com/')
  82. self.assertEqual(results[0]['content'], 'Example description')
  83. self.assertEqual(results[1]['title'], u'Example title 2')
  84. self.assertEqual(results[1]['url'], 'https://example2.com/')
  85. self.assertEqual(results[1]['content'], 'Example description 2')
  86. self.assertEqual(results[1]['img_src'], 'https://example2.com/image.jpg')