test_duckduckgo_definitions.py 8.6KB

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