Exemplo n.º 1
0
def test_many():
    child_serializer = serializer(
        field('c1')
    )

    a_serializer = serializer(
        field('a'),
        many('c', serializer=child_serializer)
    )

    a_dict_serializer = serializer(
        dict_field('a'),
        many('c', serializer=child_serializer, attr_getter=lambda x: x.get('c'))
    )

    test_obj = TestObject()
    target = a_serializer.serialize(test_obj)
    target2 = a_dict_serializer.serialize(TestObject.__dict__)

    reference = {
      'a': 1,
      'c': [{
        'c1': 'a',
      }, {
        'c1': 'a',
      }]
    }

    assert reference == target == target2
    target = a_serializer.deserialize(reference)
    assert target == reference
Exemplo n.º 2
0
def test_back_and_forth():
    class Object1(object):
        c = 'b'

    class Object2(object):
        a = 2
        b = Object1()
        d = [Object1(), Object1()]

    obj = Object2()

    serializer1 = serializer(
        field('c')
    )

    serializer3 = serializer(
        field('a'),
        child('b', serializer=serializer1),
        many('d', serializer=serializer1),
    )

    json_data = {'a': 2, 'b': {'c': 'b'}, 'd': [{'c': 'b'}, {'c': 'b'}]}

    serialized_data = serializer3.serialize(obj)
    assert serialized_data == json_data
    assert serialized_data == serializer3.deserialize(json_data)
Exemplo n.º 3
0
def test_many_full_validation():
    size_serializer = serializer(dict_field('size'))
    test_serializer = many('e',
                           serializer=size_serializer,
                           validators=[raise_validator])
    test_obj = {'e': [{"size": 'a'}, {"size": 'bb'}]}

    target = {}
    errors = None

    try:
        test_serializer.deserialize(test_obj, target)
    except ValidationException as e:
        errors = e.errors

    assert errors == {'e': {'_full_errors': ['Invalid']}}
Exemplo n.º 4
0
def test_nested_required():
    child_serializer = serializer(
        field('c1', validators=[validators.required()])
    )

    a_serializer = serializer(
        field('a'),
        many('c', serializer=child_serializer, validators=[validators.required()])
    )

    reference = {
      'a': 1,
    }

    with pytest.raises(ValidationException):
        a_serializer.deserialize(reference)

    reference = {
      'a': 1,
      'c': [],
    }

    errors = None
    try:
        a_serializer.deserialize(reference)
    except ValidationException as e:
        errors = e.errors

    assert errors == {'c': {'_full_errors': ['This field is required']}}

    reference = {
      'a': 1,
      'c': [{}],
    }

    errors = None
    try:
        a_serializer.deserialize(reference)
    except ValidationException as e:
        errors = e.errors

    assert errors == {'c': {0: {'c1': ['This field is required']}}}
Exemplo n.º 5
0
    c_field,
)

d_serializer = serializer(
    d1_field,
    d2_field,
)

a_serializer_with_child = serializer(
  a_field,
  child('d', serializer=d_serializer)
)

a_e_serializer = serializer(
  a_field,
  many('e', serializer=d_serializer)
)


def test_field_validation():

    from_json = {'a': 1}
    target = {}

    with pytest.raises(ValidationException):
        a_field.deserialize(from_json, target)

    try:
        a_field.deserialize(from_json, target)
    except Exception as e:
        assert hasattr(e, 'errors')
Exemplo n.º 6
0
a_serializer = serializer(
    a_field,
    b_field,
    c_field,
)

d_serializer = serializer(
    d1_field,
    d2_field,
)

a_serializer_with_child = serializer(a_field,
                                     child('d', serializer=d_serializer))

a_e_serializer = serializer(a_field, many('e', serializer=d_serializer))


def test_field_validation():

    from_json = {'a': 1}
    target = {}

    with pytest.raises(ValidationException):
        a_field.deserialize(from_json, target)

    try:
        a_field.deserialize(from_json, target)
    except Exception as e:
        assert hasattr(e, 'errors')
        assert e.errors == {'a': ['Failed', 'Failed, again']}
import strainer

__name__ = 'Strainer'

sub_strainer_serializer = strainer.serializer(
    strainer.field('w'),
    strainer.field('x', attr_getter=lambda obj: obj.x + 10),
    strainer.field('y'),
    strainer.field('z'),
)

complex_strainer_serializer = strainer.serializer(
    strainer.field('foo'),
    strainer.field('bar', attr_getter=lambda obj: obj.bar()),
    strainer.child('sub', serializer=sub_strainer_serializer),
    strainer.many('subs', serializer=sub_strainer_serializer),
)


def serialization_func(obj, many):
    if many:
        return [complex_strainer_serializer.serialize(x) for x in obj]
    else:
        return complex_strainer_serializer.serialize(obj)