from collections import defaultdict
import mock
from searx.engines import bing_news
from searx.testing import SearxTestCase
import lxml
class TestBingNewsEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr_FR'
dicto['time_range'] = ''
params = bing_news.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('bing.com', params['url'])
self.assertIn('fr', params['url'])
dicto['language'] = 'all'
params = bing_news.request(query, dicto)
self.assertIn('en', params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = bing_news.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self):
self.assertRaises(AttributeError, bing_news.response, None)
self.assertRaises(AttributeError, bing_news.response, [])
self.assertRaises(AttributeError, bing_news.response, '')
self.assertRaises(AttributeError, bing_news.response, '[]')
response = mock.Mock(text='')
self.assertEqual(bing_news.response(response), [])
response = mock.Mock(text='')
self.assertEqual(bing_news.response(response), [])
html = """
python - Bing News
https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS
Search results
http://10.53.64.9/rsslogo.gif
test
https://www.bing.com:443/news/search?q=test&setmkt=en-US&first=1&format=RSS
Copyright
-
Title
https://www.bing.com/news/apiclick.aspx?ref=FexRss&aid=&tid=c237eccc50bd4758b106a5e3c94fce09&url=http%3a%2f%2furl.of.article%2f&c=xxxxxxxxx&mkt=en-us
Article Content
Tue, 02 Jun 2015 13:37:00 GMT
Infoworld
http://a1.bing4.com/th?id=ON.13371337133713371337133713371337&pid=News
w={0}&h={1}&c=7
620
413
-
Another Title
https://www.bing.com/news/apiclick.aspx?ref=FexRss&aid=&tid=c237eccc50bd4758b106a5e3c94fce09&url=http%3a%2f%2fanother.url.of.article%2f&c=xxxxxxxxx&mkt=en-us
Another Article Content
Tue, 02 Jun 2015 13:37:00 GMT
""" # noqa
response = mock.Mock(text=html.encode('utf-8'))
results = bing_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'http://url.of.article/')
self.assertEqual(results[0]['content'], 'Article Content')
self.assertEqual(results[0]['img_src'], 'https://www.bing.com/th?id=ON.13371337133713371337133713371337')
self.assertEqual(results[1]['title'], 'Another Title')
self.assertEqual(results[1]['url'], 'http://another.url.of.article/')
self.assertEqual(results[1]['content'], 'Another Article Content')
self.assertNotIn('img_src', results[1])
html = """
python - Bing News
https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS
Search results
http://10.53.64.9/rsslogo.gif
test
https://www.bing.com:443/news/search?q=test&setmkt=en-US&first=1&format=RSS
Copyright
-
Title
http://another.url.of.article/
Article Content
garbage
Infoworld
http://another.bing.com/image
w={0}&h={1}&c=7
620
413
""" # noqa
response = mock.Mock(text=html.encode('utf-8'))
results = bing_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title')
self.assertEqual(results[0]['url'], 'http://another.url.of.article/')
self.assertEqual(results[0]['content'], 'Article Content')
self.assertEqual(results[0]['img_src'], 'http://another.bing.com/image')
html = """
python - Bing News
https://www.bing.com:443/news/search?q=python&setmkt=en-US&first=1&format=RSS
Search results
http://10.53.64.9/rsslogo.gif
test
https://www.bing.com:443/news/search?q=test&setmkt=en-US&first=1&format=RSS
""" # noqa
response = mock.Mock(text=html.encode('utf-8'))
results = bing_news.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = """gabarge"""
response = mock.Mock(text=html.encode('utf-8'))
self.assertRaises(lxml.etree.XMLSyntaxError, bing_news.response, response)