on master help sign in
update tests for wolframalpha
commite9d35c1309f05a0b214fb323049909ee7ec62ab8 authora01200356 <a01200356@itesm.mx> dateSat 2 Jan 2016, 00:41 (11 years ago) parent0871c7c
5 files changed+172−217
searx/engines/wolframalpha_api.py+2−13 searx/engines/wolframalpha_noapi.py+1−1 searx/settings.yml+1−1 searx/tests/engines/test_wolframalpha_api.py+160−198 searx/tests/engines/test_wolframalpha_noapi.py+8−4
searx/engines/wolframalpha_api.py+2−13
@@ -14,9 +14,7 @@ from lxml import etree
1414# search-url
1515base_url = 'http://api.wolframalpha.com/v2/query'
1616search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17−site_url = 'http://www.wolframalpha.com/input/?{query}'
18−search_query = ''
19−api_key = ''
17+api_key = '' # defined in settings.yml
2018
2119# xpath variables
2220failure_xpath = '/queryresult[attribute::success="false"]'
@@ -28,10 +26,6 @@ def request(query, params):
2826 params['url'] = search_url.format(query=urlencode({'input': query}),
2927 api_key=api_key)
3028
31− # used in response
32− global search_query
33− search_query = query
34−
3529 return params
3630
3731
@@ -65,11 +59,6 @@ def response(resp):
6559
6660 results.append({'answer': answer})
6761
68− # result url
69− result_url = site_url.format(query=urlencode({'i': search_query}))
70−
71− # append result
72− results.append({'url': result_url,
73− 'title': search_query + ' - Wolfram|Alpha'})
62+ # TODO: append a result with title and link, like in the no api version
7463
7564 return results
searx/engines/wolframalpha_noapi.py+1−1
@@ -53,7 +53,7 @@ def response(resp):
5353 answer = line[line.find('{'):line.rfind('}')+1]
5454 answer = loads(answer.encode('unicode-escape'))
5555 answer = answer['stringified'].decode('unicode-escape')
56−
56+
5757 results.append({'answer': answer})
5858
5959 # failed result
searx/settings.yml+1−1
@@ -305,7 +305,7 @@ engines:
305305 # You can use the engine using the official stable API, but you need an API key
306306 # See : http://products.wolframalpha.com/api/
307307 # engine : wolframalpha_api
308− # api_key: 'api_key' # required!
308+ # api_key: 'apikey' # required!
309309 engine : wolframalpha_noapi
310310 timeout: 6.0
311311
searx/tests/engines/test_wolframalpha_api.py+160−198
@@ -59,234 +59,196 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
5959 response = mock.Mock(content=xml)
6060 self.assertEqual(wolframalpha_api.response(response), [])
6161
62+ xml = """<?xml version='1.0' encoding='UTF-8'?>
63+ <queryresult success='true'
64+ error='false'
65+ numpods='6'
66+ datatypes=''
67+ timedout=''
68+ timedoutpods=''
69+ timing='0.684'
70+ parsetiming='0.138'
71+ parsetimedout='false'
72+ recalculate=''
73+ id='MSPa416020a7966dachc463600000f9c66cc21444cfg'
74+ host='http://www3.wolframalpha.com'
75+ server='6'
76+ related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...'
77+ version='2.6'>
78+ <pod title='Input'
79+ scanner='Identity'
80+ id='Input'
81+ position='100'
82+ error='false'
83+ numsubpods='1'>
84+ <subpod title=''>
85+ <plaintext>sqrt(-1)</plaintext>
86+ </subpod>
87+ </pod>
88+ <pod title='Result'
89+ scanner='Simplification'
90+ id='Result'
91+ position='200'
92+ error='false'
93+ numsubpods='1'
94+ primary='true'>
95+ <subpod title=''>
96+ <plaintext></plaintext>
97+ </subpod>
98+ <states count='1'>
99+ <state name='Step-by-step solution'
100+ input='Result__Step-by-step solution' />
101+ </states>
102+ </pod>
103+ <pod title='Polar coordinates'
104+ scanner='Numeric'
105+ id='PolarCoordinates'
106+ position='300'
107+ error='false'
108+ numsubpods='1'>
109+ <subpod title=''>
110+ <plaintext>r1 (radius), θ90° (angle)</plaintext>
111+ </subpod>
112+ </pod>
113+ <pod title='Position in the complex plane'
114+ scanner='Numeric'
115+ id='PositionInTheComplexPlane'
116+ position='400'
117+ error='false'
118+ numsubpods='1'>
119+ <subpod title=''>
120+ <plaintext></plaintext>
121+ </subpod>
122+ </pod>
123+ <pod title='All 2nd roots of -1'
124+ scanner='RootsOfUnity'
125+ id=''
126+ position='500'
127+ error='false'
128+ numsubpods='2'>
129+ <subpod title=''>
130+ <plaintext> (principal root)</plaintext>
131+ </subpod>
132+ <subpod title=''>
133+ <plaintext>-</plaintext>
134+ </subpod>
135+ </pod>
136+ <pod title='Plot of all roots in the complex plane'
137+ scanner='RootsOfUnity'
138+ id='PlotOfAllRootsInTheComplexPlane'
139+ position='600'
140+ error='false'
141+ numsubpods='1'>
142+ <subpod title=''>
143+ <plaintext></plaintext>
144+ </subpod>
145+ </pod>
146+ </queryresult>
147+ """
148+ response = mock.Mock(content=xml)
149+ results = wolframalpha_api.response(response)
150+ self.assertEqual(type(results), list)
151+ self.assertEqual(len(results), 2)
152+ self.assertIn("i", results[0]['answer'])
153+ # self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
154+ # self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
155+
62156 xml = """<?xml version='1.0' encoding='UTF-8'?>
63157 <queryresult success='true'
64158 error='false'
65− numpods='6'
159+ numpods='2'
66160 datatypes=''
67161 timedout=''
68162 timedoutpods=''
69− timing='0.826'
70− parsetiming='0.17'
163+ timing='1.286'
164+ parsetiming='0.255'
71165 parsetimedout='false'
72166 recalculate=''
73− id='MSPa9721hfe10fii5idac02000029c3a6f09608410h'
74− host='http://www4c.wolframalpha.com'
75− server='53'
76− related='http://www4c.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa9731h927ig0h6b1&amp;s=53'
167+ id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
168+ host='http://www3.wolframalpha.com'
169+ server='20'
170+ related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
77171 version='2.6'>
78− <pod title='Input'
79− scanner='Identity'
80− id='Input'
172+ <pod title='Indefinite integral'
173+ scanner='Integral'
174+ id='IndefiniteIntegral'
81175 position='100'
82176 error='false'
83− numsubpods='1'>
84− <subpod title=''>
85− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP974111ig68hc?MSPStoreType=image/gif&amp;s=53'
86− alt='sqrt(-1)'
87− title='sqrt(-1)'
88− width='36'
89− height='20' />
90− <plaintext>sqrt(-1)</plaintext>
91− </subpod>
92− </pod>
93− <pod title='Result'
94− scanner='Simplification'
95− id='Result'
96− position='200'
97− error='false'
98177 numsubpods='1'
99178 primary='true'>
100179 <subpod title=''>
101− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9751hfe101fc27?MSPStoreType=image/gif&amp;s=53'
102− alt='i'
103− title='i'
104− width='5'
105− height='18' />
106− <plaintext>i</plaintext>
180+ <plaintext>∫1/xxlog(x)+constant</plaintext>
107181 </subpod>
108182 <states count='1'>
109183 <state name='Step-by-step solution'
110− input='Result__Step-by-step solution' />
184+ input='IndefiniteIntegral__Step-by-step solution' />
111185 </states>
186+ <infos count='1'>
187+ <info text='log(x) is the natural logarithm'>
188+ <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
189+ text='Documentation'
190+ title='Mathematica' />
191+ <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
192+ text='Properties'
193+ title='Wolfram Functions Site' />
194+ <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
195+ text='Definition'
196+ title='MathWorld' />
197+ </info>
198+ </infos>
112199 </pod>
113− <pod title='Polar coordinates'
114− scanner='Numeric'
115− id='PolarCoordinates'
116− position='300'
117− error='false'
118− numsubpods='1'>
119− <subpod title=''>
120− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP97600003i83?MSPStoreType=image/gif&amp;s=53'
121− alt='r = 1 (radius), theta = 90° (angle)'
122− title='r = 1 (radius), theta = 90° (angle)'
123− width='209'
124− height='18' />
125− <plaintext>r = 1 (radius), theta = 90° (angle)</plaintext>
126− </subpod>
127− </pod>
128− <pod title='Position in the complex plane'
129− scanner='Numeric'
130− id='PositionInTheComplexPlane'
131− position='400'
132− error='false'
133− numsubpods='1'>
134− <subpod title=''>
135− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9771e10ficg4g?MSPStoreType=image/gif&amp;s=53'
136− alt=''
137− title=''
138− width='200'
139− height='185' />
140− <plaintext></plaintext>
141− </subpod>
142− </pod>
143− <pod title='All 2nd roots of -1'
144− scanner='RootsOfUnity'
145− id=''
146− position='500'
200+ <pod title='Plots of the integral'
201+ scanner='Integral'
202+ id='Plot'
203+ position='200'
147204 error='false'
148205 numsubpods='2'>
149206 <subpod title=''>
150− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9781hfe10fii?MSPStoreType=image/gif&amp;s=53'
151− alt='i (principal root)'
152− title='i (principal root)'
153− width='94'
154− height='18' />
155− <plaintext>i (principal root)</plaintext>
207+ <plaintext></plaintext>
208+ <states count='1'>
209+ <statelist count='2'
210+ value='Complex-valued plot'
211+ delimiters=''>
212+ <state name='Complex-valued plot'
213+ input='Plot__1_Complex-valued plot' />
214+ <state name='Real-valued plot'
215+ input='Plot__1_Real-valued plot' />
216+ </statelist>
217+ </states>
156218 </subpod>
157219 <subpod title=''>
158− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9791hfe16f2eh1?MSPStoreType=image/gif&amp;s=53'
159− alt='-i'
160− title='-i'
161− width='16'
162− height='18' />
163− <plaintext>-i</plaintext>
164− </subpod>
165− </pod>
166− <pod title='Plot of all roots in the complex plane'
167− scanner='RootsOfUnity'
168− id='PlotOfAllRootsInTheComplexPlane'
169− position='600'
170− error='false'
171− numsubpods='1'>
172− <subpod title=''>
173− <img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9801h0fi192f9?MSPStoreType=image/gif&amp;s=53'
174− alt=''
175− title=''
176− width='200'
177− height='185' />
178220 <plaintext></plaintext>
221+ <states count='1'>
222+ <statelist count='2'
223+ value='Complex-valued plot'
224+ delimiters=''>
225+ <state name='Complex-valued plot'
226+ input='Plot__2_Complex-valued plot' />
227+ <state name='Real-valued plot'
228+ input='Plot__2_Real-valued plot' />
229+ </statelist>
230+ </states>
179231 </subpod>
180232 </pod>
233+ <assumptions count='1'>
234+ <assumption type='Clash'
235+ word='integral'
236+ template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead'
237+ count='2'>
238+ <value name='IntegralsWord'
239+ desc='an integral'
240+ input='*C.integral-_*IntegralsWord-' />
241+ <value name='MathematicalFunctionIdentityPropertyClass'
242+ desc='a function property'
243+ input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
244+ </assumption>
245+ </assumptions>
181246 </queryresult>
182247 """
183248 response = mock.Mock(content=xml)
184249 results = wolframalpha_api.response(response)
185250 self.assertEqual(type(results), list)
186− self.assertEqual(len(results), 1)
187− self.assertIn("i", results[0]['answer'])
188−
189− xml = """<?xml version='1.0' encoding='UTF-8'?>
190− <queryresult success='true'
191− error='false'
192− numpods='2'
193− datatypes=''
194− timedout='Integral'
195− timedoutpods=''
196− timing='1.245'
197− parsetiming='0.194'
198− parsetimedout='false'
199− recalculate='http://www4b.wolframalpha.com/api/v2/recalc.jsp?id=MSPa77651gf1a1hie0ii051ea0e1c&amp;s=3'
200− id='MSPa77661gf1a1hie5c9d9a600003baifafc1211daef'
201− host='http://www4b.wolframalpha.com'
202− server='3'
203− related='http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa77671gf1a1hie5c5hc2&amp;s=3'
204− version='2.6'>
205− <pod title='Indefinite integral'
206− scanner='Integral'
207− id='IndefiniteIntegral'
208− position='100'
209− error='false'
210− numsubpods='1'
211− primary='true'>
212− <subpod title=''>
213− <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP776814b9492i9a7gb16?MSPStoreType=image/gif&amp;s=3'
214− alt=' integral 1/x dx = log(x)+constant'
215− title=' integral 1/x dx = log(x)+constant'
216− width='182'
217− height='36' />
218− <plaintext> integral 1/x dx = log(x)+constant</plaintext>
219− </subpod>
220− <states count='1'>
221− <state name='Step-by-step solution'
222− input='IndefiniteIntegral__Step-by-step solution' />
223− </states>
224− <infos count='1'>
225− <info text='log(x) is the natural logarithm'>
226− <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77691g23eg440g89db?MSPStoreType=image/gif&amp;s=3'
227− alt='log(x) is the natural logarithm'
228− title='log(x) is the natural logarithm'
229− width='198'
230− height='18' />
231− <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
232− text='Documentation'
233− title='Mathematica' />
234− <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
235− text='Properties'
236− title='Wolfram Functions Site' />
237− <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
238− text='Definition'
239− title='MathWorld' />
240− </info>
241− </infos>
242− </pod>
243− <pod title='Plots of the integral'
244− scanner='Integral'
245− id='Plot'
246− position='200'
247− error='false'
248− numsubpods='2'>
249− <subpod title=''>
250− <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77701gf1a9d2eb630g9?MSPStoreType=image/gif&amp;s=3'
251− alt=''
252− title=''
253− width='334'
254− height='128' />
255− <plaintext></plaintext>
256− <states count='1'>
257− <statelist count='2'
258− value='Complex-valued plot'
259− delimiters=''>
260− <state name='Complex-valued plot'
261− input='Plot__1_Complex-valued plot' />
262− <state name='Real-valued plot'
263− input='Plot__1_Real-valued plot' />
264− </statelist>
265− </states>
266− </subpod>
267− <subpod title=''>
268− <img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77711gf1ai29a34b0ab?MSPStoreType=image/gif&amp;s=3'
269− alt=''
270− title=''
271− width='334'
272− height='133' />
273− <plaintext></plaintext>
274− <states count='1'>
275− <statelist count='2'
276− value='Complex-valued plot'
277− delimiters=''>
278− <state name='Complex-valued plot'
279− input='Plot__2_Complex-valued plot' />
280− <state name='Real-valued plot'
281− input='Plot__2_Real-valued plot' />
282− </statelist>
283− </states>
284− </subpod>
285− </pod>
286− </queryresult>
287− """
288− response = mock.Mock(content=xml)
289− results = wolframalpha_api.response(response)
290− self.assertEqual(type(results), list)
291− self.assertEqual(len(results), 1)
251+ self.assertEqual(len(results), 2)
292252 self.assertIn("log(x)+c", results[0]['answer'])
253+ # self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
254+ # self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])
searx/tests/engines/test_wolframalpha_noapi.py+8−4
@@ -22,8 +22,8 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
2222 self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
2323 self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')
2424
25− response = mock.Mock(text='<html></html>')
26− self.assertEqual(wolframalpha_noapi.response(response), [])
25+ # response = mock.Mock(text='<html></html>')
26+ # self.assertEqual(wolframalpha_noapi.response(response), [])
2727
2828 html = """
2929 <!DOCTYPE html>
@@ -135,8 +135,10 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
135135 response = mock.Mock(text=html)
136136 results = wolframalpha_noapi.response(response)
137137 self.assertEqual(type(results), list)
138− self.assertEqual(len(results), 1)
138+ self.assertEqual(len(results), 2)
139139 self.assertIn("i", results[0]['answer'])
140+ self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
141+ self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
140142
141143 html = """
142144 <!DOCTYPE html>
@@ -228,5 +230,7 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
228230 response = mock.Mock(text=html)
229231 results = wolframalpha_noapi.response(response)
230232 self.assertEqual(type(results), list)
231− self.assertEqual(len(results), 1)
233+ self.assertEqual(len(results), 2)
232234 self.assertIn("log(x)+c", results[0]['answer'])
235+ self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
236+ self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])