dry run now for output, rate sources works

This commit is contained in:
Pablo Martin 2024-06-12 17:59:34 +02:00
parent 15e8b8e513
commit e3ec041922
2 changed files with 18 additions and 23 deletions

View file

@ -37,9 +37,11 @@ def test_get_rates_for_hardcoded_case_returns_expected_output():
"--start-date",
"2024-01-01",
"--end-date",
"2024-01-03",
"2024-01-02",
"--currencies",
"USD,GBP,EUR",
"USD,GBP",
"--rates-source",
"xe",
"--output",
"test_output.csv",
],
@ -88,7 +90,6 @@ def test_get_rates_dry_run_always_returns_42_as_rates():
",".join(some_random_currencies),
"--output",
"test_output.csv",
"--dry-run",
],
)

View file

@ -6,6 +6,7 @@ from typing import List
from money.currency import Currency
from xecd_rates_client import XecdClient
from xexe.constants import RATES_SOURCES
from xexe.exchange_rates import ExchangeRates, add_equal_rates, add_inverse_rates
from xexe.rate_fetching import MockRateFetcher, RateFetcher, XERateFetcher
from xexe.rate_writing import CSVRateWriter, RateWriter
@ -62,24 +63,27 @@ def run_get_rates(
) -> None:
logger.info("Getting rates")
process_state = GetRatesProcessState(
output=output, dry_run=dry_run, ignore_warnings=ignore_warnings
)
process_state = GetRatesProcessState(output=output, ignore_warnings=ignore_warnings)
rates = obtain_rates_from_source(
process_state,
rates_source=rates_source,
date_range=date_range,
currencies=currencies,
)
logger.info("Rates obtained.")
if dry_run:
logger.info("Dry run mode active. Not writing rates to output.")
return
write_rates_to_output(process_state, rates)
logger.info("Rates written to output.")
def obtain_rates_from_source(
process_state, date_range: DateRange, currencies: List[Currency]
process_state, rates_source: str, date_range: DateRange, currencies: List[Currency]
) -> ExchangeRates:
rates_fetcher = process_state.get_fetcher()
rates_fetcher = build_rate_fetcher(rates_source)
currency_and_date_combinations = generate_currency_and_dates_combinations(
date_range=date_range, currencies=currencies
@ -128,10 +132,13 @@ def write_rates_to_output(process_state, rates):
rates_writer.write_rates(rates)
def build_rate_fetcher(rates_source: str):
return RATES_SOURCES[rates_source]()
class GetRatesProcessState:
def __init__(self, output: str, dry_run: bool, ignore_warnings: bool) -> None:
def __init__(self, output: str, ignore_warnings: bool) -> None:
self.writer = self._select_writer(output)
self.fetcher = self._select_fetcher(dry_run)
self.ignore_warnings = ignore_warnings
@staticmethod
@ -143,18 +150,5 @@ class GetRatesProcessState:
raise ValueError(f"Don't know how to handle passed output: {output}")
@staticmethod
def _select_fetcher(dry_run: bool) -> RateFetcher:
if dry_run:
logger.info("Dry-run activated. Running against MockRateFetcher.")
return MockRateFetcher()
if not dry_run:
logger.info("Real run active. Running against XE.com's API.")
return XERateFetcher()
def get_fetcher(self) -> RateFetcher:
return self.fetcher
def get_writer(self) -> RateWriter:
return self.writer