anikolaienko / py-automapper

Python object auto mapper
https://anikolaienko.github.io/py-automapper/
MIT License
56 stars 10 forks source link

Fix mapping of string enum types #17

Closed soldag closed 1 year ago

soldag commented 1 year ago

I noticed that mapping of string enums currently doesn't work as the mapper handles such values as an iterable and returns a list of the individual characters of the enum value. I added a simple fix to handle enum values the same as primitive values.

anikolaienko commented 1 year ago

Nice! Thank you @soldag for contributing. I suggest instead of modifying existing tests, please create a separate test file with tests dedicated to this fix.

anikolaienko commented 1 year ago

I can offer a simple a test file test_str_enums_mapping.py that would test this defect only:

from enum import Enum

from automapper import mapper

class Role(str, Enum):
    Admin = "admin"
    Reporter = "reporter"
    Regular = "regular"

class UserInfo:
    def __init__(self, name: str, role: Role):
        self.name = name
        self.role = role

class PublicUserInfo:
    def __init__(self, name: str, role: Role):
        self.name = name
        self.role = role

def test_map__str_enum_admin_role_mapped_correctly():
    user_info = UserInfo("John Wick", Role.Admin)
    public_user_info = mapper.to(PublicUserInfo).map(user_info)

    assert public_user_info.name == "John Wick"
    assert public_user_info.role == Role.Admin

def test_map__str_enum_reporter_role_mapped_correctly():
    user_info = UserInfo("April O'Neil", Role.Reporter)
    public_user_info = mapper.to(PublicUserInfo).map(user_info)

    assert public_user_info.name == "April O'Neil"
    assert public_user_info.role == Role.Reporter

About code checks I can apply for you or you can checkout CONTRIBUTING page how to setup pre-commit and apply locally pre-commit run --all-files to apply all code check tools on files that are already committed.

soldag commented 1 year ago

Oh, I already created a new test file on my own. Are you ok with that, or should I use your suggestion?

anikolaienko commented 1 year ago

Yours is perfect. Thank you! I'll merge it now.

anikolaienko commented 1 year ago

I'll fix code checks (I suppose it's failing cause of python 3.8) and release new version today.

anikolaienko commented 1 year ago

Fixed tests and released 1.2.2. Thanks for contribution!