# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from requests import Request
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)
# TODO: test api_key
self.assertIn('url', params)
self.assertIn('https://api.wolframalpha.com/v2/query?', params['url'])
self.assertIn(query, params['url'])
self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer'])
def test_replace_pua_chars(self):
self.assertEqual('i', wolframalpha_api.replace_pua_chars(u'\uf74e'))
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, '[]')
referer_url = 'referer_url'
request = Request(headers={'Referer': referer_url})
# test failure
xml = '''
'''
response = mock.Mock(content=xml)
self.assertEqual(wolframalpha_api.response(response), [])
# test basic case
xml = """
input_plaintext
result_plaintext
illustration_plaintext
"""
response = mock.Mock(content=xml, request=request)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual('input_plaintext', results[0]['infobox'])
self.assertEqual(len(results[0]['attributes']), 3)
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'])
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'])
self.assertEqual('Wolfram|Alpha', results[1]['title'])
# test calc
xml = """
integral_plaintext
"""
response = mock.Mock(content=xml, request=request)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertEqual('integral_plaintext', results[0]['infobox'])
self.assertEqual(len(results[0]['attributes']), 2)
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'])
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'])
self.assertEqual('Wolfram|Alpha', results[1]['title'])