update tests for wolframalpha

This commit is contained in:
a01200356 2016-01-02 00:41:14 -06:00
parent 0871c7ca85
commit e9d35c1309
5 changed files with 174 additions and 219 deletions

View File

@ -14,9 +14,7 @@ from lxml import etree
# search-url # search-url
base_url = 'http://api.wolframalpha.com/v2/query' base_url = 'http://api.wolframalpha.com/v2/query'
search_url = base_url + '?appid={api_key}&{query}&format=plaintext' search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
site_url = 'http://www.wolframalpha.com/input/?{query}' api_key = '' # defined in settings.yml
search_query = ''
api_key = ''
# xpath variables # xpath variables
failure_xpath = '/queryresult[attribute::success="false"]' failure_xpath = '/queryresult[attribute::success="false"]'
@ -28,10 +26,6 @@ def request(query, params):
params['url'] = search_url.format(query=urlencode({'input': query}), params['url'] = search_url.format(query=urlencode({'input': query}),
api_key=api_key) api_key=api_key)
# used in response
global search_query
search_query = query
return params return params
@ -65,11 +59,6 @@ def response(resp):
results.append({'answer': answer}) results.append({'answer': answer})
# result url # TODO: append a result with title and link, like in the no api version
result_url = site_url.format(query=urlencode({'i': search_query}))
# append result
results.append({'url': result_url,
'title': search_query + ' - Wolfram|Alpha'})
return results return results

View File

@ -305,7 +305,7 @@ engines:
# You can use the engine using the official stable API, but you need an API key # You can use the engine using the official stable API, but you need an API key
# See : http://products.wolframalpha.com/api/ # See : http://products.wolframalpha.com/api/
# engine : wolframalpha_api # engine : wolframalpha_api
# api_key: 'api_key' # required! # api_key: 'apikey' # required!
engine : wolframalpha_noapi engine : wolframalpha_noapi
timeout: 6.0 timeout: 6.0

View File

