Beispiel #1
0
def test_blank_true():
    field = String(name='blank', blank=True)

    output = {}
    mapper_session = get_mapper_session(data={'blank': ''}, output=output)
    field.marshal(mapper_session)
    assert output == {'blank': ''}
Beispiel #2
0
    class UserMapper(Mapper):

        __type__ = dict

        id = String(required=True, read_only=True)
        name = String()
        company = Nested(CompanyMapper)
Beispiel #3
0
def test_blank_true():
    field = String(name='blank', blank=True)

    output = {}
    mapper_session = get_mapper_session(data={'blank': ''}, output=output)
    field.marshal(mapper_session)
    assert output == {'blank': ''}
Beispiel #4
0
def test_supports_unicode():

    field = String(name='unicode')
    output = {}
    data = {'unicode': u"foo \u2192"}
    mapper_session = get_mapper_session(data=data, output=output)
    field.marshal(mapper_session)
    assert output == {'unicode': u'foo →'}
Beispiel #5
0
def test_supports_unicode_from_json():

    field = String(name='unicode')
    output = {}
    data = '{"unicode": "foo \u2192"}'
    mapper_session = get_mapper_session(data=json.loads(data), output=output)
    field.marshal(mapper_session)
    assert output == {'unicode': u'foo →'}
Beispiel #6
0
def test_string_input_cast():

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(data={'name': 123}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': '123'}
Beispiel #7
0
def test_string_input_cast():

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(data={'name': 123}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': '123'}
Beispiel #8
0
def test_supports_unicode():

    field = String(name='unicode')
    output = {}
    data = {'unicode': u"foo \u2192"}
    mapper_session = get_mapper_session(data=data, output=output)
    field.marshal(mapper_session)
    assert output == {'unicode': u'foo →'}
Beispiel #9
0
    class UserMapper(Mapper):

        __type__ = dict

        id = String(required=True, read_only=True)
        name = String()

        __roles__ = {'private': whitelist('name')}
Beispiel #10
0
def test_supports_unicode_from_json():

    field = String(name='unicode')
    output = {}
    data = '{"unicode": "foo \u2192"}'
    mapper_session = get_mapper_session(data=json.loads(data), output=output)
    field.marshal(mapper_session)
    assert output == {'unicode': u'foo →'}
Beispiel #11
0
def test_string_input_unicode():
    # TODO this requires fleshing out some more..

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(data={'name': u'unicöde'}, output=output)
    field.marshal(mapper_session)
    assert output == {'name': u'unicöde'}
Beispiel #12
0
def test_marshal_read_only_string():

    field = String(name='name', read_only=True, required=True)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': 'foo', 'email': '*****@*****.**'}, output=output)
    field.marshal(mapper_session)
    assert output == {}
Beispiel #13
0
    class MapperA(PolymorphicMapper):

        __type__ = TestType
        id = Integer()
        name = String()
        object_type = String()

        __mapper_args__ = {
            'polymorphic_on': 'object_type',
        }
Beispiel #14
0
def test_string_input_unicode():
    # TODO this requires fleshing out some more..

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(data={'name': u'unicöde'},
                                        output=output)
    field.marshal(mapper_session)
    assert output == {'name': u'unicöde'}
Beispiel #15
0
def test_blank_false():
    field = String(name='blank', blank=False)

    output = {}
    mapper_session = get_mapper_session(data={'blank': ''}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'blank': 'bla'}, output=output)
    field.marshal(mapper_session)
    assert output == {'blank': 'bla'}
Beispiel #16
0
def test_is_valid_choice():

    field = String(name='type', choices=['one', 'two'])
    output = {}
    mapper_session = get_mapper_session(data={'type': 'three'}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'type': 'one'}, output=output)
    field.marshal(mapper_session)
    assert output == {'type': 'one'}
Beispiel #17
0
def test_string_input():
    # TODO this requires fleshing out some more..

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': 'foo', 'email': '*****@*****.**'}, output=output)

    field.marshal(mapper_session)
    assert output == {'name': 'foo'}
Beispiel #18
0
def test_max_length_input():
    field = String(name='length', max=3)

    output = {}
    mapper_session = get_mapper_session(data={'length': 'seven'}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'length': 'one'}, output=output)
    field.marshal(mapper_session)
    assert output == {'length': 'one'}
Beispiel #19
0
def test_string_output():
    # TODO this requires fleshing out some more..

    class Foo(object):
        name = 'value'

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(obj=Foo(), output=output)
    field.serialize(mapper_session)
    assert output == {'name': 'value'}
Beispiel #20
0
def test_string_output():
    # TODO this requires fleshing out some more..

    class Foo(object):
        name = 'value'

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(obj=Foo(), output=output)
    field.serialize(mapper_session)
    assert output == {'name': 'value'}
