test_currency_convert.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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('finance.google.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 = "<span class=bld>0.5 {}</span>".format(dicto['to'])
  29. response = mock.Mock(text=body, search_params=dicto)
  30. results = currency_convert.response(response)
  31. self.assertEqual(type(results), list)
  32. self.assertEqual(len(results), 1)
  33. self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
  34. ' = 0.5 USD (United States dollar)')
  35. target_url = 'https://finance.google.com/finance?q={}{}'.format(
  36. dicto['from'], dicto['to'])
  37. self.assertEqual(results[0]['url'], target_url)