瀏覽代碼

Yacy's unit test

Cqoicebordel 10 年之前
父節點
當前提交
0a537d3b89
共有 3 個檔案被更改,包括 109 行新增9 行删除
  1. 11
    8
      searx/engines/yacy.py
  2. 96
    0
      searx/tests/engines/test_yacy.py
  3. 2
    1
      searx/tests/test_engines.py

+ 11
- 8
searx/engines/yacy.py 查看文件

@@ -25,10 +25,10 @@ number_of_results = 5
25 25
 # search-url
26 26
 base_url = 'http://localhost:8090'
27 27
 search_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'
32 32
 
33 33
 # yacy specific type-definitions
34 34
 search_types = {'general': 'text',
@@ -41,7 +41,7 @@ search_types = {'general': 'text',
41 41
 # do search-request
42 42
 def request(query, params):
43 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 46
     params['url'] = base_url +\
47 47
         search_url.format(query=urlencode({'query': query}),
@@ -66,9 +66,12 @@ def response(resp):
66 66
     if not raw_search_results:
67 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
-    for result in search_results:
71
+    if len(search_results) == 0:
72
+        return []
73
+
74
+    for result in search_results[0].get('items', []):
72 75
         # parse image results
73 76
         if result.get('image'):
74 77
             # append result
@@ -88,7 +91,7 @@ def response(resp):
88 91
                             'content': result['description'],
89 92
                             'publishedDate': publishedDate})
90 93
 
91
-        #TODO parse video, audio and file results
94
+        # TODO parse video, audio and file results
92 95
 
93 96
     # return results
94 97
     return results

+ 96
- 0
searx/tests/engines/test_yacy.py 查看文件

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

+ 2
- 1
searx/tests/test_engines.py 查看文件

@@ -30,6 +30,7 @@ from searx.tests.engines.test_subtitleseeker import *  # noqa
30 30
 from searx.tests.engines.test_twitter import *  # noqa
31 31
 from searx.tests.engines.test_vimeo import *  # noqa
32 32
 from 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
34 34
 from searx.tests.engines.test_yahoo import *  # noqa
35
+from searx.tests.engines.test_youtube import *  # noqa
35 36
 from searx.tests.engines.test_yahoo_news import *  # noqa