From d4957045513d6fb32dcffbc7ea87483479a8cb6e Mon Sep 17 00:00:00 2001 From: Cqoicebordel Date: Thu, 29 Jan 2015 01:13:33 +0100 Subject: [PATCH] Deviant Art's unit test --- searx/engines/deviantart.py | 7 +- searx/tests/engines/test_deviantart.py | 118 +++++++++++++++++++++++++ searx/tests/engines/test_digg.py | 46 +++++++++- searx/tests/test_engines.py | 1 + 4 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 searx/tests/engines/test_deviantart.py diff --git a/searx/engines/deviantart.py b/searx/engines/deviantart.py index 6284cf59..4198e8c7 100644 --- a/searx/engines/deviantart.py +++ b/searx/engines/deviantart.py @@ -14,6 +14,7 @@ from urllib import urlencode from urlparse import urljoin from lxml import html import re +from searx.engines.xpath import extract_text # engine dependent config categories = ['images'] @@ -50,9 +51,9 @@ def response(resp): for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'): link = result.xpath('.//a[contains(@class, "thumb")]')[0] url = urljoin(base_url, link.attrib.get('href')) - title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa - title = ''.join(title_links[0].xpath('.//text()')) - thumbnail_src = link.xpath('.//img')[0].attrib['src'] + title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') + title = extract_text(title_links[0]) + thumbnail_src = link.xpath('.//img')[0].attrib.get('src') img_src = regex.sub('/', thumbnail_src) # append result diff --git a/searx/tests/engines/test_deviantart.py b/searx/tests/engines/test_deviantart.py new file mode 100644 index 00000000..9cf68d0b --- /dev/null +++ b/searx/tests/engines/test_deviantart.py @@ -0,0 +1,118 @@ +from collections import defaultdict +import mock +from searx.engines import deviantart +from searx.testing import SearxTestCase + + +class TestDeviantartEngine(SearxTestCase): + + def test_request(self): + query = 'test_query' + dicto = defaultdict(dict) + dicto['pageno'] = 0 + params = deviantart.request(query, dicto) + self.assertTrue('url' in params) + self.assertTrue(query in params['url']) + self.assertTrue('deviantart.com' in params['url']) + + def test_response(self): + self.assertRaises(AttributeError, deviantart.response, None) + self.assertRaises(AttributeError, deviantart.response, []) + self.assertRaises(AttributeError, deviantart.response, '') + self.assertRaises(AttributeError, deviantart.response, '[]') + + response = mock.Mock(text='') + self.assertEqual(deviantart.response(response), []) + + response = mock.Mock(status_code=302) + self.assertEqual(deviantart.response(response), []) + + html = """ +
+ + + + + + + Test + + + + + + + Title of image + + + + 5 years ago + + in Animation + + + + More Like This + + + +
+ """ + response = mock.Mock(text=html) + results = deviantart.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 1) + self.assertEqual(results[0]['title'], 'Title of image') + self.assertEqual(results[0]['url'], 'http://url.of.result/2nd.part.of.url') + self.assertNotIn('content', results[0]) + self.assertEqual(results[0]['thumbnail_src'], 'http://url.of.thumbnail') + + html = """ + + + + + + Test + + + + + + + Title of image + + + + 5 years ago + + in Animation + + + + More Like This + + + """ + response = mock.Mock(text=html) + results = deviantart.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 0) diff --git a/searx/tests/engines/test_digg.py b/searx/tests/engines/test_digg.py index 7e9006c0..6e7c9cc9 100644 --- a/searx/tests/engines/test_digg.py +++ b/searx/tests/engines/test_digg.py @@ -32,9 +32,53 @@ class TestDiggEngine(SearxTestCase): "status": "ok", "num": 10, "next_position": 20, - "html": "
" + "html": "
+ \\"\\"
" } """ + json = json.replace('\r\n', '').replace('\n', '').replace('\r', '') response = mock.Mock(text=json) results = digg.response(response) self.assertEqual(type(results), list) diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py index 309e83f1..561b436f 100644 --- a/searx/tests/test_engines.py +++ b/searx/tests/test_engines.py @@ -1,6 +1,7 @@ from searx.tests.engines.test_bing import * # noqa from searx.tests.engines.test_dailymotion import * # noqa from searx.tests.engines.test_deezer import * # noqa +from searx.tests.engines.test_deviantart import * # noqa from searx.tests.engines.test_digg import * # noqa from searx.tests.engines.test_dummy import * # noqa from searx.tests.engines.test_flickr import * # noqa