Skip to content

Commit

Permalink
create hair_color module
Browse files Browse the repository at this point in the history
  • Loading branch information
benhovinga committed May 15, 2024
1 parent 03739b0 commit 25a2c74
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions aamva/hair_color.py
Original file line number Diff line number Diff line change
@@ -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.")
27 changes: 27 additions & 0 deletions aamva/tests/test_hair_color.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 25a2c74

Please sign in to comment.