Changeset - c9382a26044a
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-05-20 19:21:13
brettcsmith@brettcsmith.org
historical: Fix the rate ordering with two dates.

Darnit, I wrote the tests first, and I wrote them right, and then I
mixed up the ordering in the code, and somehow I convinced myself
the code was the right and the tests were wrong. But no, I had the
tests right, this is really what we want. This gets the output to
follow the examples from our bookkeeping documentation.
3 files changed with 12 insertions and 11 deletions:
0 comments (0 inline, 0 general)
oxrlib/commands/historical.py
Show inline comments
...
 
@@ -182,17 +182,19 @@ def load_rates(config, loaders, date):
 
    if loaders.should_cache():
 
        config.cache.save_rate(rates)
 
    return rates
 

	
 
def run(config, stdout, stderr):
 
    loaders = config.get_loaders()
 
    cost_rates = load_rates(config, loaders, config.args.date)
 
    date_rates = load_rates(config, loaders, config.args.date)
 
    if config.args.from_date is None:
 
        cost_rates = date_rates
 
        price_rates = None
 
    else:
 
        price_rates = load_rates(config, loaders, config.args.from_date)
 
        cost_rates = load_rates(config, loaders, config.args.from_date)
 
        price_rates = date_rates
 
    formatter = config.args.output_format.value(
 
        cost_rates,
 
        price_rates,
 
        config.args.signed_currencies,
 
        denomination=config.args.denomination,
 
    )
setup.py
Show inline comments
...
 
@@ -2,13 +2,13 @@
 

	
 
from setuptools import setup
 

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

	
 
    install_requires=['babel'],
 
    setup_requires=['pytest-runner'],
tests/test_historical.py
Show inline comments
...
 
@@ -222,25 +222,25 @@ def test_rate_precision_added_as_needed(single_responder, output, any_date, outp
 
def test_from_date_rates(alternate_responder, output, any_date, output_format):
 
    config = build_config(alternate_responder, any_date,
 
                          from_currency='ANG', to_currency='AED',
 
                          from_date=any_date, output_format=output_format,
 
                          denomination='USD')
 
    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')
 
    check_fx_amount(config, lines, '1 ANG', '1.909', 'AED', None, '2.051')
 
    check_fx_amount(config, lines, '1 AED', '0.523', 'ANG', None, '0.487')
 
    assert next(lines, None) is None
 

	
 
@parametrize_format
 
def test_from_date_conversion(alternate_responder, output, any_date, output_format):
 
    config = build_config(alternate_responder, any_date,
 
                          from_currency='ANG', to_currency='AED', amount=10,
 
                          from_date=any_date, output_format=output_format,
 
                          denomination='USD')
 
    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')
 
    check_fx_amount(config, lines, '10.00 ANG', '0.507', 'USD', '$', '0.558')
 
    check_fx_amount(config, lines, '19.10 AED', '0.265', 'USD', '$', '0.272')
 
    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,
...
 
@@ -249,14 +249,13 @@ def test_rate_consistent_as_cost_and_price(alternate_responder, any_date, output
 
        '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'
 
        match = re.search(r'\{=?\d+\.\d+ USD\}', next(lines, "<EOF>"))
 
    assert match is not None
 
    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)
 
        assert match.group(0) in next(lines, "<EOF>")
0 comments (0 inline, 0 general)