many changes
This commit is contained in:
parent
126ede37a4
commit
661941a65c
7 changed files with 102 additions and 21 deletions
|
|
@ -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']}"
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ def test_handle_input_rates_works_with_full_correct_inputs():
|
|||
end_date=datetime.datetime.now() - datetime.timedelta(days=1),
|
||||
currencies="USD,EUR,GBP",
|
||||
dry_run=False,
|
||||
ignore_warnings=True,
|
||||
output="test_output.csv",
|
||||
)
|
||||
expected_result = {
|
||||
|
|
@ -23,6 +24,7 @@ def test_handle_input_rates_works_with_full_correct_inputs():
|
|||
),
|
||||
"currencies": {Currency("USD"), Currency("EUR"), Currency("GBP")},
|
||||
"dry_run": False,
|
||||
"ignore_warnings": True,
|
||||
"output": pathlib.Path("test_output.csv"),
|
||||
}
|
||||
|
||||
|
|
@ -38,6 +40,7 @@ def test_handle_input_rates_raises_with_bad_currency_code():
|
|||
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
||||
currencies="not_a_currency,USD,not_this_either",
|
||||
dry_run=False,
|
||||
ignore_warnings=True,
|
||||
output="test_output.csv",
|
||||
)
|
||||
|
||||
|
|
@ -49,6 +52,7 @@ def test_handle_input_rates_raises_with_start_date_after_end_date():
|
|||
end_date=datetime.datetime.now() - datetime.timedelta(days=7),
|
||||
currencies="GBP,USD",
|
||||
dry_run=False,
|
||||
ignore_warnings=True,
|
||||
output="test_output.csv",
|
||||
)
|
||||
|
||||
|
|
@ -60,6 +64,7 @@ def test_handle_input_rates_raises_with_output_different_than_csv():
|
|||
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
||||
currencies="GBP,USD",
|
||||
dry_run=False,
|
||||
ignore_warnings=True,
|
||||
output="test_output.xlsx",
|
||||
)
|
||||
|
||||
|
|
@ -70,6 +75,7 @@ def test_handle_input_rates_brings_future_end_date_to_today():
|
|||
end_date=datetime.datetime.now() + datetime.timedelta(days=7),
|
||||
currencies="USD,EUR,GBP",
|
||||
dry_run=False,
|
||||
ignore_warnings=True,
|
||||
output="test_output.csv",
|
||||
)
|
||||
|
||||
|
|
@ -82,6 +88,7 @@ def test_handle_input_rates_start_and_end_date_equal_works_fine():
|
|||
end_date=datetime.datetime.now(),
|
||||
currencies="USD,EUR,GBP",
|
||||
dry_run=False,
|
||||
ignore_warnings=True,
|
||||
output="test_output.csv",
|
||||
)
|
||||
expected_result = {
|
||||
|
|
@ -91,6 +98,7 @@ def test_handle_input_rates_start_and_end_date_equal_works_fine():
|
|||
),
|
||||
"currencies": {Currency("USD"), Currency("EUR"), Currency("GBP")},
|
||||
"dry_run": False,
|
||||
"ignore_warnings": True,
|
||||
"output": pathlib.Path("test_output.csv"),
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue