test_wolframalpha_noapi.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from requests import Request
  5. from searx.engines import wolframalpha_noapi
  6. from searx.testing import SearxTestCase
  7. class TestWolframAlphaNoAPIEngine(SearxTestCase):
  8. def test_request(self):
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. params = wolframalpha_noapi.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn('https://www.wolframalpha.com/input/json.jsp', params['url'])
  14. self.assertIn(query, params['url'])
  15. self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer'])
  16. def test_response(self):
  17. self.assertRaises(AttributeError, wolframalpha_noapi.response, None)
  18. self.assertRaises(AttributeError, wolframalpha_noapi.response, [])
  19. self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
  20. self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')
  21. referer_url = 'referer_url'
  22. request = Request(headers={'Referer': referer_url})
  23. # test failure
  24. json = r'''
  25. {"queryresult" : {
  26. "success" : false,
  27. "error" : false,
  28. "numpods" : 0,
  29. "id" : "",
  30. "host" : "https:\/\/www5a.wolframalpha.com",
  31. "didyoumeans" : {}
  32. }}
  33. '''
  34. response = mock.Mock(text=json, request=request)
  35. self.assertEqual(wolframalpha_noapi.response(response), [])
  36. # test basic case
  37. json = r'''
  38. {"queryresult" : {
  39. "success" : true,
  40. "error" : false,
  41. "numpods" : 6,
  42. "datatypes" : "Math",
  43. "id" : "queryresult_id",
  44. "host" : "https:\/\/www5b.wolframalpha.com",
  45. "related" : "related_url",
  46. "version" : "2.6",
  47. "pods" : [
  48. {
  49. "title" : "Input",
  50. "scanners" : [
  51. "Identity"
  52. ],
  53. "id" : "Input",
  54. "error" : false,
  55. "numsubpods" : 1,
  56. "subpods" : [
  57. {
  58. "title" : "",
  59. "img" : {
  60. "src" : "input_img_src.gif",
  61. "alt" : "input_img_alt",
  62. "title" : "input_img_title"
  63. },
  64. "plaintext" : "input_plaintext",
  65. "minput" : "input_minput"
  66. }
  67. ]
  68. },
  69. {
  70. "title" : "Result",
  71. "scanners" : [
  72. "Simplification"
  73. ],
  74. "id" : "Result",
  75. "error" : false,
  76. "numsubpods" : 1,
  77. "primary" : true,
  78. "subpods" : [
  79. {
  80. "title" : "",
  81. "img" : {
  82. "src" : "result_img_src.gif",
  83. "alt" : "result_img_alt",
  84. "title" : "result_img_title"
  85. },
  86. "plaintext" : "result_plaintext",
  87. "moutput" : "result_moutput"
  88. }
  89. ]
  90. },
  91. {
  92. "title" : "Manipulatives illustration",
  93. "scanners" : [
  94. "Arithmetic"
  95. ],
  96. "id" : "Illustration",
  97. "error" : false,
  98. "numsubpods" : 1,
  99. "subpods" : [
  100. {
  101. "title" : "",
  102. "CDFcontent" : "Resizeable",
  103. "img" : {
  104. "src" : "illustration_img_src.gif",
  105. "alt" : "illustration_img_alt",
  106. "title" : "illustration_img_title"
  107. },
  108. "plaintext" : "illustration_img_plaintext"
  109. }
  110. ]
  111. }
  112. ]
  113. }}
  114. '''
  115. response = mock.Mock(text=json, request=request)
  116. results = wolframalpha_noapi.response(response)
  117. self.assertEqual(type(results), list)
  118. self.assertEqual(len(results), 2)
  119. self.assertEqual('input_plaintext', results[0]['infobox'])
  120. self.assertEqual(len(results[0]['attributes']), 3)
  121. self.assertEqual('Input', results[0]['attributes'][0]['label'])
  122. self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
  123. self.assertEqual('Result', results[0]['attributes'][1]['label'])
  124. self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
  125. self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
  126. self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
  127. self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
  128. self.assertEqual(len(results[0]['urls']), 1)
  129. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  130. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  131. self.assertEqual(referer_url, results[1]['url'])
  132. self.assertEqual('Wolfram|Alpha (input_plaintext)', results[1]['title'])
  133. self.assertIn('result_plaintext', results[1]['content'])
  134. # test calc
  135. json = r"""
  136. {"queryresult" : {
  137. "success" : true,
  138. "error" : false,
  139. "numpods" : 2,
  140. "datatypes" : "",
  141. "id" : "queryresult_id",
  142. "host" : "https:\/\/www4b.wolframalpha.com",
  143. "related" : "related_url",
  144. "version" : "2.6",
  145. "pods" : [
  146. {
  147. "title" : "Indefinite integral",
  148. "scanners" : [
  149. "Integral"
  150. ],
  151. "id" : "IndefiniteIntegral",
  152. "error" : false,
  153. "numsubpods" : 1,
  154. "primary" : true,
  155. "subpods" : [
  156. {
  157. "title" : "",
  158. "img" : {
  159. "src" : "integral_img_src.gif",
  160. "alt" : "integral_img_alt",
  161. "title" : "integral_img_title"
  162. },
  163. "plaintext" : "integral_plaintext",
  164. "minput" : "integral_minput",
  165. "moutput" : "integral_moutput"
  166. }
  167. ]
  168. },
  169. {
  170. "title" : "Plot of the integral",
  171. "scanners" : [
  172. "Integral"
  173. ],
  174. "id" : "Plot",
  175. "error" : false,
  176. "numsubpods" : 1,
  177. "subpods" : [
  178. {
  179. "title" : "",
  180. "img" : {
  181. "src" : "plot.gif",
  182. "alt" : "plot_alt",
  183. "title" : "plot_title"
  184. },
  185. "plaintext" : "",
  186. "minput" : "plot_minput"
  187. }
  188. ]
  189. }
  190. ]
  191. }}
  192. """
  193. response = mock.Mock(text=json, request=request)
  194. results = wolframalpha_noapi.response(response)
  195. self.assertEqual(type(results), list)
  196. self.assertEqual(len(results), 2)
  197. self.assertEqual('integral_plaintext', results[0]['infobox'])
  198. self.assertEqual(len(results[0]['attributes']), 2)
  199. self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
  200. self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
  201. self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
  202. self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
  203. self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
  204. self.assertEqual(len(results[0]['urls']), 1)
  205. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  206. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  207. self.assertEqual(referer_url, results[1]['url'])
  208. self.assertEqual('Wolfram|Alpha (integral_plaintext)', results[1]['title'])
  209. self.assertIn('integral_plaintext', results[1]['content'])