Skip to content

Commit

Permalink
Create dates module
Browse files Browse the repository at this point in the history
  • Loading branch information
benhovinga committed May 16, 2024
1 parent 34e9fc5 commit 2e7f421
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
23 changes: 23 additions & 0 deletions aamva/dates.py
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.")
38 changes: 38 additions & 0 deletions aamva/tests/test_dates.py
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")

0 comments on commit 2e7f421

Please sign in to comment.