test_wolframalpha_noapi.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. # response = mock.Mock(text='<html></html>')
  21. # self.assertEqual(wolframalpha_noapi.response(response), [])
  22. html = """
  23. <!DOCTYPE html>
  24. <title> sqrt(-1) - Wolfram|Alpha</title>
  25. <meta charset="utf-8" />
  26. <body>
  27. <script type="text/javascript">
  28. try {
  29. document.domain = "wolframalpha.com";
  30. context = parent ? parent : document;
  31. } catch(e){}
  32. try {
  33. if (typeof(context.$) == "undefined") {
  34. context = window;
  35. } else {
  36. $=context.$;
  37. }
  38. }
  39. catch(e){ context = window;}
  40. try {
  41. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  42. context.jsonArray.popups.pod_0100 = [];
  43. }
  44. context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": "", "popLinks": {} });
  45. } catch(e) { }
  46. try {
  47. $("#results #pod_0100:not(iframe #pod_0100)")
  48. .add("#showsteps #pod_0100:not(iframe #pod_0100)")
  49. .add(".results-pod #pod_0100:not(iframe #pod_0100)")
  50. .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
  51. .data("podIdentifier", '\x22Input\x22')
  52. .data("podShortIdentifier", '\x22Input\x22')
  53. .data("buttonStates", '\x22\x22')
  54. .data("scanner", '\x22\x22');
  55. $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
  56. .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
  57. .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
  58. .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
  59. .data("podIdentifier", '\x22Input\x22')
  60. .data("podShortIdentifier", '\x22Input\x22')
  61. .data("buttonStates", '\x22\x22')
  62. .data("scanner", '\x22\x22');
  63. $("#results #subpod_0100_1")
  64. .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
  65. .add(".results-pod #subpod_0100_1")
  66. .data("tempFileID", "MSP44511e0dda34g97a0c89000059490h319161eea3")
  67. .data("cellDataTempFile", "MSP44521e0dda34g97a0c89000011378c50d38ede6h")
  68. .data("tempFileServer", "")
  69. .data("dataSources", "")
  70. .data("sources", "")
  71. .data("sharetype", "1")
  72. .data("shareable", "false");
  73. } catch(e){}
  74. //false
  75. try {
  76. if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
  77. context.jsonArray.popups.pod_0200 = [];
  78. }
  79. context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": "", "popLinks": {} });
  80. } catch(e) { }
  81. try {
  82. $("#results #pod_0200:not(iframe #pod_0200)")
  83. .add("#showsteps #pod_0200:not(iframe #pod_0200)")
  84. .add(".results-pod #pod_0200:not(iframe #pod_0200)")
  85. .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
  86. .data("podIdentifier", '\x22Result\x22')
  87. .data("podShortIdentifier", '\x22Result\x22')
  88. .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None,\x20None,\x20None\x7D')
  89. .data("scanner", '\x22\x22');
  90. $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
  91. .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
  92. .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
  93. .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
  94. .data("podIdentifier", '\x22Result\x22')
  95. .data("podShortIdentifier", '\x22Result\x22')
  96. .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None\x7D')
  97. .data("scanner", '\x22\x22');
  98. $("#results #subpod_0200_1")
  99. .add("#showsteps #subpod_0200_1:not(iframe #subpod_0200_1)")
  100. .add(".results-pod #subpod_0200_1")
  101. .data("tempFileID", "MSP44551e0dda34g97a0c8900003gdgd37faa7272e0")
  102. .data("cellDataTempFile", "MSP44561e0dda34g97a0c89000018ea1iae00104g13")
  103. .data("tempFileServer", "")
  104. .data("dataSources", "")
  105. .data("sources", "")
  106. .data("sharetype", "1")
  107. .data("shareable", "false");
  108. } catch(e){}
  109. </script>
  110. </body>
  111. </html>
  112. """
  113. response = mock.Mock(text=html)
  114. results = wolframalpha_noapi.response(response)
  115. self.assertEqual(type(results), list)
  116. self.assertEqual(len(results), 2)
  117. self.assertIn("i", results[0]['answer'])
  118. self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
  119. self.assertIn("http://www.wolframalpha.com/input/?i=+sqrt%28-1%29", results[1]['url'])
  120. html = """
  121. <!DOCTYPE html>
  122. <title> integral 1/x - Wolfram|Alpha</title>
  123. <meta charset="utf-8" />
  124. <body>
  125. <script type="text/javascript">
  126. //true
  127. try {
  128. document.domain = "wolframalpha.com";
  129. context = parent ? parent : document;
  130. } catch(e){}
  131. try {
  132. if (typeof(context.$) == "undefined") {
  133. context = window;
  134. } else {
  135. $=context.$;
  136. }
  137. }
  138. catch(e){ context = window;}
  139. try {
  140. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  141. context.jsonArray.popups.pod_0100 = [];
  142. }
  143. context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
  144. } catch(e) { }
  145. try {
  146. $("#results #pod_0100:not(iframe #pod_0100)")
  147. .add("#showsteps #pod_0100:not(iframe #pod_0100)")
  148. .add(".results-pod #pod_0100:not(iframe #pod_0100)")
  149. .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
  150. .data("podIdentifier", '\x22IndefiniteIntegral\x22')
  151. .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
  152. .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
  153. .data("scanner", '\x22\x22');
  154. $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
  155. .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
  156. .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
  157. .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
  158. .data("podIdentifier", '\x22IndefiniteIntegral\x22')
  159. .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
  160. .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
  161. .data("scanner", '\x22\x22');
  162. $("#results #subpod_0100_1")
  163. .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
  164. .add(".results-pod #subpod_0100_1")
  165. .data("tempFileID", "MSP2071if2202e8bg0757100004dg60f2a4ca8cf73")
  166. .data("cellDataTempFile", "MSP2081if2202e8bg0757100001h18329f72fe90fg")
  167. .data("tempFileServer", "")
  168. .data("dataSources", "")
  169. .data("sources", "")
  170. .data("sharetype", "1")
  171. .data("shareable", "false");
  172. } catch(e){}
  173. //false
  174. try {
  175. $("#results #pod_0200:not(iframe #pod_0200)")
  176. .add("#showsteps #pod_0200:not(iframe #pod_0200)")
  177. .add(".results-pod #pod_0200:not(iframe #pod_0200)")
  178. .data("tempFileID", '')
  179. .data("podIdentifier", '\x22Plot\x22')
  180. .data("podShortIdentifier", '')
  181. .data("buttonStates", '')
  182. .data("scanner", '\x22\x22');
  183. $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
  184. .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
  185. .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
  186. .data("tempFileID", '')
  187. .data("podIdentifier", '\x22Plot\x22')
  188. .data("podShortIdentifier", '')
  189. .data("buttonStates", '')
  190. .data("scanner", '\x22\x22');
  191. } catch(e){}
  192. </script>
  193. </body>
  194. </html>
  195. """
  196. response = mock.Mock(text=html)
  197. results = wolframalpha_noapi.response(response)
  198. self.assertEqual(type(results), list)
  199. self.assertEqual(len(results), 2)
  200. self.assertIn("log(x)+c", results[0]['answer'])
  201. self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
  202. self.assertIn("http://www.wolframalpha.com/input/?i=+integral+1%2Fx", results[1]['url'])