diff --git a/aamva/hair_color.py b/aamva/hair_color.py new file mode 100644 index 0000000..c44dcc5 --- /dev/null +++ b/aamva/hair_color.py @@ -0,0 +1,26 @@ +from typing import NamedTuple + + +class HairColor(NamedTuple): + code: str + color: str + + +HAIR_COLORS = ( + HairColor("BAL", "Bald"), + HairColor("BLK", "Black"), + HairColor("BLN", "Blond"), + HairColor("BRO", "Brown"), + HairColor("GRY", "Gray"), + HairColor("RED", "Red/Auburn"), + HairColor("SDY", "Sandy"), + HairColor("WHI", "White"), + HairColor("UNK", "Unknown")) + + +def parse_hair_color(code: str) -> HairColor: + code = "BRO" if code == "BRN" else code # Some cards use BRN for Brown + try: + return tuple(filter(lambda x: x.code == code, HAIR_COLORS))[0] + except IndexError: + raise ValueError(f"Color code '{code}' not found.") diff --git a/aamva/tests/test_hair_color.py b/aamva/tests/test_hair_color.py new file mode 100644 index 0000000..06773e5 --- /dev/null +++ b/aamva/tests/test_hair_color.py @@ -0,0 +1,27 @@ +import pytest + +import aamva.hair_color as hair_color + +hair_color_testdata = ( + # ((test, expects), ...) + ("BAL", ("BAL", "Bald")), + ("BLK", ("BLK", "Black")), + ("BLN", ("BLN", "Blond")), + ("BRO", ("BRO", "Brown")), + ("BRN", ("BRO", "Brown")), + ("GRY", ("GRY", "Gray")), + ("RED", ("RED", "Red/Auburn")), + ("SDY", ("SDY", "Sandy")), + ("WHI", ("WHI", "White")), + ("UNK", ("UNK", "Unknown"))) + + +class TestParseHairColorFunction: + @pytest.mark.parametrize("test, expects", hair_color_testdata, ids=tuple(map(lambda x: x[0], hair_color_testdata))) + def test_should_successfully_return_hair_color_tuple(self, test, expects): + assert hair_color.parse_hair_color(test) == expects + assert type(hair_color.parse_hair_color(test)) == hair_color.HairColor + + def test_should_raise_value_error_when_code_not_found(self): + with pytest.raises(ValueError, match='not found'): + hair_color.parse_hair_color("AAA")