Skip to content

Commit

Permalink
Fix incomplete map verification
Browse files Browse the repository at this point in the history
  • Loading branch information
jzombie committed Apr 4, 2024
1 parent 938ce11 commit 6169ec6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 5 additions & 3 deletions enum_with_dict/enum_with_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ def map(cls, key_mapping: Dict[Enum, Any]) -> Dict[Enum, Any]:
Raises:
KeyError: If any enum members are missing in the provided mapping.
"""
# Validate the new mapping
cls.validate_mapping_keys({**cls.to_dict(), **{member.name: value for member, value in key_mapping.items()}})


# Map enum members to values
mapped_values = {}
for member in cls:
if member in key_mapping:
mapped_values[member.name] = key_mapping[member]

# Validate the provided key mapping contains all enum members
cls.validate_mapping_keys(mapped_values)

return mapped_values
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='enum_with_dict',
version='0.6.1',
version='0.6.2',
packages=find_packages(),
author='Jeremy Harris',
author_email='[email protected]',
Expand Down
19 changes: 18 additions & 1 deletion test/test_enum_with_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,24 @@ class TestEnum(EnumWithDict):
}

# Perform the mapping and validate
with self.assertRaises(AttributeError):
with self.assertRaises(KeyError):
TestEnum.map(key_mapping)

def test_incomplete_map(self):
"""Test incomplete mapping enum members to values."""
class TestEnum(EnumWithDict):
VALUE_1 = 1
VALUE_2 = 2
VALUE_3 = 3

# Define the key mapping
key_mapping = {
TestEnum.VALUE_1: "some_new_value",
TestEnum.VALUE_2: "another_new_value",
}

# Perform the mapping and validate
with self.assertRaises(KeyError):
TestEnum.map(key_mapping)

def test_keys_retrieval(self):
Expand Down

0 comments on commit 6169ec6

Please sign in to comment.