test_wolframalpha_api.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from requests import Request
  5. from searx.engines import wolframalpha_api
  6. from searx.testing import SearxTestCase
  7. class TestWolframAlphaAPIEngine(SearxTestCase):
  8. def test_request(self):
  9. query = 'test_query'
  10. dicto = defaultdict(dict)
  11. params = wolframalpha_api.request(query, dicto)
  12. # TODO: test api_key
  13. self.assertIn('url', params)
  14. self.assertIn('https://api.wolframalpha.com/v2/query?', params['url'])
  15. self.assertIn(query, params['url'])
  16. self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer'])
  17. def test_replace_pua_chars(self):
  18. self.assertEqual('i', wolframalpha_api.replace_pua_chars(u'\uf74e'))
  19. def test_response(self):
  20. self.assertRaises(AttributeError, wolframalpha_api.response, None)
  21. self.assertRaises(AttributeError, wolframalpha_api.response, [])
  22. self.assertRaises(AttributeError, wolframalpha_api.response, '')
  23. self.assertRaises(AttributeError, wolframalpha_api.response, '[]')
  24. referer_url = 'referer_url'
  25. request = Request(headers={'Referer': referer_url})
  26. # test failure
  27. xml = '''<?xml version='1.0' encoding='UTF-8'?>
  28. <queryresult success='false' error='false' />
  29. '''
  30. response = mock.Mock(content=xml)
  31. self.assertEqual(wolframalpha_api.response(response), [])
  32. # test basic case
  33. xml = """<?xml version='1.0' encoding='UTF-8'?>
  34. <queryresult success='true'
  35. error='false'
  36. numpods='3'
  37. datatypes='Math'
  38. id='queryresult_id'
  39. host='http://www4c.wolframalpha.com'
  40. related='related_url'
  41. version='2.6'>
  42. <pod title='Input'
  43. scanner='Identity'
  44. id='Input'
  45. numsubpods='1'>
  46. <subpod title=''>
  47. <img src='input_img_src.gif'
  48. alt='input_img_alt'
  49. title='input_img_title' />
  50. <plaintext>input_plaintext</plaintext>
  51. </subpod>
  52. </pod>
  53. <pod title='Result'
  54. scanner='Simplification'
  55. id='Result'
  56. numsubpods='1'
  57. primary='true'>
  58. <subpod title=''>
  59. <img src='result_img_src.gif'
  60. alt='result_img_alt'
  61. title='result_img_title' />
  62. <plaintext>result_plaintext</plaintext>
  63. </subpod>
  64. </pod>
  65. <pod title='Manipulatives illustration'
  66. scanner='Arithmetic'
  67. id='Illustration'
  68. numsubpods='1'>
  69. <subpod title=''>
  70. <img src='illustration_img_src.gif'
  71. alt='illustration_img_alt' />
  72. <plaintext>illustration_plaintext</plaintext>
  73. </subpod>
  74. </pod>
  75. </queryresult>
  76. """
  77. response = mock.Mock(content=xml, request=request)
  78. results = wolframalpha_api.response(response)
  79. self.assertEqual(type(results), list)
  80. self.assertEqual(len(results), 2)
  81. self.assertEqual('input_plaintext', results[0]['infobox'])
  82. self.assertEqual(len(results[0]['attributes']), 3)
  83. self.assertEqual('Input', results[0]['attributes'][0]['label'])
  84. self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
  85. self.assertEqual('Result', results[0]['attributes'][1]['label'])
  86. self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
  87. self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
  88. self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
  89. self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
  90. self.assertEqual(len(results[0]['urls']), 1)
  91. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  92. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  93. self.assertEqual(referer_url, results[1]['url'])
  94. self.assertEqual('Wolfram|Alpha', results[1]['title'])
  95. # test calc
  96. xml = """<?xml version='1.0' encoding='UTF-8'?>
  97. <queryresult success='true'
  98. error='false'
  99. numpods='2'
  100. datatypes=''
  101. parsetimedout='false'
  102. id='queryresult_id'
  103. host='http://www5b.wolframalpha.com'
  104. related='related_url'
  105. version='2.6' >
  106. <pod title='Indefinite integral'
  107. scanner='Integral'
  108. id='IndefiniteIntegral'
  109. error='false'
  110. numsubpods='1'
  111. primary='true'>
  112. <subpod title=''>
  113. <img src='integral_image.gif'
  114. alt='integral_img_alt'
  115. title='integral_img_title' />
  116. <plaintext>integral_plaintext</plaintext>
  117. </subpod>
  118. </pod>
  119. <pod title='Plot of the integral'
  120. scanner='Integral'
  121. id='Plot'
  122. error='false'
  123. numsubpods='1'>
  124. <subpod title=''>
  125. <img src='plot.gif'
  126. alt='plot_alt'
  127. title='' />
  128. <plaintext></plaintext>
  129. </subpod>
  130. </pod>
  131. </queryresult>
  132. """
  133. response = mock.Mock(content=xml, request=request)
  134. results = wolframalpha_api.response(response)
  135. self.assertEqual(type(results), list)
  136. self.assertEqual(len(results), 2)
  137. self.assertEqual('integral_plaintext', results[0]['infobox'])
  138. self.assertEqual(len(results[0]['attributes']), 2)
  139. self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
  140. self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
  141. self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
  142. self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
  143. self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
  144. self.assertEqual(len(results[0]['urls']), 1)
  145. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  146. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  147. self.assertEqual(referer_url, results[1]['url'])
  148. self.assertEqual('Wolfram|Alpha', results[1]['title'])