From d5b8005ee10054b5260f57c1800ddebfa03c39cf Mon Sep 17 00:00:00 2001 From: Cqoicebordel Date: Sat, 31 Jan 2015 16:16:30 +0100 Subject: [PATCH] Google images' unit test --- searx/engines/google_images.py | 6 +- searx/tests/engines/test_google_images.py | 108 ++++++++++++++++++++++ searx/tests/test_engines.py | 1 + 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 searx/tests/engines/test_google_images.py diff --git a/searx/engines/google_images.py b/searx/engines/google_images.py index cc62a4fd2..092ae6639 100644 --- a/searx/engines/google_images.py +++ b/searx/engines/google_images.py @@ -18,7 +18,7 @@ paging = True # search-url url = 'https://ajax.googleapis.com/' -search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe=off&filter=off&{query}' # noqa +search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe=off&filter=off&{query}' # do search-request @@ -45,14 +45,14 @@ def response(resp): for result in search_res['responseData']['results']: href = result['originalContextUrl'] title = result['title'] - if not result['url']: + if 'url' not in result: continue thumbnail_src = result['tbUrl'] # append result results.append({'url': href, 'title': title, - 'content': '', + 'content': result['content'], 'thumbnail_src': thumbnail_src, 'img_src': unquote(result['url']), 'template': 'images.html'}) diff --git a/searx/tests/engines/test_google_images.py b/searx/tests/engines/test_google_images.py new file mode 100644 index 000000000..6870ff52f --- /dev/null +++ b/searx/tests/engines/test_google_images.py @@ -0,0 +1,108 @@ +from collections import defaultdict +import mock +from searx.engines import google_images +from searx.testing import SearxTestCase + + +class TestGoogleImagesEngine(SearxTestCase): + + def test_request(self): + query = 'test_query' + dicto = defaultdict(dict) + dicto['pageno'] = 1 + params = google_images.request(query, dicto) + self.assertTrue('url' in params) + self.assertTrue(query in params['url']) + self.assertTrue('googleapis.com' in params['url']) + + def test_response(self): + self.assertRaises(AttributeError, google_images.response, None) + self.assertRaises(AttributeError, google_images.response, []) + self.assertRaises(AttributeError, google_images.response, '') + self.assertRaises(AttributeError, google_images.response, '[]') + + response = mock.Mock(text='{}') + self.assertEqual(google_images.response(response), []) + + response = mock.Mock(text='{"data": []}') + self.assertEqual(google_images.response(response), []) + + json = """ + { + "responseData": { + "results": [ + { + "GsearchResultClass": "GimageSearch", + "width": "400", + "height": "400", + "imageId": "ANd9GcQbYb9FJuAbG_hT4i8FeC0O0x-P--EHdzgRIF9ao97nHLl7C2mREn6qTQ", + "tbWidth": "124", + "tbHeight": "124", + "unescapedUrl": "http://unescaped.url.jpg", + "url": "http://image.url.jpg", + "visibleUrl": "insolitebuzz.fr", + "title": "This is the title", + "titleNoFormatting": "Petit test sympa qui rend fou tout le monde ! A faire", + "originalContextUrl": "http://this.is.the.url", + "content": "test", + "contentNoFormatting": "test", + "tbUrl": "http://thumbnail.url" + } + ] + }, + "responseDetails": null, + "responseStatus": 200 + } + """ + response = mock.Mock(text=json) + results = google_images.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 1) + self.assertEqual(results[0]['title'], 'This is the title') + self.assertEqual(results[0]['url'], 'http://this.is.the.url') + self.assertEqual(results[0]['thumbnail_src'], 'http://thumbnail.url') + self.assertEqual(results[0]['img_src'], 'http://image.url.jpg') + self.assertEqual(results[0]['content'], 'test') + + json = """ + { + "responseData": { + "results": [ + { + "GsearchResultClass": "GimageSearch", + "width": "400", + "height": "400", + "imageId": "ANd9GcQbYb9FJuAbG_hT4i8FeC0O0x-P--EHdzgRIF9ao97nHLl7C2mREn6qTQ", + "tbWidth": "124", + "tbHeight": "124", + "unescapedUrl": "http://unescaped.url.jpg", + "visibleUrl": "insolitebuzz.fr", + "title": "This is the title", + "titleNoFormatting": "Petit test sympa qui rend fou tout le monde ! A faire", + "originalContextUrl": "http://this.is.the.url", + "content": "test", + "contentNoFormatting": "test", + "tbUrl": "http://thumbnail.url" + } + ] + }, + "responseDetails": null, + "responseStatus": 200 + } + """ + response = mock.Mock(text=json) + results = google_images.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 0) + + json = """ + { + "responseData": {}, + "responseDetails": null, + "responseStatus": 200 + } + """ + response = mock.Mock(text=json) + results = google_images.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 0) diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py index 66f8fbff7..e609f9a5c 100644 --- a/searx/tests/test_engines.py +++ b/searx/tests/test_engines.py @@ -9,6 +9,7 @@ from searx.tests.engines.test_digg import * # noqa from searx.tests.engines.test_dummy import * # noqa from searx.tests.engines.test_flickr import * # noqa from searx.tests.engines.test_github import * # noqa +from searx.tests.engines.test_google_images import * # noqa from searx.tests.engines.test_kickass import * # noqa from searx.tests.engines.test_mixcloud import * # noqa from searx.tests.engines.test_searchcode_code import * # noqa