logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_bing_news.py (6912B)


  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import bing_news
  4. from searx.testing import SearxTestCase
  5. import lxml
  6. class TestBingNewsEngine(SearxTestCase):
  7. def test_request(self):
  8. bing_news.supported_languages = ['en', 'fr']
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. dicto['pageno'] = 1
  12. dicto['language'] = 'fr-FR'
  13. dicto['time_range'] = ''
  14. params = bing_news.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn(query, params['url'])
  17. self.assertIn('bing.com', params['url'])
  18. self.assertIn('fr', params['url'])
  19. def test_no_url_in_request_year_time_range(self):
  20. dicto = defaultdict(dict)
  21. query = 'test_query'
  22. dicto['time_range'] = 'year'
  23. params = bing_news.request(query, dicto)
  24. self.assertEqual({}, params['url'])
  25. def test_response(self):
  26. self.assertRaises(AttributeError, bing_news.response, None)
  27. self.assertRaises(AttributeError, bing_news.response, [])
  28. self.assertRaises(AttributeError, bing_news.response, '')
  29. self.assertRaises(AttributeError, bing_news.response, '[]')
  30. response = mock.Mock(content='<html></html>')
  31. self.assertEqual(bing_news.response(response), [])
  32. response = mock.Mock(content='<html></html>')
  33. self.assertEqual(bing_news.response(response), [])
  34. html = """<?xml version="1.0" encoding="utf-8" ?>
  35. <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
  36. <channel>
  37. <title>python - Bing News</title>
  38. <link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  39. <description>Search results</description>
  40. <image>
  41. <url>http://10.53.64.9/rsslogo.gif</url>
  42. <title>test</title>
  43. <link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  44. </image>
  45. <copyright>Copyright</copyright>
  46. <item>
  47. <title>Title</title>
  48. <link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&amp;aid=&amp;tid=c237eccc50bd4758b106a5e3c94fce09&amp;url=http%3a%2f%2furl.of.article%2f&amp;c=xxxxxxxxx&amp;mkt=en-us</link>
  49. <description>Article Content</description>
  50. <pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate>
  51. <News:Source>Infoworld</News:Source>
  52. <News:Image>http://a1.bing4.com/th?id=ON.13371337133713371337133713371337&amp;pid=News</News:Image>
  53. <News:ImageSize>w={0}&amp;h={1}&amp;c=7</News:ImageSize>
  54. <News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio>
  55. <News:ImageMaxWidth>620</News:ImageMaxWidth>
  56. <News:ImageMaxHeight>413</News:ImageMaxHeight>
  57. </item>
  58. <item>
  59. <title>Another Title</title>
  60. <link>https://www.bing.com/news/apiclick.aspx?ref=FexRss&amp;aid=&amp;tid=c237eccc50bd4758b106a5e3c94fce09&amp;url=http%3a%2f%2fanother.url.of.article%2f&amp;c=xxxxxxxxx&amp;mkt=en-us</link>
  61. <description>Another Article Content</description>
  62. <pubDate>Tue, 02 Jun 2015 13:37:00 GMT</pubDate>
  63. </item>
  64. </channel>
  65. </rss>""" # noqa
  66. response = mock.Mock(content=html.encode('utf-8'))
  67. results = bing_news.response(response)
  68. self.assertEqual(type(results), list)
  69. self.assertEqual(len(results), 2)
  70. self.assertEqual(results[0]['title'], 'Title')
  71. self.assertEqual(results[0]['url'], 'http://url.of.article/')
  72. self.assertEqual(results[0]['content'], 'Article Content')
  73. self.assertEqual(results[0]['img_src'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337')
  74. self.assertEqual(results[1]['title'], 'Another Title')
  75. self.assertEqual(results[1]['url'], 'http://another.url.of.article/')
  76. self.assertEqual(results[1]['content'], 'Another Article Content')
  77. self.assertNotIn('img_src', results[1])
  78. html = """<?xml version="1.0" encoding="utf-8" ?>
  79. <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
  80. <channel>
  81. <title>python - Bing News</title>
  82. <link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  83. <description>Search results</description>
  84. <image>
  85. <url>http://10.53.64.9/rsslogo.gif</url>
  86. <title>test</title>
  87. <link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  88. </image>
  89. <copyright>Copyright</copyright>
  90. <item>
  91. <title>Title</title>
  92. <link>http://another.url.of.article/</link>
  93. <description>Article Content</description>
  94. <pubDate>garbage</pubDate>
  95. <News:Source>Infoworld</News:Source>
  96. <News:Image>http://another.bing.com/image</News:Image>
  97. <News:ImageSize>w={0}&amp;h={1}&amp;c=7</News:ImageSize>
  98. <News:ImageKeepOriginalRatio></News:ImageKeepOriginalRatio>
  99. <News:ImageMaxWidth>620</News:ImageMaxWidth>
  100. <News:ImageMaxHeight>413</News:ImageMaxHeight>
  101. </item>
  102. </channel>
  103. </rss>""" # noqa
  104. response = mock.Mock(content=html.encode('utf-8'))
  105. results = bing_news.response(response)
  106. self.assertEqual(type(results), list)
  107. self.assertEqual(len(results), 1)
  108. self.assertEqual(results[0]['title'], 'Title')
  109. self.assertEqual(results[0]['url'], 'http://another.url.of.article/')
  110. self.assertEqual(results[0]['content'], 'Article Content')
  111. self.assertEqual(results[0]['img_src'], 'http://another.bing.com/image')
  112. html = """<?xml version="1.0" encoding="utf-8" ?>
  113. <rss version="2.0" xmlns:News="https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS">
  114. <channel>
  115. <title>python - Bing News</title>
  116. <link>https://www.bing.com:443/news/search?q=python&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  117. <description>Search results</description>
  118. <image>
  119. <url>http://10.53.64.9/rsslogo.gif</url>
  120. <title>test</title>
  121. <link>https://www.bing.com:443/news/search?q=test&amp;setmkt=en-US&amp;first=1&amp;format=RSS</link>
  122. </image>
  123. </channel>
  124. </rss>""" # noqa
  125. response = mock.Mock(content=html.encode('utf-8'))
  126. results = bing_news.response(response)
  127. self.assertEqual(type(results), list)
  128. self.assertEqual(len(results), 0)
  129. html = """<?xml version="1.0" encoding="utf-8" ?>gabarge"""
  130. response = mock.Mock(content=html.encode('utf-8'))
  131. self.assertRaises(lxml.etree.XMLSyntaxError, bing_news.response, response)