test_wolframalpha_noapi.py 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from requests import Request
  5. from searx.engines import wolframalpha_noapi
  6. from searx.testing import SearxTestCase
  7. class TestWolframAlphaNoAPIEngine(SearxTestCase):
  8. def test_request(self):
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. params = wolframalpha_noapi.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn('https://www.wolframalpha.com/input/json.jsp', params['url'])
  14. self.assertIn(query, params['url'])
  15. self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer'])
  16. def test_response(self):
  17. self.assertRaises(AttributeError, wolframalpha_noapi.response, None)
  18. self.assertRaises(AttributeError, wolframalpha_noapi.response, [])
  19. self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
  20. self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')
  21. referer_url = 'referer_url'
  22. request = Request(headers={'Referer': referer_url})
  23. # test failure
  24. json = r'''
  25. {"queryresult" : {
  26. "success" : false,
  27. "error" : false,
  28. "numpods" : 0,
  29. "id" : "",
  30. "host" : "https:\/\/www5a.wolframalpha.com",
  31. "didyoumeans" : {}
  32. }}
  33. '''
  34. response = mock.Mock(text=json, request=request)
  35. self.assertEqual(wolframalpha_noapi.response(response), [])
  36. # test basic case
  37. json = r'''
  38. {"queryresult" : {
  39. "success" : true,
  40. "error" : false,
  41. "numpods" : 6,
  42. "datatypes" : "Math",
  43. "id" : "queryresult_id",
  44. "host" : "https:\/\/www5b.wolframalpha.com",
  45. "related" : "related_url",
  46. "version" : "2.6",
  47. "pods" : [
  48. {
  49. "title" : "Input",
  50. "scanners" : [
  51. "Identity"
  52. ],
  53. "id" : "Input",
  54. "error" : false,
  55. "numsubpods" : 1,
  56. "subpods" : [
  57. {
  58. "title" : "",
  59. "img" : {
  60. "src" : "input_img_src.gif",
  61. "alt" : "input_img_alt",
  62. "title" : "input_img_title"
  63. },
  64. "plaintext" : "input_plaintext",
  65. "minput" : "input_minput"
  66. }
  67. ]
  68. },
  69. {
  70. "title" : "Result",
  71. "scanners" : [
  72. "Simplification"
  73. ],
  74. "id" : "Result",
  75. "error" : false,
  76. "numsubpods" : 1,
  77. "primary" : true,
  78. "subpods" : [
  79. {
  80. "title" : "",
  81. "img" : {
  82. "src" : "result_img_src.gif",
  83. "alt" : "result_img_alt",
  84. "title" : "result_img_title"
  85. },
  86. "plaintext" : "result_plaintext",
  87. "moutput" : "result_moutput"
  88. }
  89. ]
  90. },
  91. {
  92. "title" : "Manipulatives illustration",
  93. "scanners" : [
  94. "Arithmetic"
  95. ],
  96. "id" : "Illustration",
  97. "error" : false,
  98. "numsubpods" : 1,
  99. "subpods" : [
  100. {
  101. "title" : "",
  102. "CDFcontent" : "Resizeable",
  103. "img" : {
  104. "src" : "illustration_img_src.gif",
  105. "alt" : "illustration_img_alt",
  106. "title" : "illustration_img_title"
  107. },
  108. "plaintext" : "illustration_img_plaintext"
  109. }
  110. ]
  111. }
  112. ]
  113. }}
  114. '''
  115. response = mock.Mock(text=json, request=request)
  116. results = wolframalpha_noapi.response(response)
  117. self.assertEqual(type(results), list)
  118. self.assertEqual(len(results), 2)
  119. self.assertEqual('input_plaintext', results[0]['infobox'])
  120. self.assertEqual(len(results[0]['attributes']), 3)
  121. self.assertEqual('Input', results[0]['attributes'][0]['label'])
  122. self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
  123. self.assertEqual('Result', results[0]['attributes'][1]['label'])
  124. self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
  125. self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
  126. self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
  127. self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
  128. self.assertEqual(len(results[0]['urls']), 1)
  129. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  130. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  131. self.assertEqual(referer_url, results[1]['url'])
  132. self.assertEqual('Wolfram|Alpha', results[1]['title'])
  133. # test calc
  134. json = r"""
  135. {"queryresult" : {
  136. "success" : true,
  137. "error" : false,
  138. "numpods" : 2,
  139. "datatypes" : "",
  140. "id" : "queryresult_id",
  141. "host" : "https:\/\/www4b.wolframalpha.com",
  142. "related" : "related_url",
  143. "version" : "2.6",
  144. "pods" : [
  145. {
  146. "title" : "Indefinite integral",
  147. "scanners" : [
  148. "Integral"
  149. ],
  150. "id" : "IndefiniteIntegral",
  151. "error" : false,
  152. "numsubpods" : 1,
  153. "primary" : true,
  154. "subpods" : [
  155. {
  156. "title" : "",
  157. "img" : {
  158. "src" : "integral_img_src.gif",
  159. "alt" : "integral_img_alt",
  160. "title" : "integral_img_title"
  161. },
  162. "plaintext" : "integral_plaintext",
  163. "minput" : "integral_minput",
  164. "moutput" : "integral_moutput"
  165. }
  166. ]
  167. },
  168. {
  169. "title" : "Plot of the integral",
  170. "scanners" : [
  171. "Integral"
  172. ],
  173. "id" : "Plot",
  174. "error" : false,
  175. "numsubpods" : 1,
  176. "subpods" : [
  177. {
  178. "title" : "",
  179. "img" : {
  180. "src" : "plot.gif",
  181. "alt" : "plot_alt",
  182. "title" : "plot_title"
  183. },
  184. "plaintext" : "",
  185. "minput" : "plot_minput"
  186. }
  187. ]
  188. }
  189. ]
  190. }}
  191. """
  192. response = mock.Mock(text=json, request=request)
  193. results = wolframalpha_noapi.response(response)
  194. self.assertEqual(type(results), list)
  195. self.assertEqual(len(results), 2)
  196. self.assertEqual('integral_plaintext', results[0]['infobox'])
  197. self.assertEqual(len(results[0]['attributes']), 2)
  198. self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
  199. self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
  200. self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
  201. self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
  202. self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
  203. self.assertEqual(len(results[0]['urls']), 1)
  204. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  205. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  206. self.assertEqual(referer_url, results[1]['url'])
  207. self.assertEqual('Wolfram|Alpha', results[1]['title'])