historical: Add main code for historical subcommand.

This commit is contained in:
Brett Smith 2017-05-17 08:48:40 -04:00
parent 9d47238245
commit 667c214e91
3 changed files with 117 additions and 0 deletions

View file

View file

@ -0,0 +1,25 @@
import oxrlib.rate
def format_one_rate(rate, from_amt, from_curr, to_curr):
return "{:g} {} = {:g} {}".format(
from_amt, from_curr, rate.convert(from_amt, from_curr, to_curr), to_curr)
def format_rate_pair(rate, from_curr, to_curr):
yield format_one_rate(rate, 1, from_curr, to_curr)
yield format_one_rate(rate, 1, to_curr, from_curr)
def run(config, stdout, stderr):
loaders = config.get_loaders()
with loaders.historical(config.args.date, config.args.base) as rate_json:
rate = oxrlib.rate.Rate.from_json_file(rate_json)
if not config.args.from_currency:
for from_curr in sorted(rate.rates):
print(*format_rate_pair(rate, from_curr, config.args.to_currency),
sep='\n', file=stdout)
elif config.args.amount is None:
print(*format_rate_pair(rate, config.args.from_currency, config.args.to_currency),
sep='\n', file=stdout)
else:
print(format_one_rate(rate, config.args.amount,
config.args.from_currency, config.args.to_currency),
file=stdout)

92
tests/test_historical.py Normal file
View file

@ -0,0 +1,92 @@
import argparse
import decimal
import io
import json
import pytest
from . import any_date, relpath
import oxrlib.commands.historical as oxrhist
class FakeResponder:
def __init__(self, response_path):
self.response_path = response_path
def _respond(self, *args, **kwargs):
return open(self.response_path)
def __getattr__(self, name):
return self._respond
class FakeConfig:
def __init__(self, responder, argvars=None):
self.responder = responder
self.args = argparse.Namespace()
if argvars is not None:
for key in argvars:
setattr(self.args, key, argvars[key])
def get_loaders(self):
return self.responder
output = pytest.fixture(lambda: io.StringIO())
@pytest.fixture
def historical1_responder():
return FakeResponder(relpath('historical1.json').as_posix())
def build_config(
responder,
date=None,
amount=None,
from_currency=None,
to_currency=None,
base='USD',
):
return FakeConfig(responder, {
'date': any_date() if date is None else date,
'base': base,
'amount': None if amount is None else decimal.Decimal(amount),
'from_currency': from_currency,
'to_currency': base if to_currency is None else to_currency,
})
def lines_from_run(config, output):
oxrhist.run(config, output, output)
output.seek(0)
return iter(output)
def test_rate_list(historical1_responder, output):
config = build_config(historical1_responder)
lines = lines_from_run(config, output)
assert next(lines).startswith('1 AED = 0.27229')
assert next(lines) == '1 USD = 3.67246 AED\n'
assert next(lines).startswith('1 ALL = 0.0069189')
assert next(lines) == '1 USD = 144.529793 ALL\n'
assert next(lines).startswith('1 ANG = 0.55865')
assert next(lines) == '1 USD = 1.79 ANG\n'
def test_one_rate(historical1_responder, output):
config = build_config(historical1_responder, from_currency='ANG')
lines = lines_from_run(config, output)
assert next(lines).startswith('1 ANG = 0.55865')
assert next(lines) == '1 USD = 1.79 ANG\n'
assert next(lines, None) is None
def test_conversion(historical1_responder, output):
config = build_config(historical1_responder, amount=10, from_currency='AED')
lines = lines_from_run(config, output)
# FIXME: Assertion probably changes after we deal with precision right.
assert next(lines).startswith('10 AED = 2.72297')
assert next(lines, None) is None
def test_back_conversion(historical1_responder, output):
config = build_config(historical1_responder,
amount=2, from_currency='USD', to_currency='ALL')
lines = lines_from_run(config, output)
# FIXME: Assertion probably changes after we deal with precision right.
assert next(lines) == '2 USD = 289.059586 ALL\n'
assert next(lines, None) is None