from collections import defaultdict import mock from searx.engines import swisscows from searx.testing import SearxTestCase class TestSwisscowsEngine(SearxTestCase): def test_request(self): swisscows.supported_languages = ['de-AT', 'de-DE'] swisscows.language_aliases = {} query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'de-DE' params = swisscows.request(query, dicto) self.assertTrue('url' in params) self.assertTrue(query in params['url']) self.assertTrue('swisscows.ch' in params['url']) self.assertTrue('uiLanguage=de' in params['url']) self.assertTrue('region=de-DE' in params['url']) dicto['category'] = 'images' params = swisscows.request(query, dicto) self.assertIn('image', params['url']) def test_response(self): self.assertRaises(AttributeError, swisscows.response, None) self.assertRaises(AttributeError, swisscows.response, []) self.assertRaises(AttributeError, swisscows.response, '') self.assertRaises(AttributeError, swisscows.response, '[]') response = mock.Mock(text=b'') self.assertEqual(swisscows.response(response), []) response = mock.Mock(text=b'') self.assertEqual(swisscows.response(response), []) html = b""" """ response = mock.Mock(text=html) results = swisscows.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 3) 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]['title'], 'Datei:This should1.svg') self.assertEqual(results[1]['url'], 'http://de.wikipedia.org/wiki/Datei:This should1.svg') self.assertEqual(results[1]['img_src'], 'http://ts2.mm.This/should1.png') self.assertEqual(results[1]['template'], 'images.html') self.assertEqual(results[2]['title'], 'Datei:This should.svg') self.assertEqual(results[2]['url'], 'http://de.wikipedia.org/wiki/Datei:This should.svg') self.assertEqual(results[2]['img_src'], 'http://ts2.mm.This/should.png') self.assertEqual(results[2]['template'], 'images.html') def test_fetch_supported_languages(self): html = """""" response = mock.Mock(text=html) languages = swisscows._fetch_supported_languages(response) self.assertEqual(type(languages), list) self.assertEqual(len(languages), 0) html = """
""" response = mock.Mock(text=html) languages = swisscows._fetch_supported_languages(response) self.assertEqual(type(languages), list) self.assertEqual(len(languages), 3) self.assertIn('de-CH', languages) self.assertIn('fr-CH', languages)