some utils and tests
This commit is contained in:
parent
622cefc4d4
commit
7012cbec97
2 changed files with 81 additions and 0 deletions
49
tests/tests_unit/test_utils.py
Normal file
49
tests/tests_unit/test_utils.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from money.currency import Currency
|
||||||
|
|
||||||
|
from xexe.utils import DateRange, generate_currency_and_dates_combinations
|
||||||
|
|
||||||
|
|
||||||
|
def test_date_range_breaks_with_reversed_dates():
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DateRange(
|
||||||
|
start_date=datetime.date(year=2024, month=1, day=3),
|
||||||
|
end_date=datetime.date(year=2024, month=1, day=1),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_date_range_generates_proper_dates_when_itered():
|
||||||
|
|
||||||
|
a_range = DateRange(
|
||||||
|
start_date=datetime.date(year=2024, month=1, day=1),
|
||||||
|
end_date=datetime.date(year=2024, month=1, day=3),
|
||||||
|
)
|
||||||
|
|
||||||
|
dates = [a_date for a_date in a_range]
|
||||||
|
|
||||||
|
assert dates[0] == datetime.date(year=2024, month=1, day=1)
|
||||||
|
assert dates[1] == datetime.date(year=2024, month=1, day=2)
|
||||||
|
assert dates[-1] == datetime.date(year=2024, month=1, day=3)
|
||||||
|
assert len(dates) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def generate_currency_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),
|
||||||
|
)
|
||||||
|
|
||||||
|
currencies = {Currency.USD, Currency.EUR, Currency.GBP}
|
||||||
|
|
||||||
|
combinations = generate_currency_and_dates_combinations(
|
||||||
|
currencies=currencies, date_range=date_range
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
from itertools import combinations
|
||||||
|
from typing import Set
|
||||||
|
|
||||||
|
from money.currency import Currency
|
||||||
|
|
||||||
|
|
||||||
class DateRange:
|
class DateRange:
|
||||||
|
|
@ -55,3 +59,31 @@ class DateRange:
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"""Date Range: {self.start_date.strftime("%Y-%m-%d")} to {self.end_date.strftime("%Y-%m-%d")}."""
|
return f"""Date Range: {self.start_date.strftime("%Y-%m-%d")} to {self.end_date.strftime("%Y-%m-%d")}."""
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
current_date = self.start_date
|
||||||
|
while current_date <= self.end_date:
|
||||||
|
yield current_date
|
||||||
|
current_date += datetime.timedelta(days=1)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_currency_and_dates_combinations(
|
||||||
|
date_range: DateRange, currencies: Set[Currency]
|
||||||
|
):
|
||||||
|
currency_pairs = list(combinations(currencies, 2))
|
||||||
|
|
||||||
|
combinations = []
|
||||||
|
for date in date_range:
|
||||||
|
for from_currency, to_currency in currency_pairs:
|
||||||
|
combinations.append(
|
||||||
|
{
|
||||||
|
"from_currency": from_currency,
|
||||||
|
"to_currency": to_currency,
|
||||||
|
"date": date,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
combinations = tuple(combinations)
|
||||||
|
|
||||||
|
# Convert the result to a tuple
|
||||||
|
return combinations
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue