|
@@ -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')
|