test_currency_convert.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from collections import defaultdict
  2. from datetime import datetime
  3. import mock
  4. from searx.engines import currency_convert
  5. from searx.testing import SearxTestCase
  6. class TestCurrencyConvertEngine(SearxTestCase):
  7. def test_request(self):
  8. query = b'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = currency_convert.request(query, dicto)
  12. self.assertNotIn('url', params)
  13. query = b'convert 10 Pound Sterlings to United States Dollars'
  14. params = currency_convert.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn('duckduckgo.com', params['url'])
  17. self.assertIn('GBP', params['url'])
  18. self.assertIn('USD', params['url'])
  19. def test_response(self):
  20. dicto = defaultdict(dict)
  21. dicto['amount'] = float(10)
  22. dicto['from'] = "GBP"
  23. dicto['to'] = "USD"
  24. dicto['from_name'] = "pound sterling"
  25. dicto['to_name'] = "United States dollar"
  26. response = mock.Mock(text='a,b,c,d', search_params=dicto)
  27. self.assertEqual(currency_convert.response(response), [])
  28. body = """ddg_spice_currency(
  29. {
  30. "conversion":{
  31. "converted-amount": "0.5"
  32. },
  33. "topConversions":[
  34. {
  35. },
  36. {
  37. }
  38. ]
  39. }
  40. );
  41. """
  42. response = mock.Mock(text=body, search_params=dicto)
  43. results = currency_convert.response(response)
  44. self.assertEqual(type(results), list)
  45. self.assertEqual(len(results), 1)
  46. self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
  47. ' = 0.5 USD (United States dollar)')
  48. target_url = 'https://duckduckgo.com/js/spice/currency/1/{}/{}'.format(
  49. dicto['from'], dicto['to'])
  50. self.assertEqual(results[0]['url'], target_url)