Exemplo n.º 1
0
def test_enum_serialization() -> None:
    d = DAT(choice=En.x)
    d2 = DAT(choice=En.z)
    ds = autotui.serialize_namedtuple(d)
    ds2 = autotui.serialize_namedtuple(d2)

    assert ds["choice"] == 1
    assert ds2["choice"] == "something"

    assert d == autotui.deserialize_namedtuple(ds, DAT)
    assert d2 == autotui.deserialize_namedtuple(ds2, DAT)
Exemplo n.º 2
0
def test_no_way_to_serialize() -> None:
    a = Action(t=timedelta(seconds=5))
    with pytest.warns(UserWarning,
                      match=r"No known way to serialize timedelta"):
        not_serialized = autotui.serialize_namedtuple(a)
    with pytest.raises(
            TypeError,
            match=r"Object of type timedelta is not JSON serializable"):
        json.dumps(not_serialized)
Exemplo n.º 3
0
def test_no_way_to_serialize_warning() -> None:
    x = X(a=None)  # type: ignore
    with pytest.warns(
            UserWarning,
            match=
            r"No value for non-optional type None, attempting to be serialized to int",
    ):
        sx = autotui.serialize_namedtuple(x)
    assert sx["a"] is None
Exemplo n.º 4
0
def test_no_value_for_collection_non_optional_warning() -> None:
    l = LL(a=None)  # type: ignore
    with pytest.warns(
            UserWarning,
            match=
            r"No value found for non-optional type a, defaulting to empty container",
    ):
        lnt = autotui.serialize_namedtuple(l)
    assert lnt["a"] == []
Exemplo n.º 5
0
def test_supply_serializer_deserializer() -> None:
    cur: datetime = datetime.now()
    timestamp: int = int(cur.timestamp())
    w = WeightData(when=cur, data=Weight("20lbs"))
    wd = autotui.serialize_namedtuple(
        w, type_serializers={Weight: weight_serializer})
    assert type(wd) == dict
    assert wd["data"] == 20.0
    assert wd["when"] == timestamp

    # test dumping to JSON
    assert json.dumps(wd) == '{"when": ' + str(timestamp) + ', "data": 20.0}'

    # JSON, there and back
    w_jsonstr: str = json.dumps(wd)
    w_loaded: Json = json.loads(w_jsonstr)
    w_loaded_obj: Json = autotui.deserialize_namedtuple(
        w_loaded, WeightData, type_deserializers={Weight: weight_deserializer})
    assert int(w_loaded_obj.when.timestamp()) == timestamp
    assert w_loaded_obj.data == Weight("20lbs")

    wd = autotui.serialize_namedtuple(
        w, attr_serializers={"data": weight_serializer})
    assert type(wd) == dict
    assert wd["data"] == 20.0
    assert wd["when"] == timestamp

    bw = autotui.deserialize_namedtuple(
        wd, WeightData, type_deserializers={Weight: weight_deserializer})
    assert type(bw) == type(w)
    # annoyig because of tz_info
    # assert bw.when == cur
    assert int(bw.when.timestamp()) == timestamp
    assert bw.data == Weight("20lbs")

    # supply attr_deserializers instead
    bw = autotui.deserialize_namedtuple(
        wd, WeightData, attr_deserializers={"data": weight_deserializer})
    assert type(bw) == type(w)
    # annoyig because of tz_info
    # assert bw.when == cur
    assert int(bw.when.timestamp()) == timestamp
    assert bw.data == Weight("20lbs")
Exemplo n.º 6
0
def test_basic_serialize() -> None:
    cur: datetime = datetime.now()
    timestamp: int = int(cur.timestamp())
    x = P(a=1, b=2.0, c="test", d=cur)
    xd = autotui.serialize_namedtuple(x)
    assert type(xd) == dict
    assert xd["a"] == 1
    assert xd["b"] == 2
    assert xd["c"] == "test"
    assert xd["d"] == timestamp
    json.dumps(xd)
Exemplo n.º 7
0
def test_int_converts_to_float_no_warning() -> None:

    p = P(a=5, b=5, c="test", d=datetime.now())

    with warnings.catch_warnings(record=True) as record:
        serialized = autotui.serialize_namedtuple(p)
        # not converted when serialized
        assert serialized["b"] == 5

        # since the type hint specifies a float, convert
        # the 5 to a float
        deserialized: P = autotui.deserialize_namedtuple(serialized, to=P)
        assert isinstance(deserialized.b, float)
        assert deserialized.b == 5.0

    assert len(record) == 0