|
@@ -0,0 +1,72 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+from collections import defaultdict
|
|
3
|
+import mock
|
|
4
|
+from searx.engines import duckduckgo_images
|
|
5
|
+from searx.testing import SearxTestCase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+class TestDuckduckgoImagesEngine(SearxTestCase):
|
|
9
|
+
|
|
10
|
+ def test_request(self):
|
|
11
|
+ query = 'test_query'
|
|
12
|
+ dicto = defaultdict(dict)
|
|
13
|
+ dicto['is_test'] = True
|
|
14
|
+ dicto['pageno'] = 1
|
|
15
|
+ dicto['safesearch'] = 0
|
|
16
|
+ dicto['language'] = 'all'
|
|
17
|
+ params = duckduckgo_images.request(query, dicto)
|
|
18
|
+ self.assertIn('url', params)
|
|
19
|
+ self.assertIn(query, params['url'])
|
|
20
|
+ self.assertIn('duckduckgo.com', params['url'])
|
|
21
|
+ self.assertIn('s=0', params['url'])
|
|
22
|
+ self.assertIn('p=-1', params['url'])
|
|
23
|
+ self.assertIn('vqd=12345', params['url'])
|
|
24
|
+
|
|
25
|
+ # test paging and safe search
|
|
26
|
+ dicto['pageno'] = 2
|
|
27
|
+ dicto['safesearch'] = 2
|
|
28
|
+ params = duckduckgo_images.request(query, dicto)
|
|
29
|
+ self.assertIn('url', params)
|
|
30
|
+ self.assertIn(query, params['url'])
|
|
31
|
+ self.assertIn('s=50', params['url'])
|
|
32
|
+ self.assertIn('p=1', params['url'])
|
|
33
|
+
|
|
34
|
+ def test_response(self):
|
|
35
|
+ self.assertRaises(AttributeError, duckduckgo_images.response, None)
|
|
36
|
+ self.assertRaises(AttributeError, duckduckgo_images.response, [])
|
|
37
|
+ self.assertRaises(AttributeError, duckduckgo_images.response, '')
|
|
38
|
+ self.assertRaises(AttributeError, duckduckgo_images.response, '[]')
|
|
39
|
+
|
|
40
|
+ response = mock.Mock(text='If this error persists, please let us know: ops@duckduckgo.com')
|
|
41
|
+ self.assertEqual(duckduckgo_images.response(response), [])
|
|
42
|
+
|
|
43
|
+ json = u"""
|
|
44
|
+ {
|
|
45
|
+ "query": "test_query",
|
|
46
|
+ "results": [
|
|
47
|
+ {
|
|
48
|
+ "title": "Result 1",
|
|
49
|
+ "url": "https://site1.url",
|
|
50
|
+ "thumbnail": "https://thumb1.nail",
|
|
51
|
+ "image": "https://image1"
|
|
52
|
+ },
|
|
53
|
+ {
|
|
54
|
+ "title": "Result 2",
|
|
55
|
+ "url": "https://site2.url",
|
|
56
|
+ "thumbnail": "https://thumb2.nail",
|
|
57
|
+ "image": "https://image2"
|
|
58
|
+ }
|
|
59
|
+ ]
|
|
60
|
+ }
|
|
61
|
+ """
|
|
62
|
+ response = mock.Mock(text=json)
|
|
63
|
+ results = duckduckgo_images.response(response)
|
|
64
|
+ self.assertEqual(len(results), 2)
|
|
65
|
+ self.assertEqual(results[0]['title'], 'Result 1')
|
|
66
|
+ self.assertEqual(results[0]['url'], 'https://site1.url')
|
|
67
|
+ self.assertEqual(results[0]['thumbnail_src'], 'https://thumb1.nail')
|
|
68
|
+ self.assertEqual(results[0]['img_src'], 'https://image1')
|
|
69
|
+ self.assertEqual(results[1]['title'], 'Result 2')
|
|
70
|
+ self.assertEqual(results[1]['url'], 'https://site2.url')
|
|
71
|
+ self.assertEqual(results[1]['thumbnail_src'], 'https://thumb2.nail')
|
|
72
|
+ self.assertEqual(results[1]['img_src'], 'https://image2')
|