test_wolframalpha_api.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import wolframalpha_api
  5. from searx.testing import SearxTestCase
  6. class TestWolframAlphaAPIEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. api_key = 'XXXXXX-XXXXXXXXXX'
  10. dicto = defaultdict(dict)
  11. dicto['api_key'] = api_key
  12. params = wolframalpha_api.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn(query, params['url'])
  15. self.assertIn('wolframalpha.com', params['url'])
  16. self.assertIn('api_key', params)
  17. self.assertIn(api_key, params['api_key'])
  18. def test_response(self):
  19. self.assertRaises(AttributeError, wolframalpha_api.response, None)
  20. self.assertRaises(AttributeError, wolframalpha_api.response, [])
  21. self.assertRaises(AttributeError, wolframalpha_api.response, '')
  22. self.assertRaises(AttributeError, wolframalpha_api.response, '[]')
  23. xml = '''<?xml version='1.0' encoding='UTF-8'?>
  24. <queryresult success='false' error='false' />
  25. '''
  26. response = mock.Mock(content=xml)
  27. self.assertEqual(wolframalpha_api.response(response), [])
  28. xml = """<?xml version='1.0' encoding='UTF-8'?>
  29. <queryresult success='false'
  30. error='false'
  31. numpods='0'
  32. datatypes=''
  33. timedout=''
  34. timedoutpods=''
  35. timing='0.241'
  36. parsetiming='0.074'
  37. parsetimedout='false'
  38. recalculate=''
  39. id=''
  40. host='http://www5a.wolframalpha.com'
  41. server='56'
  42. related=''
  43. version='2.6'>
  44. <tips count='1'>
  45. <tip text='Check your spelling, and use English' />
  46. </tips>
  47. </queryresult>
  48. """
  49. response = mock.Mock(content=xml)
  50. self.assertEqual(wolframalpha_api.response(response), [])
  51. xml = """<?xml version='1.0' encoding='UTF-8'?>
  52. <queryresult success='true'
  53. error='false'
  54. numpods='6'
  55. datatypes=''
  56. timedout=''
  57. timedoutpods=''
  58. timing='0.684'
  59. parsetiming='0.138'
  60. parsetimedout='false'
  61. recalculate=''
  62. id='MSPa416020a7966dachc463600000f9c66cc21444cfg'
  63. host='http://www3.wolframalpha.com'
  64. server='6'
  65. related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...'
  66. version='2.6'>
  67. <pod title='Input'
  68. scanner='Identity'
  69. id='Input'
  70. position='100'
  71. error='false'
  72. numsubpods='1'>
  73. <subpod title=''>
  74. <plaintext>sqrt(-1)</plaintext>
  75. </subpod>
  76. </pod>
  77. <pod title='Result'
  78. scanner='Simplification'
  79. id='Result'
  80. position='200'
  81. error='false'
  82. numsubpods='1'
  83. primary='true'>
  84. <subpod title=''>
  85. <plaintext></plaintext>
  86. </subpod>
  87. <states count='1'>
  88. <state name='Step-by-step solution'
  89. input='Result__Step-by-step solution' />
  90. </states>
  91. </pod>
  92. <pod title='Polar coordinates'
  93. scanner='Numeric'
  94. id='PolarCoordinates'
  95. position='300'
  96. error='false'
  97. numsubpods='1'>
  98. <subpod title=''>
  99. <plaintext>r1 (radius), θ90° (angle)</plaintext>
  100. </subpod>
  101. </pod>
  102. <pod title='Position in the complex plane'
  103. scanner='Numeric'
  104. id='PositionInTheComplexPlane'
  105. position='400'
  106. error='false'
  107. numsubpods='1'>
  108. <subpod title=''>
  109. <plaintext></plaintext>
  110. </subpod>
  111. </pod>
  112. <pod title='All 2nd roots of -1'
  113. scanner='RootsOfUnity'
  114. id=''
  115. position='500'
  116. error='false'
  117. numsubpods='2'>
  118. <subpod title=''>
  119. <plaintext> (principal root)</plaintext>
  120. </subpod>
  121. <subpod title=''>
  122. <plaintext>-</plaintext>
  123. </subpod>
  124. </pod>
  125. <pod title='Plot of all roots in the complex plane'
  126. scanner='RootsOfUnity'
  127. id='PlotOfAllRootsInTheComplexPlane'
  128. position='600'
  129. error='false'
  130. numsubpods='1'>
  131. <subpod title=''>
  132. <plaintext></plaintext>
  133. </subpod>
  134. </pod>
  135. </queryresult>
  136. """
  137. response = mock.Mock(content=xml)
  138. results = wolframalpha_api.response(response)
  139. self.assertEqual(type(results), list)
  140. self.assertEqual(len(results), 2)
  141. self.assertIn("i", results[0]['answer'])
  142. # self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
  143. # self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
  144. xml = """<?xml version='1.0' encoding='UTF-8'?>
  145. <queryresult success='true'
  146. error='false'
  147. numpods='2'
  148. datatypes=''
  149. timedout=''
  150. timedoutpods=''
  151. timing='1.286'
  152. parsetiming='0.255'
  153. parsetimedout='false'
  154. recalculate=''
  155. id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
  156. host='http://www3.wolframalpha.com'
  157. server='20'
  158. related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
  159. version='2.6'>
  160. <pod title='Indefinite integral'
  161. scanner='Integral'
  162. id='IndefiniteIntegral'
  163. position='100'
  164. error='false'
  165. numsubpods='1'
  166. primary='true'>
  167. <subpod title=''>
  168. <plaintext>∫1/xxlog(x)+constant</plaintext>
  169. </subpod>
  170. <states count='1'>
  171. <state name='Step-by-step solution'
  172. input='IndefiniteIntegral__Step-by-step solution' />
  173. </states>
  174. <infos count='1'>
  175. <info text='log(x) is the natural logarithm'>
  176. <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
  177. text='Documentation'
  178. title='Mathematica' />
  179. <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
  180. text='Properties'
  181. title='Wolfram Functions Site' />
  182. <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
  183. text='Definition'
  184. title='MathWorld' />
  185. </info>
  186. </infos>
  187. </pod>
  188. <pod title='Plots of the integral'
  189. scanner='Integral'
  190. id='Plot'
  191. position='200'
  192. error='false'
  193. numsubpods='2'>
  194. <subpod title=''>
  195. <plaintext></plaintext>
  196. <states count='1'>
  197. <statelist count='2'
  198. value='Complex-valued plot'
  199. delimiters=''>
  200. <state name='Complex-valued plot'
  201. input='Plot__1_Complex-valued plot' />
  202. <state name='Real-valued plot'
  203. input='Plot__1_Real-valued plot' />
  204. </statelist>
  205. </states>
  206. </subpod>
  207. <subpod title=''>
  208. <plaintext></plaintext>
  209. <states count='1'>
  210. <statelist count='2'
  211. value='Complex-valued plot'
  212. delimiters=''>
  213. <state name='Complex-valued plot'
  214. input='Plot__2_Complex-valued plot' />
  215. <state name='Real-valued plot'
  216. input='Plot__2_Real-valued plot' />
  217. </statelist>
  218. </states>
  219. </subpod>
  220. </pod>
  221. <assumptions count='1'>
  222. <assumption type='Clash'
  223. word='integral'
  224. template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead'
  225. count='2'>
  226. <value name='IntegralsWord'
  227. desc='an integral'
  228. input='*C.integral-_*IntegralsWord-' />
  229. <value name='MathematicalFunctionIdentityPropertyClass'
  230. desc='a function property'
  231. input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
  232. </assumption>
  233. </assumptions>
  234. </queryresult>
  235. """
  236. response = mock.Mock(content=xml)
  237. results = wolframalpha_api.response(response)
  238. self.assertEqual(type(results), list)
  239. self.assertEqual(len(results), 2)
  240. self.assertIn("log(x)+c", results[0]['answer'])
  241. # self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
  242. # self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])