test_google_news.py 4.2KB

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