[fix] currency_convert: make unit tests pass

also make regex more efficent and normalize 's'es
This commit is contained in:
Maxqia 2016-05-02 17:06:17 -07:00
parent dcc8f996d4
commit 56e53c9297
2 changed files with 10 additions and 13 deletions

View file

@ -9,13 +9,13 @@ categories = []
url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X' url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
weight = 100 weight = 100
parser_re = re.compile(u'.*?(\d+(?:\.\d+)?) ([^.0-9].+) (in|to) ([^\.]+)\W*$', re.I) # noqa parser_re = re.compile(u'.*?(\d+(?:\.\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I) # noqa
db = 1 db = 1
def normalize_name(name): def normalize_name(name):
name = name.lower().replace('-', ' ') name = name.lower().replace('-', ' ').rstrip('s')
name = re.sub(' +', ' ', name) name = re.sub(' +', ' ', name)
return unicodedata.normalize('NFKD', name).lower() return unicodedata.normalize('NFKD', name).lower()
@ -40,7 +40,7 @@ def request(query, params):
# wrong query # wrong query
return params return params
ammount, from_currency, none, to_currency = m.groups() ammount, from_currency, to_currency = m.groups()
ammount = float(ammount) ammount = float(ammount)
from_currency = name_to_iso4217(from_currency.strip()) from_currency = name_to_iso4217(from_currency.strip())
to_currency = name_to_iso4217(to_currency.strip()) to_currency = name_to_iso4217(to_currency.strip())

View file

@ -14,23 +14,19 @@ class TestCurrencyConvertEngine(SearxTestCase):
params = currency_convert.request(query, dicto) params = currency_convert.request(query, dicto)
self.assertNotIn('url', params) self.assertNotIn('url', params)
query = '1.1.1 EUR in USD' query = 'convert 10 Pound Sterlings to United States Dollars'
params = currency_convert.request(query, dicto)
self.assertNotIn('url', params)
query = '10 eur in usd'
params = currency_convert.request(query, dicto) params = currency_convert.request(query, dicto)
self.assertIn('url', params) self.assertIn('url', params)
self.assertIn('finance.yahoo.com', params['url']) self.assertIn('finance.yahoo.com', params['url'])
self.assertIn('EUR', params['url']) self.assertIn('GBP', params['url'])
self.assertIn('USD', params['url']) self.assertIn('USD', params['url'])
def test_response(self): def test_response(self):
dicto = defaultdict(dict) dicto = defaultdict(dict)
dicto['ammount'] = float(10) dicto['ammount'] = float(10)
dicto['from'] = "EUR" dicto['from'] = "GBP"
dicto['to'] = "USD" dicto['to'] = "USD"
dicto['from_name'] = "euro" dicto['from_name'] = "pound sterling"
dicto['to_name'] = "United States dollar" dicto['to_name'] = "United States dollar"
response = mock.Mock(text='a,b,c,d', search_params=dicto) response = mock.Mock(text='a,b,c,d', search_params=dicto)
self.assertEqual(currency_convert.response(response), []) self.assertEqual(currency_convert.response(response), [])
@ -40,7 +36,8 @@ class TestCurrencyConvertEngine(SearxTestCase):
results = currency_convert.response(response) results = currency_convert.response(response)
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 1) self.assertEqual(len(results), 1)
self.assertEqual(results[0]['answer'], '10.0 EUR = 5.0 USD, 1 EUR (euro) = 0.5 USD (United States dollar)') self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
' = 0.5 USD (United States dollar)')
now_date = datetime.now().strftime('%Y%m%d') now_date = datetime.now().strftime('%Y%m%d')
self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' + self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' +
now_date + '/10.0-eur-to-usd.html') now_date + '/10.0-gbp-to-usd.html')