logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_wolframalpha_api.py (6976B)


  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(text=xml.encode('utf-8'))
  31. self.assertEqual(wolframalpha_api.response(response), [])
  32. # test basic case
  33. xml = b"""<?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(text=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 (input_plaintext)', results[1]['title'])
  95. self.assertIn('result_plaintext', results[1]['content'])
  96. # test calc
  97. xml = b"""<?xml version='1.0' encoding='UTF-8'?>
  98. <queryresult success='true'
  99. error='false'
  100. numpods='2'
  101. datatypes=''
  102. parsetimedout='false'
  103. id='queryresult_id'
  104. host='http://www5b.wolframalpha.com'
  105. related='related_url'
  106. version='2.6' >
  107. <pod title='Indefinite integral'
  108. scanner='Integral'
  109. id='IndefiniteIntegral'
  110. error='false'
  111. numsubpods='1'
  112. primary='true'>
  113. <subpod title=''>
  114. <img src='integral_image.gif'
  115. alt='integral_img_alt'
  116. title='integral_img_title' />
  117. <plaintext>integral_plaintext</plaintext>
  118. </subpod>
  119. </pod>
  120. <pod title='Plot of the integral'
  121. scanner='Integral'
  122. id='Plot'
  123. error='false'
  124. numsubpods='1'>
  125. <subpod title=''>
  126. <img src='plot.gif'
  127. alt='plot_alt'
  128. title='' />
  129. <plaintext></plaintext>
  130. </subpod>
  131. </pod>
  132. </queryresult>
  133. """
  134. response = mock.Mock(text=xml, request=request)
  135. results = wolframalpha_api.response(response)
  136. self.assertEqual(type(results), list)
  137. self.assertEqual(len(results), 2)
  138. self.assertEqual('integral_plaintext', results[0]['infobox'])
  139. self.assertEqual(len(results[0]['attributes']), 2)
  140. self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
  141. self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
  142. self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
  143. self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
  144. self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
  145. self.assertEqual(len(results[0]['urls']), 1)
  146. self.assertEqual(referer_url, results[0]['urls'][0]['url'])
  147. self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
  148. self.assertEqual(referer_url, results[1]['url'])
  149. self.assertEqual('Wolfram|Alpha (integral_plaintext)', results[1]['title'])
  150. self.assertIn('integral_plaintext', results[1]['content'])