From 53979a7bf7669c803c2a493fbf136519f6a293e6 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Sat, 2 Jan 2016 11:14:49 +0100 Subject: [mod] remove buildout/makefile infrastructure --- tests/unit/engines/test_google.py | 178 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 tests/unit/engines/test_google.py (limited to 'tests/unit/engines/test_google.py') diff --git a/tests/unit/engines/test_google.py b/tests/unit/engines/test_google.py new file mode 100644 index 000000000..37a83cae3 --- /dev/null +++ b/tests/unit/engines/test_google.py @@ -0,0 +1,178 @@ +# -*- coding: utf-8 -*- +from collections import defaultdict +import mock +import lxml +from searx.engines import google +from searx.testing import SearxTestCase + + +class TestGoogleEngine(SearxTestCase): + + def mock_response(self, text): + response = mock.Mock(text=text, url='https://www.google.com/search?q=test&start=0&gbv=1&gws_rd=cr') + response.search_params = mock.Mock() + response.search_params.get = mock.Mock(return_value='www.google.com') + return response + + def test_request(self): + query = 'test_query' + dicto = defaultdict(dict) + dicto['pageno'] = 1 + dicto['language'] = 'fr_FR' + params = google.request(query, dicto) + self.assertIn('url', params) + self.assertIn(query, params['url']) + self.assertIn('google.fr', params['url']) + self.assertIn('fr', params['headers']['Accept-Language']) + + dicto['language'] = 'all' + params = google.request(query, dicto) + self.assertIn('google.com', params['url']) + self.assertIn('en', params['headers']['Accept-Language']) + + def test_response(self): + self.assertRaises(AttributeError, google.response, None) + self.assertRaises(AttributeError, google.response, []) + self.assertRaises(AttributeError, google.response, '') + self.assertRaises(AttributeError, google.response, '[]') + + response = self.mock_response('') + self.assertEqual(google.response(response), []) + + html = """ +
+

+ + This is the title + +

+
+
+ + test.psychologies.com/ + +
‎ + + +
+
+ + This should be the content. + +
+ +
+
+
+

+ + This + +

+
+
+

+ + This is + +

+
+
+

+ + This is the + +

+
+
+

+ + This is the + +

+
+

+ + suggestion title + +

+ """ + response = self.mock_response(html) + results = google.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertEqual(results[0]['title'], 'This is 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]['suggestion'], 'suggestion title') + + html = """ +
  • +
  • + """ + response = self.mock_response(html) + results = google.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 0) + + response = mock.Mock(text='', url='https://sorry.google.com') + response.search_params = mock.Mock() + response.search_params.get = mock.Mock(return_value='www.google.com') + self.assertRaises(RuntimeWarning, google.response, response) + + response = mock.Mock(text='', url='https://www.google.com/sorry/IndexRedirect') + response.search_params = mock.Mock() + response.search_params.get = mock.Mock(return_value='www.google.com') + self.assertRaises(RuntimeWarning, google.response, response) + + def test_parse_images(self): + html = """ +
  • +
    + + + +
    +
  • + """ + dom = lxml.html.fromstring(html) + results = google.parse_images(dom, 'www.google.com') + self.assertEqual(type(results), list) + self.assertEqual(len(results), 1) + self.assertEqual(results[0]['url'], 'http://this.is.the.url/') + self.assertEqual(results[0]['title'], '') + self.assertEqual(results[0]['content'], '') + self.assertEqual(results[0]['img_src'], 'https://this.is.the.image/image.jpg') -- cgit v1.2.3