# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import wolframalpha_api
from searx.testing import SearxTestCase
class TestWolframAlphaAPIEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
api_key = 'XXXXXX-XXXXXXXXXX'
dicto = defaultdict(dict)
dicto['api_key'] = api_key
params = wolframalpha_api.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('wolframalpha.com', params['url'])
self.assertIn('api_key', params)
self.assertIn(api_key, params['api_key'])
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, '[]')
xml = '''
'''
# test failure
response = mock.Mock(content=xml)
self.assertEqual(wolframalpha_api.response(response), [])
xml = """
sqrt(-1)
r1 (radius), θ90° (angle)
(principal root)
-
"""
# test private user area char in response
response = mock.Mock(content=xml)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertIn('i', results[0]['answer'])
xml = """
∫1/xxlog(x)+constant
"""
# test integral
response = mock.Mock(content=xml)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertIn('log(x)+c', results[0]['answer'])
xml = """
solve x^2+x = 0
x = -1
x = 0
"""
# test ecuation with multiple answers
response = mock.Mock(content=xml)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 2)
self.assertIn('x = -1', results[0]['answer'])
self.assertIn('x = 0', results[1]['answer'])