historical: Always format rates with the same precision.

When we format a rate as a price, we don't know how much precision
is "enough" to do the conversion, because we don't know what's
being converted to. As a result, we may (=will almost certainly)
end up formatting the rate with different precision on the cost
date vs. the price date, and that causes Beancount/Ledger to fail
to make the connection between them.

Using a constant of 6 is enough to make the current test for
"enough" precision pass, so just do that for now. This might need
further refinement in the future.
This commit is contained in:
Brett Smith 2020-05-19 15:56:19 -04:00
parent 8dede9d139
commit ae3e4617d3
3 changed files with 32 additions and 27 deletions

View file

@ -14,7 +14,7 @@ except ImportError:
class Formatter: class Formatter:
def __init__(self, cost_rates, price_rates=None, def __init__(self, cost_rates, price_rates=None,
signed_currencies=(), base_fmt='#,##0.###', signed_currencies=(), base_fmt='#,##0.###',
rate_precision=5, denomination=None): rate_precision=6, denomination=None):
self.cost_rates = cost_rates self.cost_rates = cost_rates
self.price_rates = price_rates self.price_rates = price_rates
self.base_fmt = base_fmt self.base_fmt = base_fmt
@ -95,27 +95,13 @@ class BeancountFormatter(Formatter):
qrate = rate qrate = rate
return qrate.normalize() return qrate.normalize()
def normalize_enough(self, rate, curr, from_amt, to_amt, prec=None): def _pretty_rate(self, fmt, rate, curr):
if prec is None: rate_s = self.format_currency(
prec = self.rate_prec self.normalize_rate(rate),
# Starting from prec, find the least amount of precision to curr,
# make sure from_amt converts exactly to to_amt. currency_digits=False,
for try_prec in itertools.count(prec): )
try_rate = self.normalize_rate(rate, try_prec) return fmt.format(rate_s)
got_amt = self.currency_decimal(from_amt * try_rate, curr)
# If got_amt == to_amt, this is enough precision to do the
# conversion exactly, so we're done.
# If try_rate == rate, there's no more precision available, so stop.
if (got_amt == to_amt) or (try_rate == rate):
break
return try_rate
def _pretty_rate(self, fmt, rate, curr, from_amt=None, to_amt=None):
if to_amt is None:
rate = self.normalize_rate(rate)
else:
rate = self.normalize_enough(rate, curr, from_amt, to_amt)
return fmt.format(self.format_currency(rate, curr, currency_digits=False))
def format_rate_pair(self, from_curr, to_curr): def format_rate_pair(self, from_curr, to_curr):
from_amt = 1 from_amt = 1
@ -151,12 +137,10 @@ class BeancountFormatter(Formatter):
if price is None: if price is None:
price_s = '' price_s = ''
else: else:
price_s = self._pretty_rate( price_s = self._pretty_rate(self.PRICE_FMT, price, denomination)
self.PRICE_FMT, price, denomination, amount, to_amt,
)
return "{} {}{}".format( return "{} {}{}".format(
amt_s, amt_s,
self._pretty_rate(self.COST_FMT, cost, denomination, amount, to_amt), self._pretty_rate(self.COST_FMT, cost, denomination),
price_s, price_s,
) )

View file

@ -5,7 +5,7 @@ from setuptools import setup
setup( setup(
name='oxrlib', name='oxrlib',
description="Library to query the Open Exchange Rates (OXR) API", description="Library to query the Open Exchange Rates (OXR) API",
version='2.0', version='2.1',
author='Brett Smith', author='Brett Smith',
author_email='brettcsmith@brettcsmith.org', author_email='brettcsmith@brettcsmith.org',
license='GNU AGPLv3+', license='GNU AGPLv3+',

View file

@ -239,3 +239,24 @@ def test_from_date_conversion(alternate_responder, output, any_date, output_form
check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$', '0.507') 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') check_fx_amount(config, lines, '20.52 AED', '0.272', 'USD', '$', '0.265')
assert next(lines, None) is None assert next(lines, None) is None
@parametrize_format
def test_rate_consistent_as_cost_and_price(alternate_responder, any_date, output_format):
config_kwargs = {
'responder': alternate_responder,
'amount': 65000,
'from_currency': 'RUB',
'output_format': output_format,
'signed_currencies': (),
}
config = build_config(date=any_date, **config_kwargs)
with io.StringIO() as output:
lines = lines_from_run(config, output)
match = re.search(r'\{=?(\d+\.\d+ USD)\}', next(lines, "<EOF>"))
assert match
expect_rate = f' @ {match.group(1)}\n'
future_date = any_date.replace(year=any_date.year + 1)
config = build_config(date=future_date, from_date=any_date, **config_kwargs)
with io.StringIO() as output:
lines = lines_from_run(config, output)
assert next(lines, "<EOF>").endswith(expect_rate)