tests: Historical tests use more flexible rate matching.

This lets the tests be more flexible about how much precision is used in
rates when appropriate, and makes them ready to parametrize for
Beancount.
This commit is contained in:
Brett Smith 2020-05-17 10:44:03 -04:00
parent fb2114896d
commit 71893ace4d

View file

@ -2,6 +2,7 @@ import argparse
import decimal
import io
import json
import re
import pytest
@ -68,6 +69,23 @@ def lines_from_run(config, output):
output.seek(0)
return iter(output)
def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=None):
if price is None:
price = cost
cost = re.escape(cost) + r'\d*'
price = re.escape(price) + r'\d*'
if fx_sign is not None and fx_code in config.args.signed_currencies:
rate_fmt = f'{re.escape(fx_sign)}{{}}'
else:
rate_fmt = f'{{}} {re.escape(fx_code)}'
pattern = r'^{} {{={}}} @ {}$'.format(
re.escape(amount),
rate_fmt.format(cost),
rate_fmt.format(price),
)
line = next(lines, "<EOF>")
assert re.match(pattern, line)
def test_rate_list(historical1_responder, output, any_date):
config = build_config(historical1_responder, any_date)
lines = lines_from_run(config, output)
@ -102,15 +120,15 @@ def test_ledger_rate(historical1_responder, output, any_date):
config = build_config(historical1_responder, any_date,
from_currency='ANG', ledger=True)
lines = lines_from_run(config, output)
assert next(lines) == '1 ANG {=$0.55866} @ $0.55866\n'
assert next(lines) == '1 USD {=1.79 ANG} @ 1.79 ANG\n'
check_fx_amount(config, lines, '1 ANG', '0.5586', 'USD', '$')
check_fx_amount(config, lines, '1 USD', '1.79', 'ANG')
assert next(lines, None) is None
def test_ledger_conversion(historical1_responder, output, any_date):
config = build_config(historical1_responder, any_date,
from_currency='ALL', amount=300, ledger=True)
lines = lines_from_run(config, output)
assert next(lines) == '300 ALL {=$0.006919} @ $0.006919\n'
check_fx_amount(config, lines, '300 ALL', '0.00691', 'USD', '$')
assert next(lines) == '$2.08\n'
assert next(lines, None) is None
@ -118,8 +136,8 @@ def test_signed_currencies(historical1_responder, output, any_date):
config = build_config(historical1_responder, any_date,
from_currency='AED', ledger=True, signed_currencies=['EUR'])
lines = lines_from_run(config, output)
assert next(lines) == '1 AED {=0.2723 USD} @ 0.2723 USD\n'
assert next(lines) == '1 USD {=3.67246 AED} @ 3.67246 AED\n'
check_fx_amount(config, lines, '1 AED', '0.272', 'USD', '$')
check_fx_amount(config, lines, '1 USD', '3.672', 'AED')
assert next(lines, None) is None
def test_denomination(historical1_responder, output, any_date):
@ -127,8 +145,8 @@ def test_denomination(historical1_responder, output, any_date):
from_currency='ANG', to_currency='AED', amount=10,
ledger=True, denomination='USD')
lines = lines_from_run(config, output)
assert next(lines) == '10.00 ANG {=$0.55866} @ $0.55866\n'
assert next(lines) == '20.52 AED {=$0.2723} @ $0.2723\n'
check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$')
check_fx_amount(config, lines, '20.52 AED', '0.272', 'USD', '$')
assert next(lines, None) is None
def test_redundant_denomination(historical1_responder, output, any_date):
@ -136,7 +154,7 @@ def test_redundant_denomination(historical1_responder, output, any_date):
from_currency='ANG', to_currency='USD', amount=10,
ledger=True, denomination='USD')
lines = lines_from_run(config, output)
assert next(lines) == '10.00 ANG {=$0.55866} @ $0.55866\n'
check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$')
assert next(lines) == '$5.59\n'
assert next(lines, None) is None
@ -146,7 +164,7 @@ def test_from_denomination(historical1_responder, output, any_date):
ledger=True, denomination='USD')
lines = lines_from_run(config, output)
assert next(lines) == '$10.00\n'
assert next(lines) == '1,445 ALL {=$0.006919} @ $0.006919\n'
check_fx_amount(config, lines, '1,445 ALL', '0.00691', 'USD', '$')
assert next(lines, None) is None
def test_rate_precision_added_as_needed(historical1_responder, output, any_date):
@ -158,6 +176,6 @@ def test_rate_precision_added_as_needed(historical1_responder, output, any_date)
# 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.
assert next(lines) == '63,805.00 RUB {=$0.0175204} @ $0.0175204\n'
check_fx_amount(config, lines, '63,805.00 RUB', '0.0175204', 'USD', '$')
assert next(lines) == '$1,117.89\n'
assert next(lines, None) is None