Exemple #1
0
def test_child():
    child_serializer = serializer(field('b1'))

    a_serializer = serializer(field('a'),
                              child('b', serializer=child_serializer))

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

    test_obj = TestObject()
    target = a_serializer.serialize(test_obj)

    target2 = a_dict_serializer.serialize(test_obj.__class__.__dict__)

    assert {
        'a': 1,
        'b': {
            'b1': 2,
        }
    } == target == target2

    from_json = {
        'a': 1,
        'b': {
            'b1': 2,
        }
    }

    target = a_serializer.deserialize(from_json)

    assert from_json == target
Exemple #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)
Exemple #3
0
def test_docs():

    artist_serializer = serializer(
        field('name', validators=[validators.required()]))

    album_schema = serializer(
        field('title', validators=[validators.required()]),
        field('release_date',
              validators=[validators.required(),
                          validators.datetime()],
              formatters=[formatters.format_datetime()]),
        child('artist',
              serializer=artist_serializer,
              validators=[validators.required()]))

    class Artist(object):
        def __init__(self, name):
            self.name = name

    class Album(object):
        def __init__(self, title, release_date, artist):
            self.title = title
            self.release_date = release_date
            self.artist = artist

    bowie = Artist(name='David Bowie')
    album = Album(artist=bowie,
                  title='Hunky Dory',
                  release_date=datetime.datetime(1971, 12, 17))

    assert album_schema.serialize(album) == {
        'artist': {
            'name': 'David Bowie'
        },
        'release_date': '1971-12-17T00:00:00',
        'title': 'Hunky Dory'
    }
    assert album_schema.deserialize(album_schema.serialize(album)) == {
        'artist': {
            'name': 'David Bowie'
        },
        'release_date': datetime.datetime(1971,
                                          12,
                                          17,
                                          0,
                                          0,
                                          tzinfo=iso8601.UTC),
        'title': 'Hunky Dory'
    }
    input = album_schema.serialize(album)
    del input['artist']
    errors = None
    try:
        album_schema.deserialize(input)
    except Exception as e:
        errors = e.errors

    assert errors == {'artist': ['This field is required']}
Exemple #4
0
def test_empty_child():
    child_serializer = serializer(
        field('b1'),
        field('c1'),
    )

    a_serializer = serializer(field('a'),
                              child('b2', serializer=child_serializer))

    test_obj = TestObject()

    target = a_serializer.serialize(test_obj)

    assert target == {'a': 1, 'b2': {'b1': None, 'c1': None}}

    target = a_serializer.serialize(test_obj, context=serialization_context)

    assert target == {'a': 1}
d2_field = field('d2', validators=[valid_validator, nil_validator])

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)
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)