Skip to content

Commit a8e461d

Browse files
Add additional test cases for download_data.py.
1 parent b0b64ae commit a8e461d

File tree

3 files changed

+102
-26
lines changed

3 files changed

+102
-26
lines changed

src/backtest_bay/data/download_data.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ def _validate_symbol(symbol):
2222
is_symbol_string = isinstance(symbol, str)
2323
if not is_symbol_string:
2424
error_msg = "Symbol must be a non-empty string."
25-
raise ValueError(error_msg)
25+
raise TypeError(error_msg)
2626

2727
is_symbol_existent = not yf.Ticker(symbol).history(period="1d").empty
2828
if not is_symbol_existent:
29-
error_msg = f"Invalid symbol: '{symbol}'. Please provide a valid ticker symbol."
29+
error_msg = (
30+
f"Invalid symbol: '{symbol}'. Please provide a valid ticker symbol "
31+
"from https://finance.yahoo.com/"
32+
)
3033
raise ValueError(error_msg)
3134

3235

@@ -52,6 +55,9 @@ def _validate_interval(interval):
5255

5356

5457
def _validate_date_format(date_str):
58+
if not isinstance(date_str, str):
59+
error_msg = "Date must be a string in 'YYYY-MM-DD' format."
60+
raise TypeError(error_msg)
5561
try:
5662
datetime.fromisoformat(date_str)
5763
except ValueError as e:

src/backtest_bay/test_download_data.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/data/test_download_data.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pytest
2+
3+
from backtest_bay.data.download_data import (
4+
_validate_date_format,
5+
_validate_interval,
6+
_validate_symbol,
7+
)
8+
9+
10+
# Tests for _validate_symbol
11+
def test_validate_symbol_valid_cases():
12+
_validate_symbol("AAPL")
13+
_validate_symbol("MSFT")
14+
15+
16+
def test_validate_symbol_invalid_invalid_symbol():
17+
with pytest.raises(ValueError, match="Invalid symbol: 'INVALID'"):
18+
_validate_symbol("INVALID")
19+
20+
21+
def test_validate_symbol_invalid_empty_string():
22+
with pytest.raises(ValueError, match="Invalid symbol: ''"):
23+
_validate_symbol("")
24+
25+
26+
def test_validate_symbol_invalid_non_string():
27+
with pytest.raises(TypeError, match="Symbol must be a non-empty string."):
28+
_validate_symbol(123)
29+
30+
31+
# Tests for _validate_date_format
32+
@pytest.mark.parametrize(
33+
"interval",
34+
[
35+
"1m",
36+
"2m",
37+
"5m",
38+
"15m",
39+
"30m",
40+
"60m",
41+
"90m",
42+
"1h",
43+
"1d",
44+
"5d",
45+
"1wk",
46+
"1mo",
47+
"3mo",
48+
],
49+
)
50+
def test_validate_interval_valid(interval):
51+
_validate_interval(interval)
52+
53+
54+
def test_validate_interval_invalid():
55+
with pytest.raises(ValueError, match="Invalid interval.*"):
56+
_validate_interval("10m")
57+
58+
59+
@pytest.mark.parametrize("invalid_interval", [None, "", " "])
60+
def test_validate_interval_edge_cases(invalid_interval):
61+
with pytest.raises(ValueError, match="Invalid interval.*"):
62+
_validate_interval(invalid_interval)
63+
64+
65+
# tests for _validate_date_format
66+
@pytest.mark.parametrize(
67+
"valid_date",
68+
[
69+
"2023-01-01",
70+
"1999-12-31",
71+
"2024-02-29", # Leap year
72+
],
73+
)
74+
def test_validate_date_format_valid(valid_date):
75+
_validate_date_format(valid_date)
76+
77+
78+
@pytest.mark.parametrize(
79+
"invalid_date",
80+
[
81+
"01-01-2023", # Incorrect format
82+
"2023-13-01", # Invalid month
83+
"2023-02-30", # Invalid day
84+
],
85+
)
86+
def test_validate_date_format_invalid(invalid_date):
87+
with pytest.raises(ValueError, match="Date must be in 'YYYY-MM-DD' format."):
88+
_validate_date_format(invalid_date)
89+
90+
91+
def test_validate_date_format_non_string():
92+
match = "Date must be a string in 'YYYY-MM-DD' format."
93+
with pytest.raises(TypeError, match=match):
94+
_validate_date_format(12345)

0 commit comments

Comments
 (0)