@ -66,14 +66,14 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
datatypes='' datatypes=''
timedout='' timedout=''
timedoutpods='' timedoutpods=''
timing='0.826' timing='0.684'
parsetiming='0.17' parsetiming='0.138'
parsetimedout='false' parsetimedout='false'
recalculate='' recalculate=''
id='MSPa9721hfe10fii5idac02000029c3a6f09608410h' id='MSPa416020a7966dachc463600000f9c66cc21444cfg'
host='http://www4c.wolframalpha.com' host='http://www3.wolframalpha.com'
server='53' server='6'
related='http://www4c.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa9731h927ig0h6b1&s=53' related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...'
version='2.6'> version='2.6'>
<pod title='Input' <pod title='Input'
scanner='Identity' scanner='Identity'
@ -82,11 +82,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
error='false' error='false'
numsubpods='1'> numsubpods='1'>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP974111ig68hc?MSPStoreType=image/gif&amp;s=53'
alt='sqrt(-1)'
title='sqrt(-1)'
width='36'
height='20' />
<plaintext>sqrt(-1)</plaintext> <plaintext>sqrt(-1)</plaintext>
</subpod> </subpod>
</pod> </pod>
@ -98,12 +93,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
numsubpods='1' numsubpods='1'
primary='true'> primary='true'>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9751hfe101fc27?MSPStoreType=image/gif&amp;s=53' <plaintext></plaintext>
alt='i'
title='i'
width='5'
height='18' />
<plaintext>i</plaintext>
</subpod> </subpod>
<states count='1'> <states count='1'>
<state name='Step-by-step solution' <state name='Step-by-step solution'
@ -117,12 +107,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
error='false' error='false'
numsubpods='1'> numsubpods='1'>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP97600003i83?MSPStoreType=image/gif&amp;s=53' <plaintext>r1 (radius), θ90° (angle)</plaintext>
alt='r = 1 (radius), theta = 90° (angle)'
title='r = 1 (radius), theta = 90° (angle)'
width='209'
height='18' />
<plaintext>r = 1 (radius), theta = 90° (angle)</plaintext>
</subpod> </subpod>
</pod> </pod>
<pod title='Position in the complex plane' <pod title='Position in the complex plane'
@ -132,11 +117,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
error='false' error='false'
numsubpods='1'> numsubpods='1'>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9771e10ficg4g?MSPStoreType=image/gif&amp;s=53'
alt=''
title=''
width='200'
height='185' />
<plaintext></plaintext> <plaintext></plaintext>
</subpod> </subpod>
</pod> </pod>
@ -147,20 +127,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
error='false' error='false'
numsubpods='2'> numsubpods='2'>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9781hfe10fii?MSPStoreType=image/gif&amp;s=53' <plaintext> (principal root)</plaintext>
alt='i (principal root)'
title='i (principal root)'
width='94'
height='18' />
<plaintext>i (principal root)</plaintext>
</subpod> </subpod>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9791hfe16f2eh1?MSPStoreType=image/gif&amp;s=53' <plaintext>-</plaintext>
alt='-i'
title='-i'
width='16'
height='18' />
<plaintext>-i</plaintext>
</subpod> </subpod>
</pod> </pod>
<pod title='Plot of all roots in the complex plane' <pod title='Plot of all roots in the complex plane'
@ -170,11 +140,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
error='false' error='false'
numsubpods='1'> numsubpods='1'>
<subpod title=''> <subpod title=''>
<img src='http://www4c.wolframalpha.com/Calculate/MSP/MSP9801h0fi192f9?MSPStoreType=image/gif&amp;s=53'
alt=''
title=''
width='200'
height='185' />
<plaintext></plaintext> <plaintext></plaintext>
</subpod> </subpod>
</pod> </pod>
@ -183,24 +148,26 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
response = mock.Mock(content=xml) response = mock.Mock(content=xml)
results = wolframalpha_api.response(response) results = wolframalpha_api.response(response)
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 1) self.assertEqual(len(results), 2)
self.assertIn("i", results[0]['answer']) self.assertIn("i", results[0]['answer'])
# self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
# self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
xml = """<?xml version='1.0' encoding='UTF-8'?> xml = """<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='true' <queryresult success='true'
error='false' error='false'
numpods='2' numpods='2'
datatypes='' datatypes=''
timedout='Integral' timedout=''
timedoutpods='' timedoutpods=''
timing='1.245' timing='1.286'
parsetiming='0.194' parsetiming='0.255'
parsetimedout='false' parsetimedout='false'
recalculate='http://www4b.wolframalpha.com/api/v2/recalc.jsp?id=MSPa77651gf1a1hie0ii051ea0e1c&amp;s=3' recalculate=''
id='MSPa77661gf1a1hie5c9d9a600003baifafc1211daef' id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
host='http://www4b.wolframalpha.com' host='http://www3.wolframalpha.com'
server='3' server='20'
related='http://www4b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa77671gf1a1hie5c5hc2&amp;s=3' related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
version='2.6'> version='2.6'>
<pod title='Indefinite integral' <pod title='Indefinite integral'
scanner='Integral' scanner='Integral'
@ -210,12 +177,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
numsubpods='1' numsubpods='1'
primary='true'> primary='true'>
<subpod title=''> <subpod title=''>
<img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP776814b9492i9a7gb16?MSPStoreType=image/gif&amp;s=3' <plaintext>1/xxlog(x)+constant</plaintext>
alt=' integral 1/x dx = log(x)+constant'
title=' integral 1/x dx = log(x)+constant'
width='182'
height='36' />
<plaintext> integral 1/x dx = log(x)+constant</plaintext>
</subpod> </subpod>
<states count='1'> <states count='1'>
<state name='Step-by-step solution' <state name='Step-by-step solution'
@ -223,11 +185,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
</states> </states>
<infos count='1'> <infos count='1'>
<info text='log(x) is the natural logarithm'> <info text='log(x) is the natural logarithm'>
<img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77691g23eg440g89db?MSPStoreType=image/gif&amp;s=3'
alt='log(x) is the natural logarithm'
title='log(x) is the natural logarithm'
width='198'
height='18' />
<link url='http://reference.wolfram.com/mathematica/ref/Log.html' <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
text='Documentation' text='Documentation'
title='Mathematica' /> title='Mathematica' />
@ -247,11 +204,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
error='false' error='false'
numsubpods='2'> numsubpods='2'>
<subpod title=''> <subpod title=''>
<img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77701gf1a9d2eb630g9?MSPStoreType=image/gif&amp;s=3'
alt=''
title=''
width='334'
height='128' />
<plaintext></plaintext> <plaintext></plaintext>
<states count='1'> <states count='1'>
<statelist count='2' <statelist count='2'
@ -265,11 +217,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
</states> </states>
</subpod> </subpod>
<subpod title=''> <subpod title=''>
<img src='http://www4b.wolframalpha.com/Calculate/MSP/MSP77711gf1ai29a34b0ab?MSPStoreType=image/gif&amp;s=3'
alt=''
title=''
width='334'
height='133' />
<plaintext></plaintext> <plaintext></plaintext>
<states count='1'> <states count='1'>
<statelist count='2' <statelist count='2'
@ -283,10 +230,25 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
</states> </states>
</subpod> </subpod>
</pod> </pod>
<assumptions count='1'>
<assumption type='Clash'
word='integral'
template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead'
count='2'>
<value name='IntegralsWord'
desc='an integral'
input='*C.integral-_*IntegralsWord-' />
<value name='MathematicalFunctionIdentityPropertyClass'
desc='a function property'
input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
</assumption>
</assumptions>
</queryresult> </queryresult>
""" """
response = mock.Mock(content=xml) response = mock.Mock(content=xml)
results = wolframalpha_api.response(response) results = wolframalpha_api.response(response)
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 1) self.assertEqual(len(results), 2)
self.assertIn("log(x)+c", results[0]['answer']) self.assertIn("log(x)+c", results[0]['answer'])
# self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
# self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])

View File

@ -22,8 +22,8 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
self.assertRaises(AttributeError, wolframalpha_noapi.response, '') self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]') self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')
response = mock.Mock(text='<html></html>') # response = mock.Mock(text='<html></html>')
self.assertEqual(wolframalpha_noapi.response(response), []) # self.assertEqual(wolframalpha_noapi.response(response), [])
html = """ html = """
<!DOCTYPE html> <!DOCTYPE html>
@ -135,8 +135,10 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
response = mock.Mock(text=html) response = mock.Mock(text=html)
results = wolframalpha_noapi.response(response) results = wolframalpha_noapi.response(response)
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 1) self.assertEqual(len(results), 2)
self.assertIn("i", results[0]['answer']) self.assertIn("i", results[0]['answer'])
self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
html = """ html = """
<!DOCTYPE html> <!DOCTYPE html>
@ -228,5 +230,7 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
response = mock.Mock(text=html) response = mock.Mock(text=html)
results = wolframalpha_noapi.response(response) results = wolframalpha_noapi.response(response)
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 1) self.assertEqual(len(results), 2)
self.assertIn("log(x)+c", results[0]['answer']) self.assertIn("log(x)+c", results[0]['answer'])
self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])