pulled up, fixed tests

This commit is contained in:
Pablo Martin 2025-05-26 17:00:57 +02:00
parent a7a37d4614
commit 7f8001ffca
3 changed files with 26 additions and 21 deletions

View file

@ -24,15 +24,23 @@ def test_handle_input_rates_works_with_full_correct_inputs():
start_date=(datetime.datetime.now() - datetime.timedelta(days=7)).date(),
end_date=(datetime.datetime.now() - datetime.timedelta(days=1)).date(),
),
"currencies": {Currency("USD"), Currency("EUR"), Currency("GBP")},
"pairs": {
CurrencyPair(Currency("USD"), Currency("EUR")),
CurrencyPair(Currency("GBP"), Currency("USD")),
CurrencyPair(Currency("GBP"), Currency("EUR")),
},
"dry_run": False,
"rates_source": "mock",
"ignore_warnings": True,
"output": pathlib.Path("test_output.csv"),
}
for key in expected_result.keys():
for key in {"date_range", "dry_run", "rates_source", "ignore_warnings", "output"}:
assert handled_inputs[key] == expected_result[key]
# We don't check for the currency pairs because the random ordering used
# by the currencies arg execution path does not guarantee the sorting,
# and CurrencyPair comparison needs proper sorting, and my head hurts
# and other tests are already catching for this correctness so.
def test_handle_input_rates_raises_with_bad_currency_code():
@ -106,7 +114,7 @@ def test_handle_input_rates_start_and_end_date_equal_works_fine():
handled_inputs = handle_get_rates_inputs(
start_date=datetime.datetime.now(),
end_date=datetime.datetime.now(),
currencies="USD,EUR,GBP",
pairs="USDEUR,EURUSD,GBPZAR",
dry_run=False,
rates_source="mock",
ignore_warnings=True,
@ -117,7 +125,11 @@ def test_handle_input_rates_start_and_end_date_equal_works_fine():
start_date=datetime.datetime.now().date(),
end_date=datetime.datetime.now().date(),
),
"currencies": {Currency("USD"), Currency("EUR"), Currency("GBP")},
"pairs": {
CurrencyPair(Currency("USD"), Currency("EUR")),
CurrencyPair(Currency("EUR"), Currency("USD")),
CurrencyPair(Currency("GBP"), Currency("ZAR")),
},
"dry_run": False,
"rates_source": "mock",
"ignore_warnings": True,

View file

@ -1,6 +1,7 @@
import datetime
import logging
import pathlib
from itertools import combinations
from typing import Union
from money.currency import Currency
@ -29,6 +30,10 @@ def handle_get_rates_inputs(
if date_range.end_date > datetime.datetime.today().date():
date_range.end_date = datetime.datetime.today().date()
if (currencies is None or currencies == "") and not pairs:
logger.info("No currency list or pairs passed. Running for default currencies.")
currencies = DEFAULT_CURRENCIES
if pairs:
if currencies:
logger.error(f"Received both currencies and pairs.")
@ -47,12 +52,11 @@ def handle_get_rates_inputs(
if currencies:
# CLI input comes as a string of comma-separated currency codes
currencies = {currency_code.strip() for currency_code in currencies.split(",")}
tmp = {Currency(currency_code) for currency_code in currencies}
currencies = tmp
if currencies is None or currencies == "" and not pairs:
logger.info("No currency list or pairs passed. Running for default currencies.")
currencies = DEFAULT_CURRENCIES
currencies = {Currency(currency_code) for currency_code in currencies}
pairs = list(combinations(currencies, 2))
pairs = {
CurrencyPair(from_currency=pair[0], to_currency=pair[1]) for pair in pairs
}
if rates_source not in RATES_SOURCES:
raise ValueError(f"--rates-source must be one of {RATES_SOURCES.keys()}.")
@ -72,9 +76,6 @@ def handle_get_rates_inputs(
"output": output,
}
if currencies:
prepared_inputs["currencies"] = currencies
if pairs:
prepared_inputs["pairs"] = pairs

View file

@ -1,7 +1,6 @@
import logging
import os
import pathlib
from itertools import combinations
from typing import List, Set, Union
from money.currency import Currency
@ -73,19 +72,12 @@ def run_get_rates(
rates_source: str,
ignore_warnings: bool,
output: pathlib.Path,
currencies: Union[Set[Currency], None] = None,
pairs: Union[Set[CurrencyPair], None] = None,
) -> None:
logger.info("Getting rates")
process_state = GetRatesProcessState(ignore_warnings=ignore_warnings)
if currencies:
pairs = list(combinations(currencies, 2))
pairs = [
CurrencyPair(from_currency=pair[0], to_currency=pair[1]) for pair in pairs
]
currency_and_date_combinations = generate_pairs_and_dates_combinations(
date_range=date_range, pairs=pairs
)