test_wolframalpha_noapi.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import wolframalpha_noapi
  5. from searx.testing import SearxTestCase
  6. class TestWolframAlphaNoAPIEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = wolframalpha_noapi.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('wolframalpha.com', params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, wolframalpha_noapi.response, None)
  17. self.assertRaises(AttributeError, wolframalpha_noapi.response, [])
  18. self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
  19. self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')
  20. html = """
  21. <!DOCTYPE html>
  22. <title> Parangaricutirimícuaro - Wolfram|Alpha</title>
  23. <meta charset="utf-8" />
  24. <body>
  25. <div id="closest">
  26. <p class="pfail">Wolfram|Alpha doesn't know how to interpret your input.</p>
  27. <div id="dtips">
  28. <div class="tip">
  29. <span class="tip-title">Tip:&nbsp;</span>
  30. Check your spelling, and use English
  31. <span class="tip-extra"></span>
  32. </div>
  33. </div>
  34. </div>
  35. </body>
  36. </html>
  37. """
  38. response = mock.Mock(text=html)
  39. self.assertEqual(wolframalpha_noapi.response(response), [])
  40. html = """
  41. <!DOCTYPE html>
  42. <title> sqrt(-1) - Wolfram|Alpha</title>
  43. <meta charset="utf-8" />
  44. <body>
  45. <script type="text/javascript">
  46. try {
  47. document.domain = "wolframalpha.com";
  48. context = parent ? parent : document;
  49. } catch(e){}
  50. try {
  51. if (typeof(context.$) == "undefined") {
  52. context = window;
  53. } else {
  54. $=context.$;
  55. }
  56. }
  57. catch(e){ context = window;}
  58. try {
  59. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  60. context.jsonArray.popups.pod_0100 = [];
  61. }
  62. context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": "", "popLinks": {} });
  63. } catch(e) { }
  64. try {
  65. $("#results #pod_0100:not(iframe #pod_0100)")
  66. .add("#showsteps #pod_0100:not(iframe #pod_0100)")
  67. .add(".results-pod #pod_0100:not(iframe #pod_0100)")
  68. .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
  69. .data("podIdentifier", '\x22Input\x22')
  70. .data("podShortIdentifier", '\x22Input\x22')
  71. .data("buttonStates", '\x22\x22')
  72. .data("scanner", '\x22\x22');
  73. $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
  74. .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
  75. .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
  76. .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
  77. .data("podIdentifier", '\x22Input\x22')
  78. .data("podShortIdentifier", '\x22Input\x22')
  79. .data("buttonStates", '\x22\x22')
  80. .data("scanner", '\x22\x22');
  81. $("#results #subpod_0100_1")
  82. .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
  83. .add(".results-pod #subpod_0100_1")
  84. .data("tempFileID", "MSP44511e0dda34g97a0c89000059490h319161eea3")
  85. .data("cellDataTempFile", "MSP44521e0dda34g97a0c89000011378c50d38ede6h")
  86. .data("tempFileServer", "")
  87. .data("dataSources", "")
  88. .data("sources", "")
  89. .data("sharetype", "1")
  90. .data("shareable", "false");
  91. } catch(e){}
  92. //false
  93. try {
  94. if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
  95. context.jsonArray.popups.pod_0200 = [];
  96. }
  97. context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": "", "popLinks": {} });
  98. } catch(e) { }
  99. try {
  100. $("#results #pod_0200:not(iframe #pod_0200)")
  101. .add("#showsteps #pod_0200:not(iframe #pod_0200)")
  102. .add(".results-pod #pod_0200:not(iframe #pod_0200)")
  103. .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
  104. .data("podIdentifier", '\x22Result\x22')
  105. .data("podShortIdentifier", '\x22Result\x22')
  106. .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None,\x20None,\x20None\x7D')
  107. .data("scanner", '\x22\x22');
  108. $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
  109. .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
  110. .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
  111. .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
  112. .data("podIdentifier", '\x22Result\x22')
  113. .data("podShortIdentifier", '\x22Result\x22')
  114. .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None\x7D')
  115. .data("scanner", '\x22\x22');
  116. $("#results #subpod_0200_1")
  117. .add("#showsteps #subpod_0200_1:not(iframe #subpod_0200_1)")
  118. .add(".results-pod #subpod_0200_1")
  119. .data("tempFileID", "MSP44551e0dda34g97a0c8900003gdgd37faa7272e0")
  120. .data("cellDataTempFile", "MSP44561e0dda34g97a0c89000018ea1iae00104g13")
  121. .data("tempFileServer", "")
  122. .data("dataSources", "")
  123. .data("sources", "")
  124. .data("sharetype", "1")
  125. .data("shareable", "false");
  126. } catch(e){}
  127. </script>
  128. </body>
  129. </html>
  130. """
  131. response = mock.Mock(text=html)
  132. results = wolframalpha_noapi.response(response)
  133. self.assertEqual(type(results), list)
  134. self.assertEqual(len(results), 2)
  135. self.assertIn("i", results[0]['answer'])
  136. self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
  137. self.assertIn("http://www.wolframalpha.com/input/?i=+sqrt%28-1%29", results[1]['url'])
  138. html = """
  139. <!DOCTYPE html>
  140. <title> integral 1/x - Wolfram|Alpha</title>
  141. <meta charset="utf-8" />
  142. <body>
  143. <script type="text/javascript">
  144. //true
  145. try {
  146. document.domain = "wolframalpha.com";
  147. context = parent ? parent : document;
  148. } catch(e){}
  149. try {
  150. if (typeof(context.$) == "undefined") {
  151. context = window;
  152. } else {
  153. $=context.$;
  154. }
  155. }
  156. catch(e){ context = window;}
  157. try {
  158. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  159. context.jsonArray.popups.pod_0100 = [];
  160. }
  161. context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
  162. } catch(e) { }
  163. try {
  164. $("#results #pod_0100:not(iframe #pod_0100)")
  165. .add("#showsteps #pod_0100:not(iframe #pod_0100)")
  166. .add(".results-pod #pod_0100:not(iframe #pod_0100)")
  167. .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
  168. .data("podIdentifier", '\x22IndefiniteIntegral\x22')
  169. .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
  170. .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
  171. .data("scanner", '\x22\x22');
  172. $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
  173. .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
  174. .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
  175. .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
  176. .data("podIdentifier", '\x22IndefiniteIntegral\x22')
  177. .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
  178. .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
  179. .data("scanner", '\x22\x22');
  180. $("#results #subpod_0100_1")
  181. .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
  182. .add(".results-pod #subpod_0100_1")
  183. .data("tempFileID", "MSP2071if2202e8bg0757100004dg60f2a4ca8cf73")
  184. .data("cellDataTempFile", "MSP2081if2202e8bg0757100001h18329f72fe90fg")
  185. .data("tempFileServer", "")
  186. .data("dataSources", "")
  187. .data("sources", "")
  188. .data("sharetype", "1")
  189. .data("shareable", "false");
  190. } catch(e){}
  191. //false
  192. try {
  193. $("#results #pod_0200:not(iframe #pod_0200)")
  194. .add("#showsteps #pod_0200:not(iframe #pod_0200)")
  195. .add(".results-pod #pod_0200:not(iframe #pod_0200)")
  196. .data("tempFileID", '')
  197. .data("podIdentifier", '\x22Plot\x22')
  198. .data("podShortIdentifier", '')
  199. .data("buttonStates", '')
  200. .data("scanner", '\x22\x22');
  201. $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
  202. .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
  203. .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
  204. .data("tempFileID", '')
  205. .data("podIdentifier", '\x22Plot\x22')
  206. .data("podShortIdentifier", '')
  207. .data("buttonStates", '')
  208. .data("scanner", '\x22\x22');
  209. } catch(e){}
  210. </script>
  211. </body>
  212. </html>
  213. """
  214. response = mock.Mock(text=html)
  215. results = wolframalpha_noapi.response(response)
  216. self.assertEqual(type(results), list)
  217. self.assertEqual(len(results), 2)
  218. self.assertIn("log(x)+c", results[0]['answer'])
  219. self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
  220. self.assertIn("http://www.wolframalpha.com/input/?i=+integral+1%2Fx", results[1]['url'])