data-xexe/tests/tests_integration/test_get_rates.py
2024-06-27 17:42:10 +02:00

145 lines
4.8 KiB
Python

import csv
import datetime
import random
from click.testing import CliRunner
from money.currency import Currency
from xexe.cli import get_rates
from xexe.constants import DEFAULT_CURRENCIES
def test_get_rates_for_hardcoded_case_returns_expected_output():
"""
Calling the CLI get-rates command to get the rates between CAD and GBP for
2024-06-20 returns the expected records in a CSV.
ATTENTION!
This is a production-grade test. It will run against xe.com and requires you
to have a proper setup. It is not intended to be an automated or frequent
test, but rather be a helper.
Because of this, you can see that the test is mostly commented out. If you
want to run it, uncomment and use it only for what you need.
When commented, the test should always pass to not bother automated test
runs.
"""
""" # Unstring this to activate test
runner = CliRunner()
with runner.isolated_filesystem():
run_result = runner.invoke(
get_rates,
[
"--start-date",
"2024-06-20",
"--end-date",
"2024-06-20",
"--currencies",
"CAD,EUR",
"--rates-source",
"xe",
"--output",
"test_output.csv",
],
)
assert run_result.exit_code == 0
expected_output = [
["from_currency", "to_currency", "rate", "rate_date", "exported_at"],
["CAD", "EUR", "0.67884137", "2024-06-20", "2024-06-27T17:26:04"],
["EUR", "CAD", "1.47309820", "2024-06-20", "2024-06-27T17:26:04"],
["CAD", "CAD", "1.00000000", "2024-06-20", "2024-06-27T17:26:04"],
["EUR", "EUR", "1.00000000", "2024-06-20", "2024-06-27T17:26:04"],
]
with open("test_output.csv", newline="") as csvfile:
reader = csv.reader(csvfile)
output = list(reader)
headers_match = output[0] == expected_output[0]
assert headers_match
for i, row in enumerate(output[1:], start=1):
row_matches = row[:4] == expected_output[i][:4]
exported_at_is_valid_iso_datetime = bool(
datetime.datetime.fromisoformat(row[4])
)
assert row_matches and exported_at_is_valid_iso_datetime
"""
# This is for when the main test is commented out
assert True
def test_get_rates_dry_run_always_returns_42_as_rates():
"""
Calling the CLI get-rates command with the dry-run flag will apparently
work, but it always returns 42 as the rate no matter what it's being asked
for since it doesn't call xe.com at all and 42 is the Answer to the Ultimate
Question of Life, the Universe, and Everything.
"""
some_random_date = datetime.datetime(
year=random.choice(range(2010, 2020)),
month=random.choice(range(1, 13)),
day=random.choice(range(1, 29)),
).date()
some_random_currencies = [
some_currency.value
for some_currency in random.sample(population=list(DEFAULT_CURRENCIES), k=3)
]
runner = CliRunner()
with runner.isolated_filesystem():
run_result = runner.invoke(
get_rates,
[
"--start-date",
some_random_date.strftime("%Y-%m-%d"),
"--end-date",
(some_random_date + datetime.timedelta(days=3)).strftime("%Y-%m-%d"),
"--currencies",
",".join(some_random_currencies),
"--output",
"test_output.csv",
],
)
assert run_result.exit_code == 0
with open("test_output.csv", newline="") as csv_file:
reader = csv.DictReader(csv_file)
rows = list(reader)
# Ensure that the output contains the correct number of rows
expected_num_rows = 36
assert (
len(rows) == expected_num_rows
), f"Expected {expected_num_rows} rows, but got {len(rows)}"
# Check that all rows have the expected rate of 42, 1/42 or 1 and the correct dates
for row in rows:
assert row["rate"] in (
"42.00000000",
"0.02380952",
"0.00000000",
"1.00000000",
), f"Expected rate to be 42, 1/42 or 1, but got {row['rate']}"
assert row["rate_date"] in [
(some_random_date + datetime.timedelta(days=i)).strftime("%Y-%m-%d")
for i in range(4)
], f"Unexpected rate_date {row['rate_date']}"
assert (
row["from_currency"] in some_random_currencies
), f"Unexpected from_currency {row['from_currency']}"
assert (
row["to_currency"] in some_random_currencies
), f"Unexpected to_currency {row['to_currency']}"