test_wolframalpha_api.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.assertEqual(len(results), 1)
  142. self.assertIn("i", results[0]['answer'])
  143. # self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
  144. # self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
  145. xml = """<?xml version='1.0' encoding='UTF-8'?>
  146. <queryresult success='true'
  147. error='false'
  148. numpods='2'
  149. datatypes=''
  150. timedout=''
  151. timedoutpods=''
  152. timing='1.286'
  153. parsetiming='0.255'
  154. parsetimedout='false'
  155. recalculate=''
  156. id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
  157. host='http://www3.wolframalpha.com'
  158. server='20'
  159. related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
  160. version='2.6'>
  161. <pod title='Indefinite integral'
  162. scanner='Integral'
  163. id='IndefiniteIntegral'
  164. position='100'
  165. error='false'
  166. numsubpods='1'
  167. primary='true'>
  168. <subpod title=''>
  169. <plaintext>∫1/xxlog(x)+constant</plaintext>
  170. </subpod>
  171. <states count='1'>
  172. <state name='Step-by-step solution'
  173. input='IndefiniteIntegral__Step-by-step solution' />
  174. </states>
  175. <infos count='1'>
  176. <info text='log(x) is the natural logarithm'>
  177. <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
  178. text='Documentation'
  179. title='Mathematica' />
  180. <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
  181. text='Properties'
  182. title='Wolfram Functions Site' />
  183. <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
  184. text='Definition'
  185. title='MathWorld' />
  186. </info>
  187. </infos>
  188. </pod>
  189. <pod title='Plots of the integral'
  190. scanner='Integral'
  191. id='Plot'
  192. position='200'
  193. error='false'
  194. numsubpods='2'>
  195. <subpod title=''>
  196. <plaintext></plaintext>
  197. <states count='1'>
  198. <statelist count='2'
  199. value='Complex-valued plot'
  200. delimiters=''>
  201. <state name='Complex-valued plot'
  202. input='Plot__1_Complex-valued plot' />
  203. <state name='Real-valued plot'
  204. input='Plot__1_Real-valued plot' />
  205. </statelist>
  206. </states>
  207. </subpod>
  208. <subpod title=''>
  209. <plaintext></plaintext>
  210. <states count='1'>
  211. <statelist count='2'
  212. value='Complex-valued plot'
  213. delimiters=''>
  214. <state name='Complex-valued plot'
  215. input='Plot__2_Complex-valued plot' />
  216. <state name='Real-valued plot'
  217. input='Plot__2_Real-valued plot' />
  218. </statelist>
  219. </states>
  220. </subpod>
  221. </pod>
  222. <assumptions count='1'>
  223. <assumption type='Clash'
  224. word='integral'
  225. template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead'
  226. count='2'>
  227. <value name='IntegralsWord'
  228. desc='an integral'
  229. input='*C.integral-_*IntegralsWord-' />
  230. <value name='MathematicalFunctionIdentityPropertyClass'
  231. desc='a function property'
  232. input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
  233. </assumption>
  234. </assumptions>
  235. </queryresult>
  236. """
  237. response = mock.Mock(content=xml)
  238. results = wolframalpha_api.response(response)
  239. self.assertEqual(type(results), list)
  240. # self.assertEqual(len(results), 2)
  241. self.assertEqual(len(results), 1)
  242. self.assertIn("log(x)+c", results[0]['answer'])
  243. # self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
  244. # self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])