def test_can_use_setattr_strategy(scoped_register): mappr.register(Src, EmptyConstructor) result = mappr.convert(EmptyConstructor, Src(), strategy=mappr.Strategy.SETATTR) assert result.text == 'hello' assert result.num == 10
def test_constructor_strategy_doesnt_work_without_appropriate_ctor( scoped_register): mappr.register(Person, Account) with pytest.raises(TypeError) as exc_info: mappr.convert(Account, Person(name='John', age=25)) assert 'got an unexpected keyword argument' in str(exc_info.value)
def test_works(scoped_register): mappr.register(User, UserModel, mapping=dict(id=mappr.use_default)) model = mappr.convert(UserModel, User()) assert model.id is None assert model.email == '*****@*****.**' assert model.username == 'test.user'
def test_cannot_register_iso_if_reverse_converter_already_exists( scoped_register): mappr.register(Dst, Src, mapping=dict( content=mappr.alias('text'), count=mappr.alias('num'), )) with pytest.raises(mappr.ConverterAlreadyExists): mappr.register_iso(Src, Dst, mapping=dict(content='text', count='num'))
def test_can_override_strategy_for_single_conversion(scoped_register): mappr.register(Person, Account) account = mappr.convert(Account, Person(name='John', age=25), strategy=mappr.Strategy.SETATTR) assert asdict(account) == { 'name': 'John', 'age': 25, }
def test_can_register_converter_twice_in_non_strict_mode(scoped_register): mappr.register(Src, Dst, mapping=dict( content=mappr.alias('text'), count=mappr.alias('num'), )) mappr.register(Src, Dst, strict=False, mapping=dict( content=mappr.alias('text'), count=mappr.alias('num'), ))
def test_cannot_register_converter_twice_for_the_same_types(scoped_register): mappr.register(Src, Dst, mapping=dict( content=mappr.alias('text'), count=mappr.alias('num'), )) with pytest.raises(mappr.ConverterAlreadyExists): mappr.register(Src, Dst, mapping=dict( content=mappr.alias('text'), count=mappr.alias('num'), ))
def test_use_default(scoped_register): mappr.register(Src, Dst, mapping=dict(num=mappr.use_default)) assert mappr.convert(Dst, Src()) == Dst(text='hello', num=20)
def test_auto_generated_converter(scoped_register): mappr.register(Src, Dst) assert mappr.convert(Dst, Src()) == Dst(text='hello', num=10)
@dataclass class WonkyUser: nick: str first_name: str last_name: str type: UserType joined_at: datetime def dict(self): return asdict(self) mappr.register(User, UserInternal, mapping=dict( id=mappr.set_const(None), pw_hash=mappr.set_const(None), )) mappr.register(User, WonkyUser, mapping=dict( nick=lambda o: o.username, first_name=lambda o: o.name, joined_at=lambda o: o.created_at, )) @mappr.field_iterator(test=lambda cls: issubclass(cls, pydantic.BaseModel)) def _pydantic_iter_fields(model_cls: Any) -> mappr.FieldIterator: yield from model_cls.__fields__.keys()
name: str email: str @dataclass class UserPublic: username: str first_name: str # We can register a mapper from ``User`` to ``Person``. Fields not specified in # mapping will be copied directly. The source type needs the have attributes # that match the name, otherwise an exception is raised. mappr.register(User, Person, mapping=dict( nick=lambda o: o.username, name=lambda o: f"{o.first_name} {o.last_name}", )) # We can now create a an instance of ``User`` so we can test our converter. user = User( username='******', first_name='John', last_name='Doe', email='*****@*****.**', ) # This will use the converter registered above. To allow conversion in the # reverse direction, you need to register the appropriate converter first. # Each converter works only one way. assert mappr.convert(Person, user) == Person(
def test_raises_TypeNotSupported_if_no_iterator_registered(scoped_register): mappr.register(Person, User) with pytest.raises(mappr.TypeNotSupported): mappr.convert(User, Person(name='John', age=25))