# -*- 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)
r1 (radius), θ90° (angle)
(principal root)
-
"""
response = mock.Mock(content=xml)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
# self.assertEqual(len(results), 2)
self.assertEqual(len(results), 1)
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 = """
∫1/xxlog(x)+constant
"""
response = mock.Mock(content=xml)
results = wolframalpha_api.response(response)
self.assertEqual(type(results), list)
# self.assertEqual(len(results), 2)
self.assertEqual(len(results), 1)
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'])