2017-05-17 12:48:40 +00:00
|
|
|
import argparse
|
|
|
|
import decimal
|
|
|
|
import io
|
2020-05-17 16:13:10 +00:00
|
|
|
import itertools
|
2017-05-17 12:48:40 +00:00
|
|
|
import json
|
2020-05-17 14:44:03 +00:00
|
|
|
import re
|
2017-05-17 12:48:40 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import any_date, relpath
|
|
|
|
|
|
|
|
import oxrlib.commands.historical as oxrhist
|
|
|
|
|
|
|
|
class FakeResponder:
|
2020-05-17 16:13:10 +00:00
|
|
|
def __init__(self, *response_paths):
|
|
|
|
self.paths = itertools.cycle(response_paths)
|
2017-05-17 12:48:40 +00:00
|
|
|
|
|
|
|
def _respond(self, *args, **kwargs):
|
2020-05-17 16:13:10 +00:00
|
|
|
return next(self.paths).open()
|
2017-05-17 12:48:40 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return self._respond
|
|
|
|
|
2017-05-17 16:56:19 +00:00
|
|
|
def should_cache(self):
|
|
|
|
return False
|
|
|
|
|
2017-05-17 12:48:40 +00:00
|
|
|
|
|
|
|
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())
|
2020-05-17 16:54:05 +00:00
|
|
|
parametrize_format = pytest.mark.parametrize('output_format', [
|
|
|
|
oxrhist.Formats.LEDGER,
|
2020-05-17 18:01:40 +00:00
|
|
|
oxrhist.Formats.BEANCOUNT,
|
2020-05-17 16:54:05 +00:00
|
|
|
])
|
2017-05-17 12:48:40 +00:00
|
|
|
|
2020-05-17 16:13:10 +00:00
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def single_responder():
|
|
|
|
return FakeResponder(relpath('historical1.json'))
|
|
|
|
|
2017-05-17 12:48:40 +00:00
|
|
|
@pytest.fixture
|
2020-05-17 16:13:10 +00:00
|
|
|
def alternate_responder():
|
|
|
|
return FakeResponder(
|
|
|
|
relpath('historical1.json'),
|
|
|
|
relpath('historical2.json'),
|
|
|
|
)
|
2017-05-17 12:48:40 +00:00
|
|
|
|
|
|
|
def build_config(
|
|
|
|
responder,
|
2020-05-06 18:48:27 +00:00
|
|
|
date,
|
2017-05-17 12:48:40 +00:00
|
|
|
amount=None,
|
|
|
|
from_currency=None,
|
|
|
|
to_currency=None,
|
2020-05-17 16:13:10 +00:00
|
|
|
from_date=None,
|
2020-05-17 16:54:05 +00:00
|
|
|
output_format=oxrhist.Formats.RAW,
|
2017-05-18 17:47:46 +00:00
|
|
|
signed_currencies=None,
|
2017-06-09 15:06:51 +00:00
|
|
|
denomination=None,
|
2017-05-17 12:48:40 +00:00
|
|
|
base='USD',
|
|
|
|
):
|
|
|
|
return FakeConfig(responder, {
|
2020-05-06 18:48:27 +00:00
|
|
|
'date': date,
|
2017-05-17 12:48:40 +00:00
|
|
|
'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,
|
2020-05-17 16:13:10 +00:00
|
|
|
'from_date': from_date,
|
2020-05-17 16:54:05 +00:00
|
|
|
'output_format': output_format,
|
2017-05-18 17:47:46 +00:00
|
|
|
'signed_currencies': [base] if signed_currencies is None else signed_currencies,
|
2017-06-09 15:06:51 +00:00
|
|
|
'denomination': denomination,
|
2017-05-17 12:48:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
def lines_from_run(config, output):
|
|
|
|
oxrhist.run(config, output, output)
|
|
|
|
output.seek(0)
|
|
|
|
return iter(output)
|
|
|
|
|
2020-05-17 14:44:03 +00:00
|
|
|
def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=None):
|
|
|
|
if price is None:
|
|
|
|
price = cost
|
2020-05-17 18:01:40 +00:00
|
|
|
rate_fmt = f'{{}} {re.escape(fx_code)}'
|
2020-05-17 14:44:03 +00:00
|
|
|
cost = re.escape(cost) + r'\d*'
|
|
|
|
price = re.escape(price) + r'\d*'
|
2020-05-17 18:01:40 +00:00
|
|
|
if config.args.output_format is oxrhist.Formats.LEDGER:
|
|
|
|
if fx_sign is not None and fx_code in config.args.signed_currencies:
|
|
|
|
rate_fmt = f'{re.escape(fx_sign)}{{}}'
|
|
|
|
cost_re = '{{={}}}'.format(rate_fmt.format(cost))
|
|
|
|
price_re = ' @ {}'.format(rate_fmt.format(price))
|
2020-05-17 14:44:03 +00:00
|
|
|
else:
|
2020-05-17 18:01:40 +00:00
|
|
|
amount = amount.replace(',', '')
|
|
|
|
cost_re = '{{{}}}'.format(rate_fmt.format(cost))
|
|
|
|
if config.args.from_date is None:
|
|
|
|
price_re = ''
|
|
|
|
else:
|
|
|
|
price_re = ' @ {}'.format(rate_fmt.format(price))
|
|
|
|
pattern = r'^{} {}{}$'.format(re.escape(amount), cost_re, price_re)
|
2020-05-17 14:44:03 +00:00
|
|
|
line = next(lines, "<EOF>")
|
|
|
|
assert re.match(pattern, line)
|
|
|
|
|
2020-05-17 18:01:40 +00:00
|
|
|
def check_nonfx_amount(config, lines, amount, code=None, sign=None):
|
|
|
|
if config.args.output_format is oxrhist.Formats.LEDGER:
|
|
|
|
if code is None:
|
|
|
|
code = 'USD'
|
|
|
|
sign = '$'
|
|
|
|
if code in config.args.signed_currencies and sign is not None:
|
|
|
|
expected = f'{sign}{amount}\n'
|
|
|
|
else:
|
|
|
|
expected = f'{amount} {code}\n'
|
|
|
|
else:
|
|
|
|
expected = f'{amount.replace(",", "")} {code or "USD"}\n'
|
|
|
|
assert next(lines, "<EOF>") == expected
|
|
|
|
|
2020-05-17 16:13:10 +00:00
|
|
|
def test_rate_list(single_responder, output, any_date):
|
|
|
|
config = build_config(single_responder, any_date)
|
2017-05-17 12:48:40 +00:00
|
|
|
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'
|
|
|
|
|
2020-05-17 16:13:10 +00:00
|
|
|
def test_one_rate(single_responder, output, any_date):
|
|
|
|
config = build_config(single_responder, any_date, from_currency='ANG')
|
2017-05-17 12:48:40 +00:00
|
|
|
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
|
|
|
|
|
2020-05-17 16:13:10 +00:00
|
|
|
def test_conversion(single_responder, output, any_date):
|
|
|
|
config = build_config(single_responder, any_date, amount=10, from_currency='AED')
|
2017-05-17 12:48:40 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2017-05-17 21:38:45 +00:00
|
|
|
assert next(lines) == '10.00 AED = 2.72 USD\n'
|
2017-05-17 12:48:40 +00:00
|
|
|
assert next(lines, None) is None
|
|
|
|
|
2020-05-17 16:13:10 +00:00
|
|
|
def test_back_conversion(single_responder, output, any_date):
|
|
|
|
config = build_config(single_responder, any_date,
|
2017-05-17 12:48:40 +00:00
|
|
|
amount=2, from_currency='USD', to_currency='ALL')
|
|
|
|
lines = lines_from_run(config, output)
|
2017-05-17 21:38:45 +00:00
|
|
|
assert next(lines) == '2.00 USD = 289 ALL\n'
|
2017-05-17 12:48:40 +00:00
|
|
|
assert next(lines, None) is None
|
2017-05-18 17:47:46 +00:00
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_ledger_rate(single_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(single_responder, any_date,
|
2020-05-17 16:54:05 +00:00
|
|
|
from_currency='ANG', output_format=output_format)
|
2017-05-18 17:47:46 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '1 ANG', '0.5586', 'USD', '$')
|
|
|
|
check_fx_amount(config, lines, '1 USD', '1.79', 'ANG')
|
2017-05-18 17:47:46 +00:00
|
|
|
assert next(lines, None) is None
|
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_ledger_conversion(single_responder, output, any_date, output_format):
|
|
|
|
config = build_config(single_responder, any_date, from_currency='ALL',
|
|
|
|
amount=300, output_format=output_format)
|
2017-05-18 17:47:46 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '300 ALL', '0.00691', 'USD', '$')
|
2020-05-17 18:01:40 +00:00
|
|
|
check_nonfx_amount(config, lines, '2.08')
|
2017-05-18 17:47:46 +00:00
|
|
|
assert next(lines, None) is None
|
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_signed_currencies(single_responder, output, any_date, output_format):
|
|
|
|
config = build_config(single_responder, any_date, from_currency='AED',
|
|
|
|
output_format=output_format, signed_currencies=['EUR'])
|
2017-05-18 17:47:46 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '1 AED', '0.272', 'USD', '$')
|
|
|
|
check_fx_amount(config, lines, '1 USD', '3.672', 'AED')
|
2017-05-18 17:47:46 +00:00
|
|
|
assert next(lines, None) is None
|
2017-06-09 15:06:51 +00:00
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_denomination(single_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(single_responder, any_date,
|
2020-05-06 18:48:27 +00:00
|
|
|
from_currency='ANG', to_currency='AED', amount=10,
|
2020-05-17 16:54:05 +00:00
|
|
|
output_format=output_format, denomination='USD')
|
2017-06-09 15:06:51 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$')
|
|
|
|
check_fx_amount(config, lines, '20.52 AED', '0.272', 'USD', '$')
|
2017-06-09 15:06:51 +00:00
|
|
|
assert next(lines, None) is None
|
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_redundant_denomination(single_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(single_responder, any_date,
|
2020-05-06 18:48:27 +00:00
|
|
|
from_currency='ANG', to_currency='USD', amount=10,
|
2020-05-17 16:54:05 +00:00
|
|
|
output_format=output_format, denomination='USD')
|
2017-06-09 15:06:51 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$')
|
2020-05-17 18:01:40 +00:00
|
|
|
check_nonfx_amount(config, lines, '5.59')
|
2017-06-09 15:06:51 +00:00
|
|
|
assert next(lines, None) is None
|
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_from_denomination(single_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(single_responder, any_date,
|
2020-05-06 18:48:27 +00:00
|
|
|
from_currency='USD', to_currency='ALL', amount=10,
|
2020-05-17 16:54:05 +00:00
|
|
|
output_format=output_format, denomination='USD')
|
2017-06-09 15:06:51 +00:00
|
|
|
lines = lines_from_run(config, output)
|
2020-05-17 18:01:40 +00:00
|
|
|
check_nonfx_amount(config, lines, '10.00')
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '1,445 ALL', '0.00691', 'USD', '$')
|
2017-06-09 15:06:51 +00:00
|
|
|
assert next(lines, None) is None
|
2017-06-29 20:54:16 +00:00
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_rate_precision_added_as_needed(single_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(single_responder, any_date,
|
2020-05-06 18:48:27 +00:00
|
|
|
from_currency='RUB', to_currency='USD', amount=63805,
|
2020-05-17 16:54:05 +00:00
|
|
|
output_format=output_format, denomination='USD')
|
2017-06-29 20:54:16 +00:00
|
|
|
lines = lines_from_run(config, output)
|
|
|
|
# 63,805 / 57.0763 (the RUB rate) == $1,117.89
|
|
|
|
# But using the truncated rate: 63,805 * .01752 == $1,117.86
|
|
|
|
# Make sure the rate is specified with enough precision to get the
|
|
|
|
# correct conversion amount.
|
2020-05-17 14:44:03 +00:00
|
|
|
check_fx_amount(config, lines, '63,805.00 RUB', '0.0175204', 'USD', '$')
|
2020-05-17 18:01:40 +00:00
|
|
|
check_nonfx_amount(config, lines, '1,117.89')
|
2017-06-29 20:54:16 +00:00
|
|
|
assert next(lines, None) is None
|
2020-05-17 16:13:10 +00:00
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_from_date_rates(alternate_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(alternate_responder, any_date,
|
|
|
|
from_currency='ANG', to_currency='AED',
|
2020-05-17 16:54:05 +00:00
|
|
|
from_date=any_date, output_format=output_format,
|
|
|
|
denomination='USD')
|
2020-05-17 16:13:10 +00:00
|
|
|
lines = lines_from_run(config, output)
|
|
|
|
check_fx_amount(config, lines, '1 ANG', '2.051', 'AED', None, '1.909')
|
|
|
|
check_fx_amount(config, lines, '1 AED', '0.487', 'ANG', None, '0.523')
|
|
|
|
assert next(lines, None) is None
|
|
|
|
|
2020-05-17 16:54:05 +00:00
|
|
|
@parametrize_format
|
|
|
|
def test_from_date_conversion(alternate_responder, output, any_date, output_format):
|
2020-05-17 16:13:10 +00:00
|
|
|
config = build_config(alternate_responder, any_date,
|
|
|
|
from_currency='ANG', to_currency='AED', amount=10,
|
2020-05-17 16:54:05 +00:00
|
|
|
from_date=any_date, output_format=output_format,
|
|
|
|
denomination='USD')
|
2020-05-17 16:13:10 +00:00
|
|
|
lines = lines_from_run(config, output)
|
|
|
|
check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$', '0.507')
|
|
|
|
check_fx_amount(config, lines, '20.52 AED', '0.272', 'USD', '$', '0.265')
|
|
|
|
assert next(lines, None) is None
|