Ejemplo n.º 1
0
def test_dc_columns():
    DC = make_dataclass("DC", (("a", str), ("b", str)))
    dcc = dataclass_codec(dataclass=DC)
    assert dcc.columns == ("a", "b")
Ejemplo n.º 2
0
def test_dc_decode_invalid_empty_column():
    DC = make_dataclass("DC", (("a", int), ("b", float)))
    dcc = dataclass_codec(dataclass=DC, columns=["a", "b"])
    with pytest.raises(ValueError):
        dcc.decode(["a", ""])
Ejemplo n.º 3
0
def test_dc_decode_ignore():
    DC = make_dataclass("DC", (("a", str), ("b", str)))
    dcc = dataclass_codec(dataclass=DC, columns=["a", "c", "b", "d"])
    assert dcc.decode(["a", "c", "b", "d"]) == DC(a="a", b="b")
Ejemplo n.º 4
0
def test_dc_decode_empty_columns():
    DC = make_dataclass("DC", (("a", Optional[str]), ("b", str)))
    dcc = dataclass_codec(dataclass=DC, columns=["a", "b", "c"])
    assert dcc.decode(["", ""]) == DC(a=None, b="")
Ejemplo n.º 5
0
def test_dc_decode_codec():
    DC = make_dataclass("DC", (("x", str), ("y", float)))
    codecs = {"y": currency_codec(float, "$")}
    dcc = dataclass_codec(dataclass=DC, codecs=codecs)
    assert dcc.decode(["foo", "$123.45"]) == DC(x="foo", y=123.45)
Ejemplo n.º 6
0
def test_dc_decode_reordered():
    DC = make_dataclass("DC", (("x", int), ("y", float), ("z", date)))
    dcc = dataclass_codec(dataclass=DC, columns=["z", "x", "y"])
    assert dcc.decode(["2021-03-02", "1", "12.35"]) == DC(x=1, y=12.35, z=date(2021, 3, 2))
Ejemplo n.º 7
0
def test_dc_decode_simple():
    DC = make_dataclass("DC", (("x", int), ("y", float), ("z", date)))
    dcc = dataclass_codec(dataclass=DC)
    assert dcc.decode(["1", "12.35", "2021-03-02"]) == DC(x=1, y=12.35, z=date(2021, 3, 2))
Ejemplo n.º 8
0
def test_dc_encode_none():
    DC = make_dataclass("DC", (("x", Optional[str]), ("y", str)))
    dcc = dataclass_codec(dataclass=DC)
    assert dcc.encode(DC(x=None, y="")) == ["", ""]
Ejemplo n.º 9
0
def test_dc_encode():
    DC = make_dataclass("DC", (("x", int), ("y", float), ("z", date)))
    codecs = {"y": percent_codec(int, 2)}
    dcc = dataclass_codec(dataclass=DC, codecs=codecs)
    assert dcc.encode(DC(x=1, y=0.123, z=date(2021, 3, 2))) == ["1", "12.30%", "2021-03-02"]