test_wolframalpha_noapi.py 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. # test failed query
  39. response = mock.Mock(text=html)
  40. self.assertEqual(wolframalpha_noapi.response(response), [])
  41. html = """
  42. <!DOCTYPE html>
  43. <title> sqrt(-1) - Wolfram|Alpha</title>
  44. <meta charset="utf-8" />
  45. <body>
  46. <script type="text/javascript">
  47. try {
  48. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  49. context.jsonArray.popups.pod_0100 = [];
  50. }
  51. context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": ""});
  52. } catch(e) { }
  53. try {
  54. if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
  55. context.jsonArray.popups.pod_0200 = [];
  56. }
  57. context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": ""});
  58. } catch(e) { }
  59. </script>
  60. </body>
  61. </html>
  62. """
  63. # test plaintext
  64. response = mock.Mock(text=html)
  65. results = wolframalpha_noapi.response(response)
  66. self.assertEqual(type(results), list)
  67. self.assertEqual(len(results), 2)
  68. self.assertEquals('i', results[0]['answer'])
  69. self.assertIn('sqrt(-1) - Wolfram|Alpha', results[1]['title'])
  70. self.assertEquals('http://www.wolframalpha.com/input/?i=+sqrt%28-1%29', results[1]['url'])
  71. html = """
  72. <!DOCTYPE html>
  73. <title> integral 1/x - Wolfram|Alpha</title>
  74. <meta charset="utf-8" />
  75. <body>
  76. <script type="text/javascript">
  77. try {
  78. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  79. context.jsonArray.popups.pod_0100 = [];
  80. }
  81. context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
  82. } catch(e) { }
  83. </script>
  84. </body>
  85. </html>
  86. """
  87. # test integral
  88. response = mock.Mock(text=html)
  89. results = wolframalpha_noapi.response(response)
  90. self.assertEqual(type(results), list)
  91. self.assertEqual(len(results), 2)
  92. self.assertIn('log(x)+c', results[0]['answer'])
  93. self.assertIn('integral 1/x - Wolfram|Alpha', results[1]['title'])
  94. self.assertEquals('http://www.wolframalpha.com/input/?i=+integral+1%2Fx', results[1]['url'])
  95. html = """
  96. <!DOCTYPE html>
  97. <title> &int;1&#x2f;x &#xf74c;x - Wolfram|Alpha</title>
  98. <meta charset="utf-8" />
  99. <body>
  100. <script type="text/javascript">
  101. try {
  102. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  103. context.jsonArray.popups.pod_0100 = [];
  104. }
  105. context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
  106. } catch(e) { }
  107. </script>
  108. </body>
  109. </html>
  110. """
  111. # test input in mathematical notation
  112. response = mock.Mock(text=html)
  113. results = wolframalpha_noapi.response(response)
  114. self.assertEqual(type(results), list)
  115. self.assertEqual(len(results), 2)
  116. self.assertIn('log(x)+c', results[0]['answer'])
  117. self.assertIn('∫1/x x - Wolfram|Alpha'.decode('utf-8'), results[1]['title'])
  118. self.assertEquals('http://www.wolframalpha.com/input/?i=+%E2%88%AB1%2Fx+%EF%9D%8Cx', results[1]['url'])
  119. html = """
  120. <!DOCTYPE html>
  121. <title> 1 euro to yen - Wolfram|Alpha</title>
  122. <meta charset="utf-8" />
  123. <body>
  124. <script type="text/javascript">
  125. try {
  126. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  127. context.jsonArray.popups.pod_0100 = [];
  128. }
  129. context.jsonArray.popups.pod_0100.push( {"stringified": "convert euro1 (euro) to Japanese yen"});
  130. } catch(e) { }
  131. try {
  132. if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
  133. context.jsonArray.popups.pod_0200 = [];
  134. }
  135. context.jsonArray.popups.pod_0200.push( {"stringified": "&yen;130.5 (Japanese yen)"});
  136. } catch(e) { }
  137. </script>
  138. </body>
  139. </html>
  140. """
  141. # test output with htmlentity
  142. response = mock.Mock(text=html)
  143. results = wolframalpha_noapi.response(response)
  144. self.assertEqual(type(results), list)
  145. self.assertEqual(len(results), 2)
  146. self.assertIn('¥'.decode('utf-8'), results[0]['answer'])
  147. self.assertIn('1 euro to yen - Wolfram|Alpha', results[1]['title'])
  148. self.assertEquals('http://www.wolframalpha.com/input/?i=+1+euro+to+yen', results[1]['url'])
  149. html = """
  150. <!DOCTYPE html>
  151. <title> distance from nairobi to kyoto in inches - Wolfram|Alpha</title>
  152. <meta charset="utf-8" />
  153. <body>
  154. <script type="text/javascript">
  155. try {
  156. if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
  157. context.jsonArray.popups.pod_0100 = [];
  158. }
  159. [...].pod_0100.push( {"stringified": "convert distance | from | Nairobi, Kenya\nto | Kyoto, Japan to inches"});
  160. } catch(e) { }
  161. try {
  162. if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
  163. context.jsonArray.popups.pod_0200 = [];
  164. }
  165. pod_0200.push({"stringified": "4.295&times;10^8 inches","mOutput": "Quantity[4.295×10^8,&amp;quot;Inches&amp;quot;]"});
  166. } catch(e) { }
  167. </script>
  168. </body>
  169. </html>
  170. """
  171. # test output with utf-8 character
  172. response = mock.Mock(text=html)
  173. results = wolframalpha_noapi.response(response)
  174. self.assertEqual(type(results), list)
  175. self.assertEqual(len(results), 2)
  176. self.assertIn('4.295×10^8 inches'.decode('utf-8'), results[0]['answer'])
  177. self.assertIn('distance from nairobi to kyoto in inches - Wolfram|Alpha', results[1]['title'])
  178. self.assertEquals('http://www.wolframalpha.com/input/?i=+distance+from+nairobi+to+kyoto+in+inches',
  179. results[1]['url'])