Beispiel #21
0
def test_marshal_read_only_string():

    field = String(name='name', read_only=True, required=True)

    output = {}
    mapper_session = get_mapper_session(data={
        'name': 'foo',
        'email': '*****@*****.**'
    },
                                        output=output)
    field.marshal(mapper_session)
    assert output == {}
Beispiel #22
0
def test_string_input_cast_object():
    """If I pass a nested object instead of a string, it should be cast to a
    string"""

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(
        data={'name': {'nested': 'stuff'}}, output=output)

    field.marshal(mapper_session)
    assert output == {'name': "{'nested': 'stuff'}"}
Beispiel #23
0
def test_string_input():
    # TODO this requires fleshing out some more..

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(data={
        'name': 'foo',
        'email': '*****@*****.**'
    },
                                        output=output)

    field.marshal(mapper_session)
    assert output == {'name': 'foo'}
Beispiel #24
0
def test_string_input_cast_object():
    """If I pass a nested object instead of a string, it should be cast to a
    string"""

    field = String(name='name', required=True)

    output = {}
    mapper_session = get_mapper_session(data={'name': {
        'nested': 'stuff'
    }},
                                        output=output)

    field.marshal(mapper_session)
    assert output == {'name': "{'nested': 'stuff'}"}
Beispiel #25
0
    class MapperA(PolymorphicMapper):

        __type__ = TestType
        id = Integer()
        name = String()
        object_type = String()

        __roles__ = {
            '__default__': whitelist('id'),
            'overview': whitelist('id', 'name', 'object_type'),
        }

        __mapper_args__ = {
            'polymorphic_on': 'object_type',
        }
Beispiel #26
0
    class MapperBase(Mapper):

        __type__ = TestType

        id = Integer()
        name = String()
        users = Collection(Nested(UserMapper, allow_create=True))
Beispiel #27
0
    class MapperBase(Mapper):

        __type__ = TestType

        id = Integer()
        name = String()
        user = Nested(UserMapper, null_default={})
Beispiel #28
0
        class MapperBase(Mapper):

            __type__ = TestType

            id = Integer()
            name = String()

            __roles__ = {'public': object()}
Beispiel #29
0
    class MapperBase(Mapper):

        __type__ = TestType

        id = Integer()
        name = String()

        __roles__ = {'private': blacklist('id')}
Beispiel #30
0
    class MapperBase(Mapper):

        __type__ = TestType

        id = Integer()
        name = String()
        user = Nested(UserMapper, role='private')

        __roles__ = {'private': whitelist('name', 'user')}
Beispiel #31
0
    class MapperBase(Mapper):

        __type__ = TestType

        id = Integer()
        name = String()
        user = Nested(UserMapper, role='private')

        __roles__ = {
            'private': [
                'id',
            ]
        }
Beispiel #32
0
def test_min_max_length_input():
    field = String(name='length', min=3, max=5)

    output = {}
    mapper_session = get_mapper_session(data={'length': 'o'}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'length': 'onemore'},
                                        output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'length': 'four'}, output=output)
    field.marshal(mapper_session)
    assert output == {'length': 'four'}
Beispiel #33
0
def test_is_valid_string_pipe():
    """test piping data through is_valid_string.
    """
    class InvalidString(object):
        def __str__(self):
            raise ValueError('invalid string')

    field = String(name='test')
    invalid_string = InvalidString()
    session = Session(field, invalid_string, {})

    with pytest.raises(FieldInvalid):
        is_valid_string(session)

    session.data = 'yes'
    assert is_valid_string(session) == 'yes'
Beispiel #34
0
def test_is_valid_choice():

    field = String(name='type', choices=['one', 'two'])
    output = {}
    mapper_session = get_mapper_session(data={'type': 'three'}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'type': 'one'}, output=output)
    field.marshal(mapper_session)
    assert output == {'type': 'one'}
Beispiel #35
0
def test_blank_false():
    field = String(name='blank', blank=False)

    output = {}
    mapper_session = get_mapper_session(data={'blank': ''}, output=output)
    with pytest.raises(FieldInvalid):
        field.marshal(mapper_session)

    mapper_session = get_mapper_session(data={'blank': 'bla'}, output=output)
    field.marshal(mapper_session)
    assert output == {'blank': 'bla'}
Beispiel #36
0
    class CompanyMapper(Mapper):

        __type__ = dict

        id = String(required=True, read_only=True)
        name = String()
Beispiel #37
0
    class MapperBase(Mapper):

        __type__ = TestType

        id = Integer()
        name = String()
Beispiel #38
0
    class UserMapper(Mapper):

        __type__ = dict

        id = String()
        name = String()
Beispiel #39
0
    class UserMapper(Mapper):

        __type__ = dict

        id = String(required=True)
        name = String()
Beispiel #40
0
    class UserMapper(Mapper):

        __type__ = TestType

        id = String(required=True, read_only=True)
        name = String()