-
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.
- Loading branch information
1 parent
34e9fc5
commit 2e7f421
Showing
2 changed files
with
61 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from datetime import datetime, date | ||
|
||
ISO_FORMAT = "%Y%m%d" | ||
IMPERIAL_FORMAT = "%m%d%Y" | ||
|
||
|
||
def country_date_format(country: str) -> str: | ||
country = country.upper() | ||
match country: | ||
case "CANADA": | ||
return ISO_FORMAT | ||
case "MEXICO": | ||
return ISO_FORMAT | ||
case "USA": | ||
return IMPERIAL_FORMAT | ||
raise ValueError("Provided country is not supported.") | ||
|
||
|
||
def parse_date(date_string: str, format: str) -> date: | ||
try: | ||
return datetime.strptime(date_string, format).date() | ||
except: | ||
raise ValueError("Invalid date format for provided date string.") |
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,38 @@ | ||
import pytest | ||
import datetime | ||
|
||
import aamva.dates as dates | ||
|
||
country_format_testdata = ( | ||
# ((country, expects), ...) | ||
("Canada", "%Y%m%d"), | ||
("Mexico", "%Y%m%d"), | ||
("USA", "%m%d%Y")) | ||
country_format_testdata_ids = tuple(map(lambda x: x[0], country_format_testdata)) | ||
|
||
|
||
class TestCountryDateFormatFunction: | ||
def test_should_raise_value_error_when_given_unsupported_country(self): | ||
with pytest.raises(ValueError, match="not supported"): | ||
dates.country_date_format("England") | ||
|
||
@pytest.mark.parametrize("country, expects", country_format_testdata, ids=country_format_testdata_ids) | ||
def test_should_successfully_return_correct_country_format(self, country, expects): | ||
assert dates.country_date_format(country) == expects | ||
|
||
|
||
date_testdata = ( | ||
# ((*args, expects), ...) | ||
(("20240516", "%Y%m%d"), datetime.date(2024, 5, 16)), # ISO | ||
(("05162024", "%m%d%Y"), datetime.date(2024, 5, 16))) # Imperial | ||
date_testdata_ids = ("ISO", "Imperial") | ||
|
||
|
||
class TestParseDateFunction: | ||
@pytest.mark.parametrize("args, expects", date_testdata, ids=date_testdata_ids) | ||
def test_should_successfully_return_date_object(self, args, expects): | ||
assert dates.parse_date(*args) == expects | ||
|
||
def test_should_raise_value_error_when_invalid_date_format(self): | ||
with pytest.raises(ValueError, match="Invalid date format"): | ||
dates.parse_date("05162024", "%Y%m%d") |