test_google_news.py 4.4 KB

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