test_google_news.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import google_news
  4. from searx.testing import SearxTestCase
  5. class TestGoogleNewsEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 1
  10. dicto['language'] = 'fr_FR'
  11. params = google_news.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('googleapis.com', params['url'])
  15. self.assertIn('fr', params['url'])
  16. dicto['language'] = 'all'
  17. params = google_news.request(query, dicto)
  18. self.assertIn('url', params)
  19. self.assertIn('en', params['url'])
  20. def test_response(self):
  21. self.assertRaises(AttributeError, google_news.response, None)
  22. self.assertRaises(AttributeError, google_news.response, [])
  23. self.assertRaises(AttributeError, google_news.response, '')
  24. self.assertRaises(AttributeError, google_news.response, '[]')
  25. response = mock.Mock(text='{}')
  26. self.assertEqual(google_news.response(response), [])
  27. response = mock.Mock(text='{"data": []}')
  28. self.assertEqual(google_news.response(response), [])
  29. json = """
  30. {
  31. "responseData": {
  32. "results": [
  33. {
  34. "GsearchResultClass": "GnewsSearch",
  35. "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr",
  36. "content": "This is the content",
  37. "unescapedUrl": "http://this.is.the.url",
  38. "url": "http://this.is.the.url",
  39. "title": "This is the title",
  40. "titleNoFormatting": "This is the title",
  41. "location": "",
  42. "publisher": "Jeux Actu",
  43. "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800",
  44. "signedRedirectUrl": "http://news.google.com/",
  45. "language": "fr",
  46. "image": {
  47. "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg",
  48. "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcSF4yYrs9Ycw23DGiOSAZ-5SEPXYwG3LNs",
  49. "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm",
  50. "publisher": "Jeux Actu",
  51. "tbWidth": 80,
  52. "tbHeight": 30
  53. },
  54. "relatedStories": [
  55. {
  56. "unescapedUrl": "http://www.jeuxvideo.com/test/415823/dying-light.htm",
  57. "url": "http%3A%2F%2Fwww.jeuxvideo.com%2Ftest%2F415823%2Fdying-light.htm",
  58. "title": "<b>Test</b> du jeu Dying Light - jeuxvideo.com",
  59. "titleNoFormatting": "Test du jeu Dying Light - jeuxvideo.com",
  60. "location": "",
  61. "publisher": "JeuxVideo.com",
  62. "publishedDate": "Fri, 30 Jan 2015 08:52:30 -0800",
  63. "signedRedirectUrl": "http://news.google.com/news/url?sa=T&",
  64. "language": "fr"
  65. }
  66. ]
  67. }
  68. ]
  69. },
  70. "responseDetails": null,
  71. "responseStatus": 200
  72. }
  73. """
  74. response = mock.Mock(text=json)
  75. results = google_news.response(response)
  76. self.assertEqual(type(results), list)
  77. self.assertEqual(len(results), 1)
  78. self.assertEqual(results[0]['title'], 'This is the title')
  79. self.assertEqual(results[0]['url'], 'http://this.is.the.url')
  80. self.assertEqual(results[0]['content'], 'This is the content')
  81. json = """
  82. {
  83. "responseData": {
  84. "results": [
  85. {
  86. "GsearchResultClass": "GnewsSearch",
  87. "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr",
  88. "content": "This is the content",
  89. "unescapedUrl": "http://this.is.the.url",
  90. "title": "This is the title",
  91. "titleNoFormatting": "This is the title",
  92. "location": "",
  93. "publisher": "Jeux Actu",
  94. "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800",
  95. "signedRedirectUrl": "http://news.google.com/news/",
  96. "language": "fr",
  97. "image": {
  98. "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg",
  99. "tbUrl": "http://t1.gstatic.com/images?q=tbn:b_6f-OSAZ-5SEPXYwG3LNs",
  100. "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm",
  101. "publisher": "Jeux Actu",
  102. "tbWidth": 80,
  103. "tbHeight": 30
  104. }
  105. }
  106. ]
  107. },
  108. "responseDetails": null,
  109. "responseStatus": 200
  110. }
  111. """
  112. response = mock.Mock(text=json)
  113. results = google_news.response(response)
  114. self.assertEqual(type(results), list)
  115. self.assertEqual(len(results), 0)
  116. json = """
  117. {
  118. "responseData": {},
  119. "responseDetails": null,
  120. "responseStatus": 200
  121. }
  122. """
  123. response = mock.Mock(text=json)
  124. results = google_news.response(response)
  125. self.assertEqual(type(results), list)
  126. self.assertEqual(len(results), 0)