test_duckduckgo_definitions.py 8.8 KB

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