Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Card Module #11

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions aamva/card/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from dataclasses import dataclass
from enum import Flag, auto

class CardType(Flag):
ID = auto()
DL = auto()
BOTH = ID | DL

@property
def description(self):
if self is CardType.ID:
return "Identification Card"
elif self is CardType.DL:
return "Driver License Card"
return None


@dataclass
class CardProfile:
card_type: CardType

27 changes: 27 additions & 0 deletions aamva/card/date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations
import datetime


COUNTRY_FORMAT = {
"Canada": "YYYYMMDD",
"Mexico": "YYYYMMDD", # Assuming this format for Mexico, is untested
"USA": "MMDDYYYY"}


class Date(datetime.date):
@classmethod
def parse_country_date(cls, date:str, country:str) -> Date:
date_format = COUNTRY_FORMAT[country]
year_index = date_format.index("YYYY")
month_index = date_format.index("MM")
day_index = date_format.index("DD")
year = int(date[year_index:year_index + 4])
month = int(date[month_index:month_index + 2])
day = int(date[day_index:day_index + 2])
return Date(year, month, day)

def unparse_country_date(self, country: str) -> str:
return COUNTRY_FORMAT[country] \
.replace("YYYY", str(self.year).rjust(4, "0")) \
.replace("MM", str(self.month).rjust(2, "0")) \
.replace("DD", str(self.day).rjust(2, "0"))
File renamed without changes.
Empty file added tests/card/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions tests/card/test___init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

import aamva.card as card

EXAMPLE_DATA = (
# AAMVA Version 1
"@\n\x1e\rANSI 6360000102DL00390187ZV02260032DLDAQ0123456789ABC\n" +
"DAAPUBLIC,JOHN,Q\nDAG123 MAIN STREET\nDAIANYTOWN\nDAJVA\n" +
"DAK123459999 \nDARDM \nDAS \nDAT \nDAU509\nDAW175\n" +
"DAYBL \nDAZBR \nDBA20011201\nDBB19761123\nDBCM\nDBD19961201\r" +
"ZVZVAJURISDICTIONDEFINEDELEMENT\r",

# AAMVA Version 10
"@\n\x1e\rANSI 636000100102DL00410278ZV03190008DLDAQT64235789\n" +
"DCSSAMPLE\nDDEN\nDACMICHAEL\nDDFN\nDADJOHN\nDDGN\nDCUJR\nDCAD\n" +
"DCBK\nDCDPH\nDBD06062019\nDBB06061986\nDBA12102024\nDBC1\n" +
"DAU068 in\nDAYBRO\nDAG2300 WEST BROAD STREET\nDAIRICHMOND\nDAJVA\n" +
"DAK232690000 \nDCF2424244747474786102204\nDCGUSA\nDCK123456789\n" +
"DDAF\nDDB06062018\nDDC06062020\nDDD1\rZVZVA01\r")


def test_card_type_enums():
assert card.CardType.ID.value == 1
assert card.CardType.DL.value == 2
assert card.CardType.BOTH.value == 3
assert card.CardType.ID in card.CardType.BOTH
assert card.CardType.DL in card.CardType.BOTH


def test_card_type_descriptions():
assert card.CardType.ID.description == "Identification Card"
assert card.CardType.DL.description == "Driver License Card"
assert card.CardType.BOTH.description == None
27 changes: 27 additions & 0 deletions tests/card/test_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import datetime

from aamva.card.date import Date


testdata_ids = ("Canada's date format", "Mexico's date format", "USA's date format")
testdata = (
# ((unparsed, country, parsed), ...)
("20240512", "Canada", (2024, 5, 12)),
("20240512", "Mexico", (2024, 5, 12)),
("05122024", "USA", (2024, 5, 12)))


@pytest.mark.parametrize("unparsed, country, parsed", testdata, ids=testdata_ids)
def test_can_parse_country_date(unparsed, country, parsed):
assert Date.parse_country_date(unparsed, country) == datetime.date(*parsed)


@pytest.mark.parametrize("unparsed, country, parsed", testdata, ids=testdata_ids)
def test_can_unparse_country_date(unparsed, country, parsed):
assert Date(*parsed).unparse_country_date(country) == unparsed


@pytest.mark.parametrize("unparsed, country, parsed", testdata, ids=testdata_ids)
def test_can_unparse_country_datetime(unparsed, country, parsed):
assert Date.unparse_country_date(datetime.date(*parsed), country) == unparsed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

import aamva.issuing_authority as issuing_authority
import aamva.card.issuing_authority as issuing_authority


@pytest.fixture
Expand Down
Loading