# -*- 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 = '''
'''
response = mock.Mock(content=xml)
self.assertEqual(wolframalpha_api.response(response), [])
xml = """
"""
response = mock.Mock(content=xml)
self.assertEqual(wolframalpha_api.response(response), [])
xml = """
sqrt(-1)
i
r = 1 (radius), theta = 90° (angle)
i (principal root)
-i
"""
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 = """
integral 1/x dx = log(x)+constant
"""
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'])