logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_duckduckgo_definitions.py (9068B)


  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'])