# -*- coding: utf-8 -*- from collections import defaultdict import mock from searx.engines import bing from searx.testing import SearxTestCase class TestBingEngine(SearxTestCase): def test_request(self): bing.supported_languages = ['en', 'fr', 'zh-CHS', 'zh-CHT', 'pt-PT', 'pt-BR'] query = u'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr-FR' params = bing.request(query.encode('utf-8'), dicto) self.assertTrue('url' in params) self.assertTrue(query in params['url']) self.assertTrue('language%3AFR' in params['url']) self.assertTrue('bing.com' in params['url']) dicto['language'] = 'all' params = bing.request(query.encode('utf-8'), dicto) self.assertTrue('language' in params['url']) def test_response(self): dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr-FR' self.assertRaises(AttributeError, bing.response, None) self.assertRaises(AttributeError, bing.response, []) self.assertRaises(AttributeError, bing.response, '') self.assertRaises(AttributeError, bing.response, '[]') response = mock.Mock(text='') response.search_params = dicto self.assertEqual(bing.response(response), []) response = mock.Mock(text='') response.search_params = dicto self.assertEqual(bing.response(response), []) html = """
23 900 000 résultats
    this.meta.com

    This should be the content.

""" response = mock.Mock(text=html) response.search_params = dicto results = bing.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual(results[0]['title'], 'This should be the title') self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/') self.assertEqual(results[0]['content'], 'This should be the content.') self.assertEqual(results[-1]['number_of_results'], 23900000) html = """
9-18 résultats sur 23 900 000
  1. this.meta.com

    This should be the content.

""" dicto['pageno'] = 2 response = mock.Mock(text=html) response.search_params = dicto results = bing.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual(results[0]['title'], 'This should be the title') self.assertEqual(results[0]['url'], 'http://this.should.be.the.link/') self.assertEqual(results[0]['content'], 'This should be the content.') self.assertEqual(results[-1]['number_of_results'], 23900000) html = """
23 900 000 résultats
  1. this.meta.com

    This should be the content.

""" dicto['pageno'] = 33900000 response = mock.Mock(text=html) response.search_params = dicto results = bing.response(response) self.assertEqual(bing.response(response), []) def test_fetch_supported_languages(self): html = """""" response = mock.Mock(text=html) results = bing._fetch_supported_languages(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0) html = """
""" response = mock.Mock(text=html) languages = bing._fetch_supported_languages(response) self.assertEqual(type(languages), list) self.assertEqual(len(languages), 3) self.assertIn('es', languages) self.assertIn('pt-BR', languages) self.assertIn('pt-PT', languages)