test_wolframalpha_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.826'
  59. parsetiming='0.17'
  60. parsetimedout='false'
  61. recalculate=''
  62. id='MSPa9721hfe10fii5idac02000029c3a6f09608410h'
  63. host='http://www4c.wolframalpha.com'
  64. server='53'
  65. related='http://www4c.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa9731h927ig0h6b1&amp;s=53'
  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. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP974111ig68hc?MSPStoreType=image/gif&amp;s=53'
  75. alt='sqrt(-1)'
  76. title='sqrt(-1)'
  77. width='36'
  78. height='20' />
  79. <plaintext>sqrt(-1)</plaintext>
  80. </subpod>
  81. </pod>
  82. <pod title='Result'
  83. scanner='Simplification'
  84. id='Result'
  85. position='200'
  86. error='false'
  87. numsubpods='1'
  88. primary='true'>
  89. <subpod title=''>
  90. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9751hfe101fc27?MSPStoreType=image/gif&amp;s=53'
  91. alt='i'
  92. title='i'
  93. width='5'
  94. height='18' />
  95. <plaintext>i</plaintext>
  96. </subpod>
  97. <states count='1'>
  98. <state name='Step-by-step solution'
  99. input='Result__Step-by-step solution' />
  100. </states>
  101. </pod>
  102. <pod title='Polar coordinates'
  103. scanner='Numeric'
  104. id='PolarCoordinates'
  105. position='300'
  106. error='false'
  107. numsubpods='1'>
  108. <subpod title=''>
  109. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP97600003i83?MSPStoreType=image/gif&amp;s=53'
  110. alt='r = 1 (radius), theta = 90° (angle)'
  111. title='r = 1 (radius), theta = 90° (angle)'
  112. width='209'
  113. height='18' />
  114. <plaintext>r = 1 (radius), theta = 90° (angle)</plaintext>
  115. </subpod>
  116. </pod>
  117. <pod title='Position in the complex plane'
  118. scanner='Numeric'
  119. id='PositionInTheComplexPlane'
  120. position='400'
  121. error='false'
  122. numsubpods='1'>
  123. <subpod title=''>
  124. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9771e10ficg4g?MSPStoreType=image/gif&amp;s=53'
  125. alt=''
  126. title=''
  127. width='200'
  128. height='185' />
  129. <plaintext></plaintext>
  130. </subpod>
  131. </pod>
  132. <pod title='All 2nd roots of -1'
  133. scanner='RootsOfUnity'
  134. id=''
  135. position='500'
  136. error='false'
  137. numsubpods='2'>
  138. <subpod title=''>
  139. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9781hfe10fii?MSPStoreType=image/gif&amp;s=53'
  140. alt='i (principal root)'
  141. title='i (principal root)'
  142. width='94'
  143. height='18' />
  144. <plaintext>i (principal root)</plaintext>
  145. </subpod>
  146. <subpod title=''>
  147. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9791hfe16f2eh1?MSPStoreType=image/gif&amp;s=53'
  148. alt='-i'
  149. title='-i'
  150. width='16'
  151. height='18' />
  152. <plaintext>-i</plaintext>
  153. </subpod>
  154. </pod>
  155. <pod title='Plot of all roots in the complex plane'
  156. scanner='RootsOfUnity'
  157. id='PlotOfAllRootsInTheComplexPlane'
  158. position='600'
  159. error='false'
  160. numsubpods='1'>
  161. <subpod title=''>
  162. <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9801h0fi192f9?MSPStoreType=image/gif&amp;s=53'
  163. alt=''
  164. title=''
  165. width='200'
  166. height='185' />
  167. <plaintext></plaintext>
  168. </subpod>
  169. </pod>
  170. </queryresult>
  171. """
  172. response = mock.Mock(content=xml)
  173. results = wolframalpha_api.response(response)
  174. self.assertEqual(type(results), list)
  175. self.assertEqual(len(results), 1)
  176. self.assertIn("i", results[0]['answer'])
  177. xml = """<?xml version='1.0' encoding='UTF-8'?>
  178. <queryresult success='true'
  179. error='false'
  180. numpods='2'
  181. datatypes=''
  182. timedout='Integral'
  183. timedoutpods=''
  184. timing='1.245'
  185. parsetiming='0.194'
  186. parsetimedout='false'
  187. recalculate='http://www4b.wolframalpha.com/api/v2/recalc.jsp?id=MSPa77651gf1a1hie0ii051ea0e1c&amp;s=3'
  188. id='MSPa77661gf1a1hie5c9d9a600003baifafc1211daef'
  189. host='http://www4b.wolframalpha.com'
  190. server='3'
  191. related='http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa77671gf1a1hie5c5hc2&amp;s=3'
  192. version='2.6'>
  193. <pod title='Indefinite integral'
  194. scanner='Integral'
  195. id='IndefiniteIntegral'
  196. position='100'
  197. error='false'
  198. numsubpods='1'
  199. primary='true'>
  200. <subpod title=''>
  201. <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP776814b9492i9a7gb16?MSPStoreType=image/gif&amp;s=3'
  202. alt=' integral 1/x dx = log(x)+constant'
  203. title=' integral 1/x dx = log(x)+constant'
  204. width='182'
  205. height='36' />
  206. <plaintext> integral 1/x dx = log(x)+constant</plaintext>
  207. </subpod>
  208. <states count='1'>
  209. <state name='Step-by-step solution'
  210. input='IndefiniteIntegral__Step-by-step solution' />
  211. </states>
  212. <infos count='1'>
  213. <info text='log(x) is the natural logarithm'>
  214. <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77691g23eg440g89db?MSPStoreType=image/gif&amp;s=3'
  215. alt='log(x) is the natural logarithm'
  216. title='log(x) is the natural logarithm'
  217. width='198'
  218. height='18' />
  219. <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
  220. text='Documentation'
  221. title='Mathematica' />
  222. <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
  223. text='Properties'
  224. title='Wolfram Functions Site' />
  225. <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
  226. text='Definition'
  227. title='MathWorld' />
  228. </info>
  229. </infos>
  230. </pod>
  231. <pod title='Plots of the integral'
  232. scanner='Integral'
  233. id='Plot'
  234. position='200'
  235. error='false'
  236. numsubpods='2'>
  237. <subpod title=''>
  238. <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77701gf1a9d2eb630g9?MSPStoreType=image/gif&amp;s=3'
  239. alt=''
  240. title=''
  241. width='334'
  242. height='128' />
  243. <plaintext></plaintext>
  244. <states count='1'>
  245. <statelist count='2'
  246. value='Complex-valued plot'
  247. delimiters=''>
  248. <state name='Complex-valued plot'
  249. input='Plot__1_Complex-valued plot' />
  250. <state name='Real-valued plot'
  251. input='Plot__1_Real-valued plot' />
  252. </statelist>
  253. </states>
  254. </subpod>
  255. <subpod title=''>
  256. <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77711gf1ai29a34b0ab?MSPStoreType=image/gif&amp;s=3'
  257. alt=''
  258. title=''
  259. width='334'
  260. height='133' />
  261. <plaintext></plaintext>
  262. <states count='1'>
  263. <statelist count='2'
  264. value='Complex-valued plot'
  265. delimiters=''>
  266. <state name='Complex-valued plot'
  267. input='Plot__2_Complex-valued plot' />
  268. <state name='Real-valued plot'
  269. input='Plot__2_Real-valued plot' />
  270. </statelist>
  271. </states>
  272. </subpod>
  273. </pod>
  274. </queryresult>
  275. """
  276. response = mock.Mock(content=xml)
  277. results = wolframalpha_api.response(response)
  278. self.assertEqual(type(results), list)
  279. self.assertEqual(len(results), 1)
  280. self.assertIn("log(x)+c", results[0]['answer'])