-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jeff3071/enhancement/date-range-validation
[Enhancement] Date range validation
- Loading branch information
Showing
5 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from datetime import datetime | ||
|
||
|
||
def validate_date_format(dt: str) -> datetime: | ||
date_format = '%Y-%m-%d' | ||
try: | ||
date = datetime.strptime(dt, date_format) | ||
return date | ||
except ValueError as e: | ||
raise ValueError(e) | ||
|
||
|
||
def validate_date_range(start_dt: str, end_dt: str) -> None: | ||
if start_dt: | ||
start_date = validate_date_format(start_dt) | ||
if end_dt: | ||
end_date = validate_date_format(end_dt) | ||
|
||
if start_dt and end_dt and end_date < start_date: | ||
raise ValueError('end_dt cannot be earlier than start_dt.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from baseball_stats_python.utils.utils import validate_date_format, validate_date_range | ||
|
||
|
||
def test_validate_date_format(): | ||
with pytest.raises(ValueError) as e: | ||
validate_date_format('2024/07/01') | ||
assert "does not match format '%Y-%m-%d'" in str(e.value) | ||
|
||
date = validate_date_format('2024-07-01') | ||
assert isinstance(date, datetime) | ||
assert date == datetime(2024, 7, 1) | ||
|
||
|
||
def test_validate_date_range(): | ||
with pytest.raises(ValueError) as e: | ||
validate_date_range('2024-07-02', '2024-07-01') | ||
assert str(e.value) == 'end_dt cannot be earlier than start_dt.' | ||
|
||
with pytest.raises(ValueError) as e: | ||
validate_date_range('2024/07/01', '2024/07/02') | ||
|
||
assert "does not match format '%Y-%m-%d'" in str(e.value) | ||
|
||
validate_date_range('2024-07-01', '2024-07-02') | ||
validate_date_range('2024-07-01', '') | ||
validate_date_range('', '2024-07-02') | ||
validate_date_range('', '') |