@@ -0,0 +1,250 @@
1+from collections import defaultdict
2+import mock
3+from searx.engines import duckduckgo_definitions
4+from searx.testing import SearxTestCase
5+
6+
7+class TestDDGDefinitionsEngine(SearxTestCase):
8+
9+ def test_result_to_text(self):
10+ url = ''
11+ text = 'Text'
12+ html_result = 'Html'
13+ result = duckduckgo_definitions.result_to_text(url, text, html_result)
14+ self.assertEqual(result, text)
15+
16+ html_result = '<a href="url">Text in link</a>'
17+ result = duckduckgo_definitions.result_to_text(url, text, html_result)
18+ self.assertEqual(result, 'Text in link')
19+
20+ def test_request(self):
21+ query = 'test_query'
22+ dicto = defaultdict(dict)
23+ dicto['pageno'] = 1
24+ params = duckduckgo_definitions.request(query, dicto)
25+ self.assertIn('url', params)
26+ self.assertIn(query, params['url'])
27+ self.assertIn('duckduckgo.com', params['url'])
28+
29+ def test_response(self):
30+ self.assertRaises(AttributeError, duckduckgo_definitions.response, None)
31+ self.assertRaises(AttributeError, duckduckgo_definitions.response, [])
32+ self.assertRaises(AttributeError, duckduckgo_definitions.response, '')
33+ self.assertRaises(AttributeError, duckduckgo_definitions.response, '[]')
34+
35+ response = mock.Mock(text='{}')
36+ self.assertEqual(duckduckgo_definitions.response(response), [])
37+
38+ response = mock.Mock(text='{"data": []}')
39+ self.assertEqual(duckduckgo_definitions.response(response), [])
40+
41+ json = """
42+ {
43+ "DefinitionSource": "definition source",
44+ "Heading": "heading",
45+ "ImageWidth": 0,
46+ "RelatedTopics": [
47+ {
48+ "Result": "Top-level domains",
49+ "Icon": {
50+ "URL": "",
51+ "Height": "",
52+ "Width": ""
53+ },
54+ "FirstURL": "https://first.url",
55+ "Text": "text"
56+ },
57+ {
58+ "Topics": [
59+ {
60+ "Result": "result topic",
61+ "Icon": {
62+ "URL": "",
63+ "Height": "",
64+ "Width": ""
65+ },
66+ "FirstURL": "https://duckduckgo.com/?q=2%2F2",
67+ "Text": "result topic text"
68+ }
69+ ],
70+ "Name": "name"
71+ }
72+ ],
73+ "Entity": "Entity",
74+ "Type": "A",
75+ "Redirect": "",
76+ "DefinitionURL": "http://definition.url",
77+ "AbstractURL": "https://abstract.url",
78+ "Definition": "this is the definition",
79+ "AbstractSource": "abstract source",
80+ "Infobox": {
81+ "content": [
82+ {
83+ "data_type": "string",
84+ "value": "1999",
85+ "label": "Introduced",
86+ "wiki_order": 0
87+ }
88+ ],
89+ "meta": [
90+ {
91+ "data_type": "string",
92+ "value": ".test",
93+ "label": "article_title"
94+ }
95+ ]
96+ },
97+ "Image": "image.png",
98+ "ImageIsLogo": 0,
99+ "Abstract": "abstract",
100+ "AbstractText": "abstract text",
101+ "AnswerType": "",
102+ "ImageHeight": 0,
103+ "Results": [{
104+ "Result" : "result title",
105+ "Icon" : {
106+ "URL" : "result url",
107+ "Height" : 16,
108+ "Width" : 16
109+ },
110+ "FirstURL" : "result first url",
111+ "Text" : "result text"
112+ }
113+ ],
114+ "Answer": "answer"
115+ }
116+ """
117+ response = mock.Mock(text=json)
118+ results = duckduckgo_definitions.response(response)
119+ self.assertEqual(type(results), list)
120+ self.assertEqual(len(results), 4)
121+ self.assertEqual(results[0]['answer'], 'answer')
122+ self.assertEqual(results[1]['title'], 'heading')
123+ self.assertEqual(results[1]['url'], 'result first url')
124+ self.assertEqual(results[2]['suggestion'], 'text')
125+ self.assertEqual(results[3]['infobox'], 'heading')
126+ self.assertEqual(results[3]['id'], 'http://definition.url')
127+ self.assertEqual(results[3]['entity'], 'Entity')
128+ self.assertIn('abstract', results[3]['content'])
129+ self.assertIn('this is the definition', results[3]['content'])
130+ self.assertEqual(results[3]['img_src'], 'image.png')
131+ self.assertIn('Introduced', results[3]['attributes'][0]['label'])
132+ self.assertIn('1999', results[3]['attributes'][0]['value'])
133+ self.assertIn({'url': 'https://abstract.url', 'title': 'abstract source'}, results[3]['urls'])
134+ self.assertIn({'url': 'http://definition.url', 'title': 'definition source'}, results[3]['urls'])
135+ self.assertIn({'name': 'name', 'suggestions': ['result topic text']}, results[3]['relatedTopics'])
136+
137+ json = """
138+ {
139+ "DefinitionSource": "definition source",
140+ "Heading": "heading",
141+ "ImageWidth": 0,
142+ "RelatedTopics": [],
143+ "Entity": "Entity",
144+ "Type": "A",
145+ "Redirect": "",
146+ "DefinitionURL": "",
147+ "AbstractURL": "https://abstract.url",
148+ "Definition": "",
149+ "AbstractSource": "abstract source",
150+ "Image": "",
151+ "ImageIsLogo": 0,
152+ "Abstract": "",
153+ "AbstractText": "abstract text",
154+ "AnswerType": "",
155+ "ImageHeight": 0,
156+ "Results": [],
157+ "Answer": ""
158+ }
159+ """
160+ response = mock.Mock(text=json)
161+ results = duckduckgo_definitions.response(response)
162+ self.assertEqual(type(results), list)
163+ self.assertEqual(len(results), 1)
164+ self.assertEqual(results[0]['url'], 'https://abstract.url')
165+ self.assertEqual(results[0]['title'], 'heading')
166+ self.assertEqual(results[0]['content'], '')
167+
168+ json = """
169+ {
170+ "DefinitionSource": "definition source",
171+ "Heading": "heading",
172+ "ImageWidth": 0,
173+ "RelatedTopics": [
174+ {
175+ "Result": "Top-level domains",
176+ "Icon": {
177+ "URL": "",
178+ "Height": "",
179+ "Width": ""
180+ },
181+ "FirstURL": "https://first.url",
182+ "Text": "heading"
183+ },
184+ {
185+ "Name": "name"
186+ },
187+ {
188+ "Topics": [
189+ {
190+ "Result": "result topic",
191+ "Icon": {
192+ "URL": "",
193+ "Height": "",
194+ "Width": ""
195+ },
196+ "FirstURL": "https://duckduckgo.com/?q=2%2F2",
197+ "Text": "heading"
198+ }
199+ ],
200+ "Name": "name"
201+ }
202+ ],
203+ "Entity": "Entity",
204+ "Type": "A",
205+ "Redirect": "",
206+ "DefinitionURL": "http://definition.url",
207+ "AbstractURL": "https://abstract.url",
208+ "Definition": "this is the definition",
209+ "AbstractSource": "abstract source",
210+ "Infobox": {
211+ "meta": [
212+ {
213+ "data_type": "string",
214+ "value": ".test",
215+ "label": "article_title"
216+ }
217+ ]
218+ },
219+ "Image": "image.png",
220+ "ImageIsLogo": 0,
221+ "Abstract": "abstract",
222+ "AbstractText": "abstract text",
223+ "AnswerType": "",
224+ "ImageHeight": 0,
225+ "Results": [{
226+ "Result" : "result title",
227+ "Icon" : {
228+ "URL" : "result url",
229+ "Height" : 16,
230+ "Width" : 16
231+ },
232+ "Text" : "result text"
233+ }
234+ ],
235+ "Answer": ""
236+ }
237+ """
238+ response = mock.Mock(text=json)
239+ results = duckduckgo_definitions.response(response)
240+ self.assertEqual(type(results), list)
241+ self.assertEqual(len(results), 1)
242+ self.assertEqual(results[0]['infobox'], 'heading')
243+ self.assertEqual(results[0]['id'], 'http://definition.url')
244+ self.assertEqual(results[0]['entity'], 'Entity')
245+ self.assertIn('abstract', results[0]['content'])
246+ self.assertIn('this is the definition', results[0]['content'])
247+ self.assertEqual(results[0]['img_src'], 'image.png')
248+ self.assertIn({'url': 'https://abstract.url', 'title': 'abstract source'}, results[0]['urls'])
249+ self.assertIn({'url': 'http://definition.url', 'title': 'definition source'}, results[0]['urls'])
250+ self.assertIn({'name': 'name', 'suggestions': []}, results[0]['relatedTopics'])