from collections import defaultdict import mock from searx.engines import bing_news from searx.testing import SearxTestCase class TestBingNewsEngine(SearxTestCase): def test_request(self): query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr_FR' 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']) self.assertIn('_FP', params['cookies']) self.assertIn('en', params['cookies']['_FP']) dicto['language'] = 'all' params = bing_news.request(query, dicto) self.assertIn('en', params['url']) self.assertIn('_FP', params['cookies']) self.assertIn('en', params['cookies']['_FP']) 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(content='') self.assertEqual(bing_news.response(response), []) response = mock.Mock(content='') self.assertEqual(bing_news.response(response), []) html = """
Title
Article Content metronews.fr  · 44 minutes ago
""" response = mock.Mock(content=html) 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://url.of.article/') self.assertEqual(results[0]['content'], 'Article Content') html = """
Title
Article Content metronews.fr  · 44 minutes ago
Title
Article Content metronews.fr  · 3 hours, 44 minutes ago
Title
Article Content metronews.fr  · 44 hours ago
Title
Article Content metronews.fr  · 2 days ago
Title
Article Content metronews.fr  · 27/01/2015
Title
Article Content metronews.fr  · Il y a 3 heures
""" response = mock.Mock(content=html) results = bing_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 6) html = """
Title
Article Content metronews.fr  · 44 minutes ago
""" response = mock.Mock(content=html) results = bing_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0)