many changes

This commit is contained in:
Pablo Martin 2024-06-11 21:10:07 +02:00
parent 126ede37a4
commit 661941a65c
7 changed files with 102 additions and 21 deletions

View file

@ -1,3 +1,4 @@
import csv
import datetime
import random
@ -11,6 +12,18 @@ 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.
"""
runner = CliRunner()
@ -50,7 +63,12 @@ def test_get_rates_dry_run_always_returns_42_as_rates():
day=random.choice(range(1, 29)),
).date()
some_random_currencies = random.choices(population=list(Currency), k=3)
some_random_currencies = [
# Get the last 3 right characters, which are the actual
# currency code
str(some_currency)[-3:]
for some_currency in random.sample(population=list(Currency), k=3)
]
runner = CliRunner()
@ -63,14 +81,7 @@ def test_get_rates_dry_run_always_returns_42_as_rates():
"--end-date",
(some_random_date + datetime.timedelta(days=3)).strftime("%Y-%m-%d"),
"--currencies",
",".join(
[
# Get the last 3 right characters, which are the actual
# currency code
str(some_currency)[-3:]
for some_currency in some_random_currencies
]
),
",".join(some_random_currencies),
"--output",
"test_output.csv",
"--dry-run",
@ -79,6 +90,29 @@ def test_get_rates_dry_run_always_returns_42_as_rates():
assert run_result.exit_code == 0
# Write code here to read output file and compare it against expected
# output
assert False
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 = (3 + 1) * len(
some_random_currencies
) # 3 days + 1 day = 4 dates for each currency
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 and the correct dates
for row in rows:
assert row["rate"] == "42", f"Expected rate to be 42, 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']}"