on master help sign in
Yacy's unit test
commit0a537d3b89964c227724d3cd95adebd2bbafb720 authorCqoicebordel <Cqoicebordel@users.noreply.github.com> dateMon 9 Feb 2015, 16:55 (12 years ago) parentfb04f76
3 files changed+109−9
searx/engines/yacy.py+11−8 searx/tests/engines/test_yacy.py+96 searx/tests/test_engines.py+2−1
searx/engines/yacy.py+11−8
@@ -25,10 +25,10 @@ number_of_results = 5
2525# search-url
2626base_url = 'http://localhost:8090'
2727search_url = '/yacysearch.json?{query}'\
28− '&startRecord={offset}'\
29− '&maximumRecords={limit}'\
30− '&contentdom={search_type}'\
31− '&resource=global' # noqa
28+ '&startRecord={offset}'\
29+ '&maximumRecords={limit}'\
30+ '&contentdom={search_type}'\
31+ '&resource=global'
3232
3333# yacy specific type-definitions
3434search_types = {'general': 'text',
@@ -41,7 +41,7 @@ search_types = {'general': 'text',
4141# do search-request
4242def request(query, params):
4343 offset = (params['pageno'] - 1) * number_of_results
44− search_type = search_types.get(params['category'], '0')
44+ search_type = search_types.get(params.get('category'), '0')
4545
4646 params['url'] = base_url +\
4747 search_url.format(query=urlencode({'query': query}),
@@ -66,9 +66,12 @@ def response(resp):
6666 if not raw_search_results:
6767 return []
6868
69− search_results = raw_search_results.get('channels', {})[0].get('items', [])
69+ search_results = raw_search_results.get('channels', [])
7070
71− for result in search_results:
71+ if len(search_results) == 0:
72+ return []
73+
74+ for result in search_results[0].get('items', []):
7275 # parse image results
7376 if result.get('image'):
7477 # append result
@@ -88,7 +91,7 @@ def response(resp):
8891 'content': result['description'],
8992 'publishedDate': publishedDate})
9093
91− #TODO parse video, audio and file results
94+ # TODO parse video, audio and file results
9295
9396 # return results
9497 return results
searx/tests/engines/test_yacy.pynew file+96
@@ -0,0 +1,96 @@
1+from collections import defaultdict
2+import mock
3+from searx.engines import yacy
4+from searx.testing import SearxTestCase
5+
6+
7+class TestYacyEngine(SearxTestCase):
8+
9+ def test_request(self):
10+ query = 'test_query'
11+ dicto = defaultdict(dict)
12+ dicto['pageno'] = 1
13+ dicto['language'] = 'fr_FR'
14+ params = yacy.request(query, dicto)
15+ self.assertIn('url', params)
16+ self.assertIn(query, params['url'])
17+ self.assertIn('localhost', params['url'])
18+ self.assertIn('fr', params['url'])
19+
20+ dicto['language'] = 'all'
21+ params = yacy.request(query, dicto)
22+ self.assertIn('url', params)
23+ self.assertNotIn('lr=lang_', params['url'])
24+
25+ def test_response(self):
26+ self.assertRaises(AttributeError, yacy.response, None)
27+ self.assertRaises(AttributeError, yacy.response, [])
28+ self.assertRaises(AttributeError, yacy.response, '')
29+ self.assertRaises(AttributeError, yacy.response, '[]')
30+
31+ response = mock.Mock(text='{}')
32+ self.assertEqual(yacy.response(response), [])
33+
34+ response = mock.Mock(text='{"data": []}')
35+ self.assertEqual(yacy.response(response), [])
36+
37+ json = """
38+ {
39+ "channels": [
40+ {
41+ "title": "YaCy P2P-Search for test",
42+ "description": "Search for test",
43+ "link": "http://search.yacy.de:7001/yacysearch.html?query=test&amp;resource=global&amp;contentdom=0",
44+ "image": {
45+ "url": "http://search.yacy.de:7001/env/grafics/yacy.png",
46+ "title": "Search for test",
47+ "link": "http://search.yacy.de:7001/yacysearch.html?query=test&amp;resource=global&amp;contentdom=0"
48+ },
49+ "totalResults": "249",
50+ "startIndex": "0",
51+ "itemsPerPage": "5",
52+ "searchTerms": "test",
53+ "items": [
54+ {
55+ "title": "This is the title",
56+ "link": "http://this.is.the.url",
57+ "code": "",
58+ "description": "This should be the content",
59+ "pubDate": "Sat, 08 Jun 2013 02:00:00 +0200",
60+ "size": "44213",
61+ "sizename": "43 kbyte",
62+ "guid": "lzh_1T_5FP-A",
63+ "faviconCode": "XTS4uQ_5FP-A",
64+ "host": "www.gamestar.de",
65+ "path": "/spiele/city-of-heroes-freedom/47019.html",
66+ "file": "47019.html",
67+ "urlhash": "lzh_1T_5FP-A",
68+ "ranking": "0.20106804"
69+ },
70+ {
71+ "title": "This is the title2",
72+ "icon": "/ViewImage.png?maxwidth=96&amp;maxheight=96&amp;code=7EbAbW6BpPOA",
73+ "image": "http://image.url/image.png",
74+ "cache": "/ViewImage.png?quadratic=&amp;url=http://golem.ivwbox.de/cgi-bin/ivw/CP/G_INET?d=14071378",
75+ "url": "http://this.is.the.url",
76+ "urlhash": "7EbAbW6BpPOA",
77+ "host": "www.golem.de",
78+ "width": "-1",
79+ "height": "-1"
80+ }
81+ ]
82+ }
83+ ]
84+ }
85+ """
86+ response = mock.Mock(text=json)
87+ results = yacy.response(response)
88+ self.assertEqual(type(results), list)
89+ self.assertEqual(len(results), 2)
90+ self.assertEqual(results[0]['title'], 'This is the title')
91+ self.assertEqual(results[0]['url'], 'http://this.is.the.url')
92+ self.assertEqual(results[0]['content'], 'This should be the content')
93+ self.assertEqual(results[1]['img_src'], 'http://image.url/image.png')
94+ self.assertEqual(results[1]['content'], '')
95+ self.assertEqual(results[1]['url'], 'http://this.is.the.url')
96+ self.assertEqual(results[1]['title'], 'This is the title2')
searx/tests/test_engines.py+2−1
@@ -30,6 +30,7 @@ from searx.tests.engines.test_subtitleseeker import * # noqa
3030from searx.tests.engines.test_twitter import * # noqa
3131from searx.tests.engines.test_vimeo import * # noqa
3232from searx.tests.engines.test_www500px import * # noqa
33−from searx.tests.engines.test_youtube import * # noqa
33+from searx.tests.engines.test_yacy import * # noqa
3434from searx.tests.engines.test_yahoo import * # noqa
35+from searx.tests.engines.test_youtube import * # noqa
3536from searx.tests.engines.test_yahoo_news import * # noqa