test_currency_convert.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = currency_convert.request(query, dicto)
  12. self.assertNotIn('url', params)
  13. query = '1.1.1 EUR in USD'
  14. params = currency_convert.request(query, dicto)
  15. self.assertNotIn('url', params)
  16. query = '10 eur in usd'
  17. params = currency_convert.request(query, dicto)
  18. self.assertIn('url', params)
  19. self.assertIn('finance.yahoo.com', params['url'])
  20. self.assertIn('EUR', params['url'])
  21. self.assertIn('USD', params['url'])
  22. def test_response(self):
  23. dicto = defaultdict(dict)
  24. dicto['ammount'] = float(10)
  25. dicto['from'] = "EUR"
  26. dicto['to'] = "USD"
  27. dicto['from_name'] = "euro"
  28. dicto['to_name'] = "United States dollar"
  29. response = mock.Mock(text='a,b,c,d', search_params=dicto)
  30. self.assertEqual(currency_convert.response(response), [])
  31. csv = "2,0.5,1"
  32. response = mock.Mock(text=csv, search_params=dicto)
  33. results = currency_convert.response(response)
  34. self.assertEqual(type(results), list)
  35. self.assertEqual(len(results), 1)
  36. self.assertEqual(results[0]['answer'], '10.0 EUR = 5.0 USD, 1 EUR (euro) = 0.5 USD (United States dollar)')
  37. now_date = datetime.now().strftime('%Y%m%d')
  38. self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' +
  39. now_date + '/10.0-eur-to-usd.html')