2015-12-30 07:53:15 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from collections import defaultdict
|
|
|
|
import mock
|
2016-02-28 07:47:36 +01:00
|
|
|
from requests import Request
|
2015-12-30 07:53:15 +01:00
|
|
|
from searx.engines import wolframalpha_api
|
|
|
|
from searx.testing import SearxTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class TestWolframAlphaAPIEngine(SearxTestCase):
|
|
|
|
|
|
|
|
def test_request(self):
|
|
|
|
query = 'test_query'
|
|
|
|
dicto = defaultdict(dict)
|
|
|
|
params = wolframalpha_api.request(query, dicto)
|
|
|
|
|
2016-02-28 07:47:36 +01:00
|
|
|
# TODO: test api_key
|
2015-12-30 07:53:15 +01:00
|
|
|
self.assertIn('url', params)
|
2016-02-28 07:47:36 +01:00
|
|
|
self.assertIn('https://api.wolframalpha.com/v2/query?', params['url'])
|
2015-12-30 07:53:15 +01:00
|
|
|
self.assertIn(query, params['url'])
|
2016-02-28 07:47:36 +01:00
|
|
|
self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer'])
|
2015-12-30 07:53:15 +01:00
|
|
|
|
2016-02-28 07:47:36 +01:00
|
|
|
def test_replace_pua_chars(self):
|
|
|
|
self.assertEqual('i', wolframalpha_api.replace_pua_chars(u'\uf74e'))
|
2015-12-30 07:53:15 +01:00
|
|
|
|
|
|
|
def test_response(self):
|
|
|
|
self.assertRaises(AttributeError, wolframalpha_api.response, None)
|
|
|
|
self.assertRaises(AttributeError, wolframalpha_api.response, [])
|
|
|
|
self.assertRaises(AttributeError, wolframalpha_api.response, '')
|
|
|
|
self.assertRaises(AttributeError, wolframalpha_api.response, '[]')
|
|
|
|
|
2016-02-28 07:47:36 +01:00
|
|
|
referer_url = 'referer_url'
|
|
|
|
request = Request(headers={'Referer': referer_url})
|
|
|
|
|
|
|
|
# test failure
|
2015-12-30 07:53:15 +01:00
|
|
|
xml = '''<?xml version='1.0' encoding='UTF-8'?>
|
|
|
|
<queryresult success='false' error='false' />
|
|
|
|
'''
|
2016-11-30 18:43:03 +01:00
|
|
|
response = mock.Mock(text=xml.encode('utf-8'))
|
2015-12-30 07:53:15 +01:00
|
|
|
self.assertEqual(wolframalpha_api.response(response), [])
|
|
|
|
|
2016-02-28 07:47:36 +01:00
|
|
|
# test basic case
|
2016-11-30 18:43:03 +01:00
|
|
|
xml = b"""<?xml version='1.0' encoding='UTF-8'?>
|
2016-01-02 07:41:14 +01:00
|
|
|
<queryresult success='true'
|
|
|
|
error='false'
|
2016-02-28 07:47:36 +01:00
|
|
|
numpods='3'
|
|
|
|
datatypes='Math'
|
|
|
|
id='queryresult_id'
|
|
|
|
host='http://www4c.wolframalpha.com'
|
|
|
|
related='related_url'
|
2016-01-02 07:41:14 +01:00
|
|
|
version='2.6'>
|
2016-02-28 07:47:36 +01:00
|
|
|
<pod title='Input'
|
|
|
|
scanner='Identity'
|
|
|
|
id='Input'
|
|
|
|
numsubpods='1'>
|
|
|
|
<subpod title=''>
|
|
|
|
<img src='input_img_src.gif'
|
|
|
|
alt='input_img_alt'
|
|
|
|
title='input_img_title' />
|
|
|
|
<plaintext>input_plaintext</plaintext>
|
|
|
|
</subpod>
|
|
|
|
</pod>
|
|
|
|
<pod title='Result'
|
|
|
|
scanner='Simplification'
|
|
|
|
id='Result'
|
2015-12-30 07:53:15 +01:00
|
|
|
numsubpods='1'
|
|
|
|
primary='true'>
|
2016-02-28 07:47:36 +01:00
|
|
|
<subpod title=''>
|
|
|
|
<img src='result_img_src.gif'
|
|
|
|
alt='result_img_alt'
|
|
|
|
title='result_img_title' />
|
|
|
|
<plaintext>result_plaintext</plaintext>
|
|
|
|
</subpod>
|
2015-12-30 07:53:15 +01:00
|
|
|
</pod>
|
2016-02-28 07:47:36 +01:00
|
|
|
<pod title='Manipulatives illustration'
|
|
|
|
scanner='Arithmetic'
|
|
|
|
id='Illustration'
|
|
|
|
numsubpods='1'>
|
|
|
|
<subpod title=''>
|
|
|
|
<img src='illustration_img_src.gif'
|
|
|
|
alt='illustration_img_alt' />
|
|
|
|
<plaintext>illustration_plaintext</plaintext>
|
|
|
|
</subpod>
|
2015-12-30 07:53:15 +01:00
|
|
|
</pod>
|
2016-02-28 07:47:36 +01:00
|
|
|
</queryresult>
|
2015-12-30 07:53:15 +01:00
|
|
|
"""
|
2016-11-30 18:43:03 +01:00
|
|
|
response = mock.Mock(text=xml, request=request)
|
2015-12-30 07:53:15 +01:00
|
|
|
results = wolframalpha_api.response(response)
|
|
|
|
self.assertEqual(type(results), list)
|
2016-01-06 04:47:31 +01:00
|
|
|
self.assertEqual(len(results), 2)
|
2016-02-28 09:05:52 +01:00
|
|
|
self.assertEqual('input_plaintext', results[0]['infobox'])
|
2016-02-28 07:47:36 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(results[0]['attributes']), 3)
|
2016-02-28 09:05:52 +01:00
|
|
|
self.assertEqual('Input', results[0]['attributes'][0]['label'])
|
|
|
|
self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
|
|
|
|
self.assertEqual('Result', results[0]['attributes'][1]['label'])
|
|
|
|
self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
|
|
|
|
self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
|
|
|
|
self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
|
|
|
|
self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
|
2016-02-28 07:47:36 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(results[0]['urls']), 1)
|
2016-01-04 02:57:37 +01:00
|
|
|
|
2016-02-28 07:47:36 +01:00
|
|
|
self.assertEqual(referer_url, results[0]['urls'][0]['url'])
|
|
|
|
self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
|
|
|
|
self.assertEqual(referer_url, results[1]['url'])
|
2016-09-11 00:04:24 +02:00
|
|
|
self.assertEqual('Wolfram|Alpha (input_plaintext)', results[1]['title'])
|
2016-07-07 00:29:40 +02:00
|
|
|
self.assertIn('result_plaintext', results[1]['content'])
|
2016-02-28 07:47:36 +01:00
|
|
|
|
|
|
|
# test calc
|
2016-11-30 18:43:03 +01:00
|
|
|
xml = b"""<?xml version='1.0' encoding='UTF-8'?>
|
2016-01-04 02:57:37 +01:00
|
|
|
<queryresult success='true'
|
|
|
|
error='false'
|
2016-02-28 07:47:36 +01:00
|
|
|
numpods='2'
|
|
|
|
datatypes=''
|
2016-01-04 02:57:37 +01:00
|
|
|
parsetimedout='false'
|
2016-02-28 07:47:36 +01:00
|
|
|
id='queryresult_id'
|
2016-01-06 04:47:31 +01:00
|
|
|
host='http://www5b.wolframalpha.com'
|
2016-02-28 07:47:36 +01:00
|
|
|
related='related_url'
|
|
|
|
version='2.6' >
|
|
|
|
<pod title='Indefinite integral'
|
|
|
|
scanner='Integral'
|
|
|
|
id='IndefiniteIntegral'
|
|
|
|
error='false'
|
|
|
|
numsubpods='1'
|
|
|
|
primary='true'>
|
|
|
|
<subpod title=''>
|
|
|
|
<img src='integral_image.gif'
|
|
|
|
alt='integral_img_alt'
|
|
|
|
title='integral_img_title' />
|
|
|
|
<plaintext>integral_plaintext</plaintext>
|
|
|
|
</subpod>
|
|
|
|
</pod>
|
2016-02-28 09:05:52 +01:00
|
|
|
<pod title='Plot of the integral'
|
|
|
|
scanner='Integral'
|
2016-02-28 07:47:36 +01:00
|
|
|
id='Plot'
|
|
|
|
error='false'
|
|
|
|
numsubpods='1'>
|
|
|
|
<subpod title=''>
|
|
|
|
<img src='plot.gif'
|
|
|
|
alt='plot_alt'
|
|
|
|
title='' />
|
|
|
|
<plaintext></plaintext>
|
|
|
|
</subpod>
|
|
|
|
</pod>
|
2016-01-04 02:57:37 +01:00
|
|
|
</queryresult>
|
|
|
|
"""
|
2016-11-30 18:43:03 +01:00
|
|
|
response = mock.Mock(text=xml, request=request)
|
2016-01-04 02:57:37 +01:00
|
|
|
results = wolframalpha_api.response(response)
|
|
|
|
self.assertEqual(type(results), list)
|
2016-02-28 07:47:36 +01:00
|
|
|
self.assertEqual(len(results), 2)
|
2016-02-28 09:05:52 +01:00
|
|
|
self.assertEqual('integral_plaintext', results[0]['infobox'])
|
2016-02-28 07:47:36 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(results[0]['attributes']), 2)
|
2016-02-28 09:05:52 +01:00
|
|
|
self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
|
|
|
|
self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
|
|
|
|
self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
|
|
|
|
self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
|
|
|
|
self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
|
2016-02-28 07:47:36 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(results[0]['urls']), 1)
|
|
|
|
|
|
|
|
self.assertEqual(referer_url, results[0]['urls'][0]['url'])
|
|
|
|
self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
|
|
|
|
self.assertEqual(referer_url, results[1]['url'])
|
2016-09-11 00:04:24 +02:00
|
|
|
self.assertEqual('Wolfram|Alpha (integral_plaintext)', results[1]['title'])
|
2016-07-07 00:29:40 +02:00
|
|
|
self.assertIn('integral_plaintext', results[1]['content'])
|