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 USD, EUR and GBP for 2024-01-01 to 2024-01-03 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-01-01", "--end-date", "2024-01-02", "--currencies", "USD,GBP", "--rates-source", "xe", "--output", "test_output.csv", ], ) assert run_result.exit_code == 0 # Write code here to read output file and compare it against expected # output assert False """ 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']}"