From 34e9fc5ead912ba19f604ecd17de8a263abd7748 Mon Sep 17 00:00:00 2001 From: Ben Hovinga <23349127+benhovinga@users.noreply.github.com> Date: Wed, 15 May 2024 19:59:24 -0230 Subject: [PATCH] create race_ethnicity module --- aamva/race_ethnicity.py | 23 +++++++++++++++++++++++ aamva/tests/test_race_ethnicity.py | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 aamva/race_ethnicity.py create mode 100644 aamva/tests/test_race_ethnicity.py diff --git a/aamva/race_ethnicity.py b/aamva/race_ethnicity.py new file mode 100644 index 0000000..9410346 --- /dev/null +++ b/aamva/race_ethnicity.py @@ -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.") diff --git a/aamva/tests/test_race_ethnicity.py b/aamva/tests/test_race_ethnicity.py new file mode 100644 index 0000000..59886d7 --- /dev/null +++ b/aamva/tests/test_race_ethnicity.py @@ -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")