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