on master help sign in
Faroo's unit test
commitfb04f76698f8fd7347aa54a42e99ecb7ea5f7df0 authorCqoicebordel <Cqoicebordel@users.noreply.github.com> dateSat 7 Feb 2015, 15:35 (12 years ago) parentd0a1df8
3 files changed+120−5
searx/engines/faroo.py+3−5 searx/tests/engines/test_faroo.py+116 searx/tests/test_engines.py+1
searx/engines/faroo.py+3−5
@@ -37,7 +37,7 @@ search_category = {'general': 'web',
3737
3838# do search-request
3939def request(query, params):
40− offset = (params['pageno']-1) * number_of_results + 1
40+ offset = (params['pageno'] - 1) * number_of_results + 1
4141 categorie = search_category.get(params['category'], 'web')
4242
4343 if params['language'] == 'all':
@@ -45,11 +45,11 @@ def request(query, params):
4545 else:
4646 language = params['language'].split('_')[0]
4747
48− # skip, if language is not supported
48+ # if language is not supported, put it in english
4949 if language != 'en' and\
5050 language != 'de' and\
5151 language != 'zh':
52− return params
52+ language = 'en'
5353
5454 params['url'] = search_url.format(offset=offset,
5555 number_of_results=number_of_results,
@@ -69,12 +69,10 @@ def response(resp):
6969 # HTTP-Code 401: api-key is not valide
7070 if resp.status_code == 401:
7171 raise Exception("API key is not valide")
72− return []
7372
7473 # HTTP-Code 429: rate limit exceeded
7574 if resp.status_code == 429:
7675 raise Exception("rate limit has been exceeded!")
77− return []
7876
7977 results = []
8078
searx/tests/engines/test_faroo.pynew file+116
@@ -0,0 +1,116 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
3+import mock
4+from searx.engines import faroo
5+from searx.testing import SearxTestCase
6+
7+
8+class TestFarooEngine(SearxTestCase):
9+
10+ def test_request(self):
11+ query = 'test_query'
12+ dicto = defaultdict(dict)
13+ dicto['pageno'] = 1
14+ dicto['language'] = 'fr_FR'
15+ dicto['category'] = 'general'
16+ params = faroo.request(query, dicto)
17+ self.assertIn('url', params)
18+ self.assertIn(query, params['url'])
19+ self.assertIn('faroo.com', params['url'])
20+ self.assertIn('en', params['url'])
21+ self.assertIn('web', params['url'])
22+
23+ dicto['language'] = 'all'
24+ params = faroo.request(query, dicto)
25+ self.assertIn('en', params['url'])
26+
27+ dicto['language'] = 'de_DE'
28+ params = faroo.request(query, dicto)
29+ self.assertIn('de', params['url'])
30+
31+ def test_response(self):
32+ self.assertRaises(AttributeError, faroo.response, None)
33+ self.assertRaises(AttributeError, faroo.response, [])
34+ self.assertRaises(AttributeError, faroo.response, '')
35+ self.assertRaises(AttributeError, faroo.response, '[]')
36+
37+ response = mock.Mock(text='{}')
38+ self.assertEqual(faroo.response(response), [])
39+
40+ response = mock.Mock(text='{"data": []}')
41+ self.assertEqual(faroo.response(response), [])
42+
43+ response = mock.Mock(text='{"data": []}', status_code=401)
44+ self.assertRaises(Exception, faroo.response, response)
45+
46+ response = mock.Mock(text='{"data": []}', status_code=429)
47+ self.assertRaises(Exception, faroo.response, response)
48+
49+ json = """
50+ {
51+ "results": [
52+ {
53+ "title": "This is the title",
54+ "kwic": "This is the content",
55+ "content": "",
56+ "url": "http://this.is.the.url/",
57+ "iurl": "",
58+ "domain": "css3test.com",
59+ "author": "Jim Dalrymple",
60+ "news": true,
61+ "votes": "10",
62+ "date": 1360622563000,
63+ "related": []
64+ },
65+ {
66+ "title": "This is the title2",
67+ "kwic": "This is the content2",
68+ "content": "",
69+ "url": "http://this.is.the.url2/",
70+ "iurl": "",
71+ "domain": "css3test.com",
72+ "author": "Jim Dalrymple",
73+ "news": false,
74+ "votes": "10",
75+ "related": []
76+ },
77+ {
78+ "title": "This is the title3",
79+ "kwic": "This is the content3",
80+ "content": "",
81+ "url": "http://this.is.the.url3/",
82+ "iurl": "http://upload.wikimedia.org/optimized.jpg",
83+ "domain": "css3test.com",
84+ "author": "Jim Dalrymple",
85+ "news": false,
86+ "votes": "10",
87+ "related": []
88+ }
89+ ],
90+ "query": "test",
91+ "suggestions": [],
92+ "count": 100,
93+ "start": 1,
94+ "length": 10,
95+ "time": "15"
96+ }
97+ """
98+ response = mock.Mock(text=json)
99+ results = faroo.response(response)
100+ self.assertEqual(type(results), list)
101+ self.assertEqual(len(results), 4)
102+ self.assertEqual(results[0]['title'], 'This is the title')
103+ self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
104+ self.assertEqual(results[0]['content'], 'This is the content')
105+ self.assertEqual(results[1]['title'], 'This is the title2')
106+ self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
107+ self.assertEqual(results[1]['content'], 'This is the content2')
108+ self.assertEqual(results[3]['img_src'], 'http://upload.wikimedia.org/optimized.jpg')
109+
110+ json = """
111+ {}
112+ """
113+ response = mock.Mock(text=json)
114+ results = faroo.response(response)
115+ self.assertEqual(type(results), list)
116+ self.assertEqual(len(results), 0)
searx/tests/test_engines.py+1
@@ -9,6 +9,7 @@ from searx.tests.engines.test_deviantart import * # noqa
99from searx.tests.engines.test_digg import * # noqa
1010from searx.tests.engines.test_duckduckgo import * # noqa
1111from searx.tests.engines.test_dummy import * # noqa
12+from searx.tests.engines.test_faroo import * # noqa
1213from searx.tests.engines.test_flickr import * # noqa
1314from searx.tests.engines.test_flickr_noapi import * # noqa
1415from searx.tests.engines.test_github import * # noqa