test_currency_convert.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. self.assertFalse(currency_convert.is_accepted(query, dicto))
  12. query = b'convert 10 Pound Sterlings to United States Dollars'
  13. self.assertTrue(currency_convert.is_accepted(query, dicto))
  14. params = currency_convert.request(query, dicto)
  15. self.assertIn('url', params)
  16. self.assertIn('finance.yahoo.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['ammount'] = 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. csv = "2,0.5,1"
  29. response = mock.Mock(text=csv, 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. now_date = datetime.now().strftime('%Y%m%d')
  36. self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' +
  37. now_date + '/10.0-gbp-to-usd.html')