184 lines
6.4 KiB
Python
184 lines
6.4 KiB
Python
import datetime
|
|
import pathlib
|
|
|
|
import pytest
|
|
from money.currency import Currency
|
|
|
|
from xexe.currency_pair import CurrencyPair
|
|
from xexe.inputs_handling import handle_get_rates_inputs
|
|
from xexe.utils import DateRange
|
|
|
|
|
|
def test_handle_input_rates_works_with_full_correct_inputs():
|
|
handled_inputs = handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now() - datetime.timedelta(days=7),
|
|
end_date=datetime.datetime.now() - datetime.timedelta(days=1),
|
|
currencies="USD,EUR,GBP",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
expected_result = {
|
|
"date_range": DateRange(
|
|
start_date=(datetime.datetime.now() - datetime.timedelta(days=7)).date(),
|
|
end_date=(datetime.datetime.now() - datetime.timedelta(days=1)).date(),
|
|
),
|
|
"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 {"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():
|
|
|
|
with pytest.raises(ValueError):
|
|
handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now(),
|
|
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
|
currencies="not_a_currency,USD,not_this_either",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
|
|
|
|
def test_handle_input_rates_raises_with_invalid_rates_source():
|
|
with pytest.raises(ValueError):
|
|
handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now(),
|
|
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
|
currencies="not_a_currency,USD,not_this_either",
|
|
dry_run=False,
|
|
rates_source="clearly not a rates source. I guess, dunno.",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
|
|
|
|
def test_handle_input_rates_raises_with_start_date_after_end_date():
|
|
with pytest.raises(ValueError):
|
|
handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now(),
|
|
end_date=datetime.datetime.now() - datetime.timedelta(days=7),
|
|
currencies="GBP,USD",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
|
|
|
|
def test_handle_input_rates_raises_with_output_different_than_csv():
|
|
with pytest.raises(ValueError):
|
|
handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now(),
|
|
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
|
currencies="GBP,USD",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.xlsx",
|
|
)
|
|
|
|
|
|
def test_handle_input_rates_brings_future_end_date_to_today():
|
|
handled_inputs = handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now() - datetime.timedelta(days=7),
|
|
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
|
currencies="USD,EUR,GBP",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
|
|
assert handled_inputs["date_range"].end_date == datetime.datetime.now().date()
|
|
|
|
|
|
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(),
|
|
pairs="USDEUR,EURUSD,GBPZAR",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
expected_result = {
|
|
"date_range": DateRange(
|
|
start_date=datetime.datetime.now().date(),
|
|
end_date=datetime.datetime.now().date(),
|
|
),
|
|
"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,
|
|
"output": pathlib.Path("test_output.csv"),
|
|
}
|
|
|
|
for key in expected_result.keys():
|
|
assert handled_inputs[key] == expected_result[key]
|
|
|
|
|
|
def test_handle_input_rates_with_pairs_works_fine():
|
|
handled_inputs = handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now(),
|
|
end_date=datetime.datetime.now(),
|
|
pairs="USDEUR,EURUSD,GBPZAR",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|
|
expected_result = {
|
|
"date_range": DateRange(
|
|
start_date=datetime.datetime.now().date(),
|
|
end_date=datetime.datetime.now().date(),
|
|
),
|
|
"pairs": {
|
|
CurrencyPair(from_currency=Currency["USD"], to_currency=Currency["EUR"]),
|
|
CurrencyPair(from_currency=Currency["EUR"], to_currency=Currency["USD"]),
|
|
CurrencyPair(from_currency=Currency["GBP"], to_currency=Currency["ZAR"]),
|
|
},
|
|
"dry_run": False,
|
|
"rates_source": "mock",
|
|
"ignore_warnings": True,
|
|
"output": pathlib.Path("test_output.csv"),
|
|
}
|
|
|
|
for key in expected_result.keys():
|
|
assert handled_inputs[key] == expected_result[key]
|
|
|
|
|
|
def test_handle_input_rates_raises_with_both_currencies_and_pairs():
|
|
with pytest.raises(ValueError):
|
|
handle_get_rates_inputs(
|
|
start_date=datetime.datetime.now(),
|
|
end_date=datetime.datetime.now(),
|
|
currencies="EUR,USD,ZAR",
|
|
pairs="USDEUR,EURUSD,GBPZAR",
|
|
dry_run=False,
|
|
rates_source="mock",
|
|
ignore_warnings=True,
|
|
output="test_output.csv",
|
|
)
|