Esempio n. 1
0
def test_custom_class_serializer():
    def serializer(cls, o):
        if cls is datetime:
            return o.strftime('%d/%m/%y')
        else:
            raise SerdeSkip()

    def deserializer(cls, o):
        if cls is datetime:
            return datetime.strptime(o, '%d/%m/%y')
        else:
            raise SerdeSkip()

    @serde(serializer=serializer, deserializer=deserializer)
    class Foo:
        i: int
        dt1: datetime
        dt2: datetime
        s: Optional[str] = None
        u: Union[str, int] = 10

    dt = datetime(2021, 1, 1, 0, 0, 0)
    f = Foo(10, dt, dt)

    assert to_json(
        f
    ) == '{"i": 10, "dt1": "01/01/21", "dt2": "01/01/21", "s": null, "u": 10}'
    assert f == from_json(Foo, to_json(f))

    assert to_tuple(f) == (10, '01/01/21', '01/01/21', None, 10)
    assert f == from_tuple(Foo, to_tuple(f))
Esempio n. 2
0
def test_field_serialize_override_class_serializer():
    def serializer(cls, o):
        if cls is datetime:
            return o.strftime('%d/%m/%y')
        else:
            raise SerdeSkip()

    def deserializer(cls, o):
        if cls is datetime:
            return datetime.strptime(o, '%d/%m/%y')
        else:
            raise SerdeSkip()

    @serde(serializer=serializer, deserializer=deserializer)
    class Foo:
        i: int
        dt1: datetime
        dt2: datetime = field(
            serializer=lambda x: x.strftime('%y.%m.%d'),
            deserializer=lambda x: datetime.strptime(x, '%y.%m.%d'))

    dt = datetime(2021, 1, 1, 0, 0, 0)
    f = Foo(10, dt, dt)

    assert to_json(f) == '{"i": 10, "dt1": "01/01/21", "dt2": "21.01.01"}'
    assert f == from_json(Foo, to_json(f))

    assert to_tuple(f) == (10, '01/01/21', '21.01.01')
    assert f == from_tuple(Foo, to_tuple(f))
Esempio n. 3
0
def test_override_by_default_serializer():
    def serializer(cls, o):
        if cls is datetime:
            return o.strftime('%d/%m/%y')
        else:
            raise SerdeSkip()

    def deserializer(cls, o):
        if cls is datetime:
            return datetime.strptime(o, '%d/%m/%y')
        else:
            raise SerdeSkip()

    @serde(serializer=serializer, deserializer=deserializer)
    class Foo:
        i: int
        dt1: datetime
        dt2: datetime = field(serializer=default_serializer,
                              deserializer=default_deserializer)

    dt = datetime(2021, 1, 1, 0, 0, 0)
    f = Foo(10, dt, dt)

    assert to_json(
        f) == '{"i": 10, "dt1": "01/01/21", "dt2": "2021-01-01T00:00:00"}'
    assert f == from_json(Foo, to_json(f))

    assert to_tuple(f) == (10, '01/01/21', datetime(2021, 1, 1, 0, 0))
    assert f == from_tuple(Foo, to_tuple(f))
Esempio n. 4
0
def test_custom_class_serializer():
    def serializer(cls, o):
        if cls is datetime:
            return o.strftime('%d/%m/%y')
        else:
            raise SerdeSkip()

    def deserializer(cls, o):
        if cls is datetime:
            return datetime.strptime(o, '%d/%m/%y')
        else:
            raise SerdeSkip()

    @deserialize(deserializer=deserializer)
    @serialize(serializer=serializer)
    @dataclass
    class Foo:
        i: int
        dt1: datetime
        dt2: datetime

    dt = datetime(2021, 1, 1, 0, 0, 0)
    f = Foo(10, dt, dt)

    assert to_json(f) == '{"i": 10, "dt1": "01/01/21", "dt2": "01/01/21"}'
    assert f == from_json(Foo, to_json(f))

    assert to_tuple(f) == (10, '01/01/21', '01/01/21')
    assert f == from_tuple(Foo, to_tuple(f))
Esempio n. 5
0
def test_custom_field_serializer():
    @deserialize
    @serialize
    @dataclass
    class Foo:
        dt1: datetime
        dt2: datetime = field(
            metadata={
                'serde_serializer': lambda x: x.strftime('%d/%m/%y'),
                'serde_deserializer':
                lambda x: datetime.strptime(x, '%d/%m/%y'),
            })
        dt3: Optional[datetime] = field(
            metadata={
                'serde_serializer':
                lambda x: x.strftime('%d/%m/%y') if x else None,
                'serde_deserializer':
                lambda x: datetime.strptime(x, '%d/%m/%y') if x else None,
            })

    dt = datetime(2021, 1, 1, 0, 0, 0)
    f = Foo(dt, dt, None)

    assert to_json(
        f) == '{"dt1": "2021-01-01T00:00:00", "dt2": "01/01/21", "dt3": null}'
    assert f == from_json(Foo, to_json(f))

    assert to_tuple(f) == (datetime(2021, 1, 1, 0, 0), '01/01/21', None)
    assert f == from_tuple(Foo, to_tuple(f))
Esempio n. 6
0
def test_from_tuple():
    p = Pri(10, 'foo', 100.0, True)
    d = (10, 'foo', 100.0, True)
    assert d == astuple(p)
    assert p == from_tuple(Pri, d)

    p = {'p': Pri(10, 'foo', 100.0, True)}
    d = {'p': (10, 'foo', 100.0, True)}
    assert d == astuple(p)
    assert p == from_tuple(Dict[str, Pri], d)

    p = [Pri(10, 'foo', 100.0, True)]
    d = ((10, 'foo', 100.0, True), )
    assert d == astuple(p)
    assert p == from_tuple(List[Pri], d)

    p = (Pri(10, 'foo', 100.0, True), )
    d = ((10, 'foo', 100.0, True), )
    assert d == astuple(p)
    assert p == from_tuple(Tuple[Pri], d)
Esempio n. 7
0
def test_default(se, de):
    from serde import from_dict, from_tuple

    from .data import OptDefault, PriDefault

    p = PriDefault()
    assert p == de(PriDefault, se(p))

    p = PriDefault()
    assert p == from_dict(PriDefault, {})
    assert p == from_dict(PriDefault, {'i': 10})
    assert p == from_dict(PriDefault, {'i': 10, 's': 'foo'})
    assert p == from_dict(PriDefault, {'i': 10, 's': 'foo', 'f': 100.0})
    assert p == from_dict(PriDefault, {
        'i': 10,
        's': 'foo',
        'f': 100.0,
        'b': True
    })
    assert p == from_tuple(PriDefault, (10, 'foo', 100.0, True))

    o = OptDefault()
    assert o == de(OptDefault, se(o))

    o = OptDefault()
    assert o == from_dict(OptDefault, {})
    assert o == from_dict(OptDefault, {"n": None})
    assert o == from_dict(OptDefault, {"n": None, "i": 10})
    assert o == from_tuple(OptDefault, (None, 10))

    o = OptDefault(n=None, i=None)
    assert o == from_dict(OptDefault, {"n": None, "i": None})
    assert o == from_tuple(OptDefault, (None, None))

    assert 10 == dataclasses.fields(PriDefault)[0].default
    assert 'foo' == dataclasses.fields(PriDefault)[1].default
    assert 100.0 == dataclasses.fields(PriDefault)[2].default
    assert True is dataclasses.fields(PriDefault)[3].default