on master help sign in
Yacy's unit test
0a537d3Cqoicebordel
searx/engines/yacy.py+11−8
@@ -25,10 +25,10 @@ number_of_results = 5
25# search-url
25# search-url
26base_url = 'http://localhost:8090'
26base_url = 'http://localhost:8090'
27search_url = '/yacysearch.json?{query}'\
27search_url = '/yacysearch.json?{query}'\
28− '&startRecord={offset}'\
28+ '&startRecord={offset}'\
29− '&maximumRecords={limit}'\
29+ '&maximumRecords={limit}'\
30− '&contentdom={search_type}'\
30+ '&contentdom={search_type}'\
31− '&resource=global' # noqa
31+ '&resource=global'
32
32
33# yacy specific type-definitions
33# yacy specific type-definitions
34search_types = {'general': 'text',
34search_types = {'general': 'text',
@@ -41,7 +41,7 @@ search_types = {'general': 'text',
41# do search-request
41# do search-request
42def request(query, params):
42def request(query, params):
43 offset = (params['pageno'] - 1) * number_of_results
43 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')
45
45
46 params['url'] = base_url +\
46 params['url'] = base_url +\
47 search_url.format(query=urlencode({'query': query}),
47 search_url.format(query=urlencode({'query': query}),
@@ -66,9 +66,12 @@ def response(resp):
66 if not raw_search_results:
66 if not raw_search_results:
67 return []
67 return []
68
68
69− search_results = raw_search_results.get('channels', {})[0].get('items', [])
69+ search_results = raw_search_results.get('channels', [])
70
70
71+ if len(search_results) == 0:
72+ return []
73+
71− for result in search_results:
74+ for result in search_results[0].get('items', []):
72 # parse image results
75 # parse image results
73 if result.get('image'):
76 if result.get('image'):
74 # append result
77 # append result
@@ -88,7 +91,7 @@ def response(resp):
88 'content': result['description'],
91 'content': result['description'],
89 'publishedDate': publishedDate})
92 'publishedDate': publishedDate})
90
93
91− #TODO parse video, audio and file results
94+ # TODO parse video, audio and file results
92
95
93 # return results
96 # return results
94 return results
97 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&resource=global&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&resource=global&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&maxheight=96&code=7EbAbW6BpPOA",
73+ "image": "http://image.url/image.png",
74+ "cache": "/ViewImage.png?quadratic=&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
30from searx.tests.engines.test_twitter import * # noqa
30from searx.tests.engines.test_twitter import * # noqa
31from searx.tests.engines.test_vimeo import * # noqa
31from searx.tests.engines.test_vimeo import * # noqa
32from searx.tests.engines.test_www500px import * # noqa
32from 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
34from searx.tests.engines.test_yahoo import * # noqa
34from searx.tests.engines.test_yahoo import * # noqa
35+from searx.tests.engines.test_youtube import * # noqa
35from searx.tests.engines.test_yahoo_news import * # noqa
36from searx.tests.engines.test_yahoo_news import * # noqa