0 3 0
Brett Smith - 3 months ago 2020-05-19 19:56:19
brettcsmith@brettcsmith.org
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.
3 files changed with 32 insertions and 27 deletions:
↑ Collapse Diff ↑
...
 
@@ -14,7 +14,7 @@ except ImportError:
14 14
 
class Formatter:
15 15
 
    def __init__(self, cost_rates, price_rates=None,
16 16
 
                 signed_currencies=(), base_fmt='#,##0.###',
17
 
                 rate_precision=5, denomination=None):
17
 
                 rate_precision=6, denomination=None):
18 18
 
        self.cost_rates = cost_rates
19 19
 
        self.price_rates = price_rates
20 20
 
        self.base_fmt = base_fmt
...
 
@@ -95,27 +95,13 @@ class BeancountFormatter(Formatter):
95 95
 
            qrate = rate
96 96
 
        return qrate.normalize()
97 97
 

	
98
 
    def normalize_enough(self, rate, curr, from_amt, to_amt, prec=None):
99
 
        if prec is None:
100
 
            prec = self.rate_prec
101
 
        # Starting from prec, find the least amount of precision to
102
 
        # make sure from_amt converts exactly to to_amt.
103
 
        for try_prec in itertools.count(prec):
104
 
            try_rate = self.normalize_rate(rate, try_prec)
105
 
            got_amt = self.currency_decimal(from_amt * try_rate, curr)
106
 
            # If got_amt == to_amt, this is enough precision to do the
107
 
            # conversion exactly, so we're done.
108
 
            # If try_rate == rate, there's no more precision available, so stop.
109
 
            if (got_amt == to_amt) or (try_rate == rate):
110
 
                break
111
 
        return try_rate
112
 

	
113
 
    def _pretty_rate(self, fmt, rate, curr, from_amt=None, to_amt=None):
114
 
        if to_amt is None:
115
 
            rate = self.normalize_rate(rate)
116
 
        else:
117
 
            rate = self.normalize_enough(rate, curr, from_amt, to_amt)
118
 
        return fmt.format(self.format_currency(rate, curr, currency_digits=False))
98
 
    def _pretty_rate(self, fmt, rate, curr):
99
 
        rate_s = self.format_currency(
100
 
            self.normalize_rate(rate),
101
 
            curr,
102
 
            currency_digits=False,
103
 
        )
104
 
        return fmt.format(rate_s)
119 105
 

	
120 106
 
    def format_rate_pair(self, from_curr, to_curr):
121 107
 
        from_amt = 1
...
 
@@ -151,12 +137,10 @@ class BeancountFormatter(Formatter):
151 137
 
        if price is None:
152 138
 
            price_s = ''
153 139
 
        else:
154
 
            price_s = self._pretty_rate(
155
 
                self.PRICE_FMT, price, denomination, amount, to_amt,
156
 
            )
140
 
            price_s = self._pretty_rate(self.PRICE_FMT, price, denomination)
157 141
 
        return "{} {}{}".format(
158 142
 
            amt_s,
159
 
            self._pretty_rate(self.COST_FMT, cost, denomination, amount, to_amt),
143
 
            self._pretty_rate(self.COST_FMT, cost, denomination),
160 144
 
            price_s,
161 145
 
        )
162 146
 

	
...
 
@@ -5,7 +5,7 @@ from setuptools import setup
5 5
 
setup(
6 6
 
    name='oxrlib',
7 7
 
    description="Library to query the Open Exchange Rates (OXR) API",
8
 
    version='2.0',
8
 
    version='2.1',
9 9
 
    author='Brett Smith',
10 10
 
    author_email='brettcsmith@brettcsmith.org',
11 11
 
    license='GNU AGPLv3+',
...
 
@@ -239,3 +239,24 @@ def test_from_date_conversion(alternate_responder, output, any_date, output_form
239 239
 
    check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$', '0.507')
240 240
 
    check_fx_amount(config, lines, '20.52 AED', '0.272', 'USD', '$', '0.265')
241 241
 
    assert next(lines, None) is None
242
 

	
243
 
@parametrize_format
244
 
def test_rate_consistent_as_cost_and_price(alternate_responder, any_date, output_format):
245
 
    config_kwargs = {
246
 
        'responder': alternate_responder,
247
 
        'amount': 65000,
248
 
        'from_currency': 'RUB',
249
 
        'output_format': output_format,
250
 
        'signed_currencies': (),
251
 
    }
252
 
    config = build_config(date=any_date, **config_kwargs)
253
 
    with io.StringIO() as output:
254
 
        lines = lines_from_run(config, output)
255
 
        match = re.search(r'\{=?(\d+\.\d+ USD)\}', next(lines, "<EOF>"))
256
 
    assert match
257
 
    expect_rate = f' @ {match.group(1)}\n'
258
 
    future_date = any_date.replace(year=any_date.year + 1)
259
 
    config = build_config(date=future_date, from_date=any_date, **config_kwargs)
260
 
    with io.StringIO() as output:
261
 
        lines = lines_from_run(config, output)
262
 
        assert next(lines, "<EOF>").endswith(expect_rate)
0 comments (0 inline, 0 general)