示例#1
0
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)
示例#2
0
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
示例#3
0
def test_can_convert_dst_to_src(scoped_register):
    mappr.register_iso(Src, Dst, mapping=dict(content='text', count='num'))

    dst = Dst()
    result = mappr.convert(Src, dst)

    expected = Src(text='world', num=20)
    assert asdict(result) == asdict(expected)
示例#4
0
def test_can_convert_src_to_dst(scoped_register):
    mappr.register_iso(Src, Dst, mapping=dict(content='text', count='num'))

    src = Src()
    result = mappr.convert(Dst, src)

    expected = Dst(content='hello', count=10)
    assert asdict(result) == asdict(expected)
示例#5
0
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'
示例#6
0
def test_will_create_adhoc_converter_if_strict_is_false(user):
    public = mappr.convert(UserPublic, user, strict=False)
    expected = UserPublic(
        username='******',
        name='Fake',
        last_name='User',
        type=UserType.REGULAR,
        created_at=user.created_at,
    )
    assert public.dict() == expected.dict()
示例#7
0
def test_user_to_wonky(user):
    wonky = mappr.convert(WonkyUser, user)
    expected = WonkyUser(
        nick='fake.user',
        first_name='Fake',
        last_name='User',
        type=UserType.REGULAR,
        joined_at=user.created_at,
    )

    assert wonky.dict() == expected.dict()
示例#8
0
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,
    }
示例#9
0
def test_user_to_internal(user):
    internal = mappr.convert(UserInternal, user)
    expected = UserInternal(
        email='*****@*****.**',
        username='******',
        name='Fake',
        last_name='User',
        type=UserType.REGULAR,
        token='42',
        roles=[],
        perms=[],
        created_at=user.created_at,
        updated_at=user.updated_at,
    )
    assert internal.dict() == expected.dict()
示例#10
0
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)
示例#11
0
def test_auto_generated_converter(scoped_register):
    mappr.register(Src, Dst)

    assert mappr.convert(Dst, Src()) == Dst(text='hello', num=10)
示例#12
0
def test_converter_has_to_be_registered_first_if_strict_is_True(user):
    with pytest.raises(mappr.NoConverter):
        mappr.convert(UserPublic, user, strict=True)
示例#13
0
               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(
    nick='john.doe',
    name='John Doe',
    email='*****@*****.**',
)

# For simple conversions, where the target type attributes are a subset of
# source's attributes, we can just pass ``strict=False`` to let mappr create
# an ad-hoc converter. This will only work if the attribute names are
# exactly the same.
assert mappr.convert(UserPublic, user, strict=False) == UserPublic(
    username='******',
    first_name='John',
)
示例#14
0
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))