Skip to content

Commit

Permalink
create race_ethnicity module
Browse files Browse the repository at this point in the history
  • Loading branch information
benhovinga committed May 15, 2024
1 parent 490d3a9 commit 34e9fc5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
23 changes: 23 additions & 0 deletions aamva/race_ethnicity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import NamedTuple


class RaceEthnicity(NamedTuple):
code: str
description: str


RACE_ETHNICITIES = (
RaceEthnicity("AI", "Alaskan or American Indian"),
RaceEthnicity("AP", "Asian or Pacific Islander"),
RaceEthnicity("BK", "Black"),
RaceEthnicity("H", "Hispanic Origin"),
RaceEthnicity("O", "Non-hispanic"),
RaceEthnicity("U", "Unknown"),
RaceEthnicity("W", "White"))


def parse_race_ethnicity(code: str) -> RaceEthnicity:
try:
return tuple(filter(lambda x: x.code == code, RACE_ETHNICITIES))[0]
except IndexError:
raise ValueError(f"Race/Ethnicity code '{code}' not found.")
24 changes: 24 additions & 0 deletions aamva/tests/test_race_ethnicity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

import aamva.race_ethnicity as race_ethnicity

race_ehtnicity_testdata = (
# ((test, expects), ...)
("AI", ("AI", "Alaskan or American Indian")),
("AP", ("AP", "Asian or Pacific Islander")),
("BK", ("BK", "Black")),
("H", ("H", "Hispanic Origin")),
("O", ("O", "Non-hispanic")),
("U", ("U", "Unknown")),
("W", ("W", "White")))


class TestParseRaceEthnicityFunction:
@pytest.mark.parametrize("test, expects", race_ehtnicity_testdata, ids=tuple(map(lambda x: x[0], race_ehtnicity_testdata)))
def test_should_successfully_return_race_ethnicity_tuple(self, test, expects):
assert race_ethnicity.parse_race_ethnicity(test) == expects
assert type(race_ethnicity.parse_race_ethnicity(test)) == race_ethnicity.RaceEthnicity

def test_should_raise_value_error_when_code_not_found(self):
with pytest.raises(ValueError, match='not found'):
race_ethnicity.parse_race_ethnicity("AAA")

0 comments on commit 34e9fc5

Please sign in to comment.