test_duckduckgo_definitions.py 8.9KB

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