Пример #1
0
def test_deserialize_union_type(fx_circle_type, fx_rectangle_type, fx_point,
                                fx_shape_type):
    with raises(ValueError):
        deserialize_union_type(fx_circle_type, {})

    with raises(ValueError):
        deserialize_union_type(fx_circle_type, {'_type': 'shape'})

    with raises(ValueError):
        deserialize_union_type(fx_circle_type, {
            '_type': 'foo',
            '_tag': 'circle'
        })
    with raises(ValueError):
        deserialize_union_type(fx_rectangle_type, {
            '_type': 'shape',
            '_tag': 'circle'
        })

    with raises(ValueError):
        deserialize_union_type(fx_shape_type, {
            '_type': 'shape',
            '_tag': 'semo'
        })

    deserialize = deserialize_union_type(
        fx_rectangle_type, {
            '_type': 'shape',
            '_tag': 'rectangle',
            'upper_left': serialize_record_type(fx_point),
            'lower_right': serialize_record_type(fx_point),
        })
    assert deserialize.upper_left == fx_point
    assert deserialize.lower_right == fx_point
Пример #2
0
def test_deserialize_meta_union(fx_rectangle_type, fx_point, fx_shape_type):
    d = {
        '_type': 'shape',
        '_tag': 'rectangle',
        'upper_left': serialize_record_type(fx_point),
        'lower_right': serialize_record_type(fx_point),
    }
    meta = deserialize_meta(fx_rectangle_type, d)
    union = deserialize_union_type(fx_rectangle_type, d)
    assert meta == union
    meta_from_shape = deserialize_meta(fx_shape_type, d)
    assert meta_from_shape == meta
Пример #3
0
def test_serialize_union_type(fx_point, fx_offset, fx_circle_type,
                              fx_rectangle_type):
    circle = fx_circle_type(fx_point, fx_offset)
    s = {
        '_type': 'shape', '_tag': 'circle',
        'origin': serialize_record_type(fx_point),
        'radius': serialize_boxed_type(fx_offset)
    }
    assert serialize_union_type(circle) == s
    rectangle = fx_rectangle_type(fx_point, fx_point)
    s = {
        '_type': 'shape', '_tag': 'rectangle',
        'upper_left': serialize_record_type(fx_point),
        'lower_right': serialize_record_type(fx_point),
    }
    assert serialize_union_type(rectangle) == s
Пример #4
0
def test_serialize_record_type(fx_point):
    assert serialize_record_type(fx_point) == {
        '_type': 'point',
        'x': 3.14,
        'top': 1.592
    }
Пример #5
0
 def __nirum_serialize__(self):
     return serialize_record_type(self)
Пример #6
0
 def __nirum_serialize__(self) -> typing.Mapping[str, typing.Any]:
     return serialize_record_type(self)