diff --git a/tests/test_historical.py b/tests/test_historical.py index 7e935f4c19ef08ecef618b457f55c6ca0552e338..2d019e9a05abf4ae6ee9ac6addb41ad8bfbc790b 100644 --- a/tests/test_historical.py +++ b/tests/test_historical.py @@ -40,6 +40,7 @@ class FakeConfig: output = pytest.fixture(lambda: io.StringIO()) parametrize_format = pytest.mark.parametrize('output_format', [ oxrhist.Formats.LEDGER, + oxrhist.Formats.BEANCOUNT, ]) @pytest.fixture(scope='module') @@ -85,20 +86,38 @@ def lines_from_run(config, output): def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=None): if price is None: price = cost + rate_fmt = f'{{}} {re.escape(fx_code)}' 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)}{{}}' + 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)) else: - rate_fmt = f'{{}} {re.escape(fx_code)}' - pattern = r'^{} {{={}}} @ {}$'.format( - re.escape(amount), - rate_fmt.format(cost), - rate_fmt.format(price), - ) + 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) line = next(lines, "") assert re.match(pattern, line) +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, "") == expected + def test_rate_list(single_responder, output, any_date): config = build_config(single_responder, any_date) lines = lines_from_run(config, output) @@ -144,7 +163,7 @@ def test_ledger_conversion(single_responder, output, any_date, output_format): amount=300, output_format=output_format) lines = lines_from_run(config, output) check_fx_amount(config, lines, '300 ALL', '0.00691', 'USD', '$') - assert next(lines) == '$2.08\n' + check_nonfx_amount(config, lines, '2.08') assert next(lines, None) is None @parametrize_format @@ -173,7 +192,7 @@ def test_redundant_denomination(single_responder, output, any_date, output_forma output_format=output_format, denomination='USD') lines = lines_from_run(config, output) check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$') - assert next(lines) == '$5.59\n' + check_nonfx_amount(config, lines, '5.59') assert next(lines, None) is None @parametrize_format @@ -182,7 +201,7 @@ def test_from_denomination(single_responder, output, any_date, output_format): from_currency='USD', to_currency='ALL', amount=10, output_format=output_format, denomination='USD') lines = lines_from_run(config, output) - assert next(lines) == '$10.00\n' + check_nonfx_amount(config, lines, '10.00') check_fx_amount(config, lines, '1,445 ALL', '0.00691', 'USD', '$') assert next(lines, None) is None @@ -197,7 +216,7 @@ def test_rate_precision_added_as_needed(single_responder, output, any_date, outp # Make sure the rate is specified with enough precision to get the # correct conversion amount. check_fx_amount(config, lines, '63,805.00 RUB', '0.0175204', 'USD', '$') - assert next(lines) == '$1,117.89\n' + check_nonfx_amount(config, lines, '1,117.89') assert next(lines, None) is None @parametrize_format