Skip to content

Commit 75ee8cb

Browse files
Add test functi
ons for _validate_date_range and _validate_output.
1 parent a8e461d commit 75ee8cb

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/backtest_bay/data/download_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def _validate_date_range(start_date, end_date):
7373

7474
def _validate_output(data, symbol, start_date, end_date, interval):
7575
if data.empty:
76+
# Since we already checked for a valid symbol, we check if input dates
77+
# fit to historic available data.
7678
ticker = yf.Ticker(symbol)
7779
hist = ticker.history(period="max")
7880
min_date = hist.index.min().strftime("%Y-%m-%d")

tests/data/test_download_data.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import pandas as pd
12
import pytest
23

34
from backtest_bay.data.download_data import (
45
_validate_date_format,
6+
_validate_date_range,
57
_validate_interval,
8+
_validate_output,
69
_validate_symbol,
710
)
811

@@ -92,3 +95,59 @@ def test_validate_date_format_non_string():
9295
match = "Date must be a string in 'YYYY-MM-DD' format."
9396
with pytest.raises(TypeError, match=match):
9497
_validate_date_format(12345)
98+
99+
100+
# tests for _validate_date_range
101+
102+
103+
def test_valid_date_range():
104+
_validate_date_range("2024-01-01", "2024-12-31")
105+
106+
107+
def test_invalid_date_range():
108+
with pytest.raises(ValueError, match="Start date must be before end date."):
109+
_validate_date_range("2024-12-31", "2024-01-01")
110+
111+
112+
# tests for _validate_output
113+
114+
115+
def test_validate_output_valid_input():
116+
# generate typical Yahoo Finance output format
117+
symbol = "AAPL"
118+
arrays = [
119+
["Close", "High", "Low", "Open", "Volume"],
120+
[symbol, symbol, symbol, symbol, symbol],
121+
]
122+
index = pd.MultiIndex.from_tuples(
123+
list(zip(*arrays, strict=False)), names=["Price", "Ticker"]
124+
)
125+
126+
data = pd.DataFrame(
127+
[[1, 10, 1, 4, 500], [2, 8, 2, 4, 500], [2, 4, 2, 5, 400], [3, 6, 2, 4, 200]],
128+
index=pd.to_datetime(["2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05"]),
129+
columns=index,
130+
)
131+
data.index.name = "Date"
132+
133+
_validate_output(data, symbol, "2024-01-02", "2024-01-05", "1d")
134+
135+
136+
def test_validate_output_empty_input():
137+
# generate typical Yahoo Finance output format
138+
symbol = "AAPL"
139+
arrays = [
140+
["Close", "High", "Low", "Open", "Volume"],
141+
[symbol, symbol, symbol, symbol, symbol],
142+
]
143+
index = pd.MultiIndex.from_tuples(
144+
list(zip(*arrays, strict=False)), names=["Price", "Ticker"]
145+
)
146+
147+
empty_data = pd.DataFrame(columns=index)
148+
149+
empty_data.index.name = "Date"
150+
151+
match = "No data found for AAPL between 1800-01-01 and 1800-05-01."
152+
with pytest.raises(ValueError, match=match):
153+
_validate_output(empty_data, symbol, "1800-01-01", "1800-05-01", "1d")

0 commit comments

Comments
 (0)