@@ -0,0 +1,307 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
4+from searx.engines import wolframalpha_api
5+from searx.testing import SearxTestCase
8+class TestWolframAlphaAPIEngine(SearxTestCase):
10+ def test_request(self):
12+ api_key = 'XXXXXX-XXXXXXXXXX'
13+ dicto = defaultdict(dict)
14+ dicto['api_key'] = api_key
15+ params = wolframalpha_api.request(query, dicto)
17+ self.assertIn('url', params)
18+ self.assertIn(query, params['url'])
19+ self.assertIn('wolframalpha.com', params['url'])
21+ self.assertIn('api_key', params)
22+ self.assertIn(api_key, params['api_key'])
24+ def test_response(self):
25+ self.assertRaises(AttributeError, wolframalpha_api.response, None)
26+ self.assertRaises(AttributeError, wolframalpha_api.response, [])
27+ self.assertRaises(AttributeError, wolframalpha_api.response, '')
28+ self.assertRaises(AttributeError, wolframalpha_api.response, '[]')
30+ xml = '''<?xml version='1.0' encoding='UTF-8'?>
31+ <queryresult success='false' error='false' />
34+ response = mock.Mock(content=xml)
35+ self.assertEqual(wolframalpha_api.response(response), [])
37+ xml = """<?xml version='1.0' encoding='UTF-8'?>
38+ <queryresult success='true'
46+ parsetimedout='false'
48+ id='MSPa416020a7966dachc463600000f9c66cc21444cfg'
49+ host='http://www3.wolframalpha.com'
51+ related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...'
60+ <plaintext>sqrt(-1)</plaintext>
64+ scanner='Simplification'
71+ <plaintext></plaintext>
74+ <state name='Step-by-step solution'
75+ input='Result__Step-by-step solution' />
78+ <pod title='Polar coordinates'
80+ id='PolarCoordinates'
85+ <plaintext>r1 (radius), θ90° (angle)</plaintext>
88+ <pod title='Position in the complex plane'
90+ id='PositionInTheComplexPlane'
95+ <plaintext></plaintext>
98+ <pod title='All 2nd roots of -1'
99+ scanner='RootsOfUnity'
105+ <plaintext> (principal root)</plaintext>
108+ <plaintext>-</plaintext>
111+ <pod title='Plot of all roots in the complex plane'
112+ scanner='RootsOfUnity'
113+ id='PlotOfAllRootsInTheComplexPlane'
118+ <plaintext></plaintext>
123+ # test private user area char in response
124+ response = mock.Mock(content=xml)
125+ results = wolframalpha_api.response(response)
126+ self.assertEqual(type(results), list)
127+ self.assertEqual(len(results), 2)
128+ self.assertIn('i', results[0]['answer'])
129+ self.assertIn('sqrt(-1) - Wolfram|Alpha', results[1]['title'])
130+ self.assertEquals('http://www.wolframalpha.com/input/?i=sqrt%28-1%29', results[1]['url'])
132+ xml = """<?xml version='1.0' encoding='UTF-8'?>
133+ <queryresult success='true'
141+ parsetimedout='false'
143+ id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
144+ host='http://www3.wolframalpha.com'
146+ related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
148+ <pod title='Indefinite integral'
150+ id='IndefiniteIntegral'
156+ <plaintext>∫1/xxlog(x)+constant</plaintext>
159+ <state name='Step-by-step solution'
160+ input='IndefiniteIntegral__Step-by-step solution' />
163+ <info text='log(x) is the natural logarithm'>
164+ <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
165+ text='Documentation'
166+ title='Mathematica' />
167+ <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
169+ title='Wolfram Functions Site' />
170+ <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
172+ title='MathWorld' />
176+ <pod title='Plots of the integral'
183+ <plaintext></plaintext>
185+ <statelist count='2'
186+ value='Complex-valued plot'
188+ <state name='Complex-valued plot'
189+ input='Plot__1_Complex-valued plot' />
190+ <state name='Real-valued plot'
191+ input='Plot__1_Real-valued plot' />
196+ <plaintext></plaintext>
198+ <statelist count='2'
199+ value='Complex-valued plot'
201+ <state name='Complex-valued plot'
202+ input='Plot__2_Complex-valued plot' />
203+ <state name='Real-valued plot'
204+ input='Plot__2_Real-valued plot' />
209+ <assumptions count='1'>
210+ <assumption type='Clash'
212+ template='Assuming "${word}" is ${desc1}. Use as ${desc2} instead'
214+ <value name='IntegralsWord'
216+ input='*C.integral-_*IntegralsWord-' />
217+ <value name='MathematicalFunctionIdentityPropertyClass'
218+ desc='a function property'
219+ input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
225+ response = mock.Mock(content=xml)
226+ results = wolframalpha_api.response(response)
227+ self.assertEqual(type(results), list)
228+ self.assertEqual(len(results), 2)
229+ self.assertIn('log(x)+c', results[0]['answer'])
230+ self.assertIn('∫1/xx - Wolfram|Alpha'.decode('utf-8'), results[1]['title'])
231+ self.assertEquals('http://www.wolframalpha.com/input/?i=%E2%88%AB1%2Fx%EF%9D%8Cx', results[1]['url'])
233+ xml = """<?xml version='1.0' encoding='UTF-8'?>
234+ <queryresult success='true'
242+ parsetimedout='false'
244+ id='MSPa7481f7i06d25h3deh2900004810i3a78d9b4fdc'
245+ host='http://www5b.wolframalpha.com'
247+ related='http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
249+ <pod title='Input interpretation'
256+ <plaintext>solve x^2+x0</plaintext>
259+ <pod title='Results'
267+ <plaintext>x-1</plaintext>
270+ <plaintext>x0</plaintext>
273+ <state name='Step-by-step solution'
274+ input='Result__Step-by-step solution' />
277+ <pod title='Root plot'
284+ <plaintext></plaintext>
287+ <pod title='Number line'
294+ <plaintext></plaintext>
299+ # test ecuation with multiple answers
300+ response = mock.Mock(content=xml)
301+ results = wolframalpha_api.response(response)
302+ self.assertEqual(type(results), list)
303+ self.assertEqual(len(results), 3)
304+ self.assertIn('x=-1', results[0]['answer'])
305+ self.assertIn('x=0', results[1]['answer'])
306+ self.assertIn('solve x^2+x0 - Wolfram|Alpha'.decode('utf-8'), results[2]['title'])
307+ self.assertEquals('http://www.wolframalpha.com/input/?i=solve+x%5E2%2Bx%EF%9F%990', results[2]['url'])