pairs are usable
This commit is contained in:
parent
2432b7d6dc
commit
568e27adbe
3 changed files with 67 additions and 9 deletions
|
|
@ -3,7 +3,12 @@ import datetime
|
|||
import pytest
|
||||
from money.currency import Currency
|
||||
|
||||
from xexe.utils import DateRange, generate_currency_and_dates_combinations
|
||||
from xexe.currency_pair import CurrencyPair
|
||||
from xexe.utils import (
|
||||
DateRange,
|
||||
generate_currency_and_dates_combinations,
|
||||
generate_pairs_and_dates_combinations,
|
||||
)
|
||||
|
||||
|
||||
def test_date_range_breaks_with_reversed_dates():
|
||||
|
|
@ -30,7 +35,7 @@ def test_date_range_generates_proper_dates_when_itered():
|
|||
assert len(dates) == 3
|
||||
|
||||
|
||||
def generate_currency_and_dates_combinations_outputs_correctly():
|
||||
def test_generate_currency_and_dates_combinations_outputs_correctly():
|
||||
|
||||
date_range = DateRange(
|
||||
start_date=datetime.date(year=2024, month=1, day=1),
|
||||
|
|
@ -44,6 +49,28 @@ def generate_currency_and_dates_combinations_outputs_correctly():
|
|||
)
|
||||
|
||||
assert len(combinations) == 9
|
||||
assert len({date for date in combinations["date"]}) == 3
|
||||
assert len({currency for currency in combinations["from_currency"]}) == 3
|
||||
assert len({currency for currency in combinations["to_currency"]}) == 3
|
||||
assert len({combination["date"] for combination in combinations}) == 3
|
||||
assert len({combination["from_currency"] for combination in combinations}) == 2
|
||||
assert len({combination["to_currency"] for combination in combinations}) == 2
|
||||
|
||||
|
||||
def test_generate_pair_and_dates_combinations_outputs_correctly():
|
||||
date_range = DateRange(
|
||||
start_date=datetime.date(year=2024, month=1, day=1),
|
||||
end_date=datetime.date(year=2024, month=1, day=3),
|
||||
)
|
||||
|
||||
pairs = {
|
||||
CurrencyPair(from_currency="USD", to_currency="EUR"),
|
||||
CurrencyPair(from_currency="USD", to_currency="GBP"),
|
||||
CurrencyPair(from_currency="EUR", to_currency="GBP"),
|
||||
}
|
||||
|
||||
combinations = generate_pairs_and_dates_combinations(
|
||||
pairs=pairs, date_range=date_range
|
||||
)
|
||||
|
||||
assert len(combinations) == 9
|
||||
assert len({combination["date"] for combination in combinations}) == 3
|
||||
assert len({combination["from_currency"] for combination in combinations}) == 2
|
||||
assert len({combination["to_currency"] for combination in combinations}) == 2
|
||||
|
|
|
|||
|
|
@ -11,7 +11,11 @@ from xexe.currency_pair import CurrencyPair
|
|||
from xexe.exchange_rates import ExchangeRates, add_equal_rates, add_inverse_rates
|
||||
from xexe.rate_fetching import build_rate_fetcher
|
||||
from xexe.rate_writing import build_rate_writer
|
||||
from xexe.utils import DateRange, generate_currency_and_dates_combinations
|
||||
from xexe.utils import (
|
||||
DateRange,
|
||||
generate_currency_and_dates_combinations,
|
||||
generate_pairs_and_dates_combinations,
|
||||
)
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
|
|
@ -106,10 +110,16 @@ def obtain_rates_from_source(
|
|||
rates_source=rates_source, rate_sources_mapping=RATES_SOURCES
|
||||
)
|
||||
|
||||
if currencies:
|
||||
currency_and_date_combinations = generate_currency_and_dates_combinations(
|
||||
date_range=date_range, currencies=currencies
|
||||
)
|
||||
|
||||
if pairs:
|
||||
currency_and_date_combinations = generate_pairs_and_dates_combinations(
|
||||
date_range=date_range, pairs=pairs
|
||||
)
|
||||
|
||||
large_api_call_planned = (
|
||||
rates_fetcher.is_production_grade and len(currency_and_date_combinations) > 100
|
||||
)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ from typing import Set, Tuple
|
|||
|
||||
from money.currency import Currency
|
||||
|
||||
from xexe.currency_pair import CurrencyPair
|
||||
|
||||
|
||||
class DateRange:
|
||||
|
||||
|
|
@ -86,3 +88,22 @@ def generate_currency_and_dates_combinations(
|
|||
currency_date_combinations = tuple(currency_date_combinations)
|
||||
|
||||
return currency_date_combinations
|
||||
|
||||
|
||||
def generate_pairs_and_dates_combinations(
|
||||
date_range: DateRange, pairs: Set[CurrencyPair]
|
||||
) -> Tuple[dict]:
|
||||
currency_date_combinations = []
|
||||
for date in date_range:
|
||||
for pair in pairs:
|
||||
currency_date_combinations.append(
|
||||
{
|
||||
"from_currency": pair.from_currency,
|
||||
"to_currency": pair.to_currency,
|
||||
"date": date,
|
||||
}
|
||||
)
|
||||
|
||||
currency_date_combinations = tuple(currency_date_combinations)
|
||||
|
||||
return currency_date_combinations
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue