def test_readme_example(Account, TransType, Trans):
    "Test encoding the readme example."
    rules = syn.std_ruleset()
    encode_account = rules.lookup(typ=Account, verb=syn.PY2JSON)
    decode_account = rules.lookup(typ=Account, verb=syn.JSON2PY)

    def pythonic():
        return Account(
            "bob",
            [Trans(TransType.withdraw, Decimal("523.33"), date(2019, 4, 4))],
            Decimal("77.00"),
        )

    def jsonic():
        return {
            "user":
            "******",
            "transactions": [{
                "type": "withdraw",
                "amount": Decimal("523.33"),
                "stamp": "2019-04-04"
            }],
            "balance":
            Decimal("77.00"),
        }

    assert encode_account(pythonic()) == jsonic()
    assert decode_account(jsonic()) == pythonic()
Beispiel #2
0
def test_roundtrip(pair):
    typ, py_value = pair
    rs = std_ruleset()
    act = rs.lookup(verb=PY2JSON, typ=typ)
    json_value = act(py_value)
    act2 = rs.lookup(verb=JSON2PY, typ=typ)
    rt_py_value = act2(json_value)
    assert py_value == rt_py_value
Beispiel #3
0
def test_roundtrip_arbitrary_complex(pair):
    typ, py_value = pair
    rs = std_ruleset()
    act = rs.lookup(verb=PY2JSON, typ=typ)
    json_value = act(py_value)
    act2 = rs.lookup(verb=JSON2PY, typ=typ)
    rt_py_value = act2(json_value)
    if not rs.is_ambiguous(typ=typ, threshold=Matches.potential):
        assert py_value == rt_py_value
def test_encoding_of_composite_thing(Thing, Other):
    "Test encoding of a cyclic type."
    rs = syn.std_ruleset()
    encoder = rs.lookup(typ=Thing, verb=syn.PY2JSON)
    decoder = rs.lookup(typ=Thing, verb=syn.JSON2PY)

    def pythonic():
        return Thing(
            foo=False,
            bar=[
                Other(x=3.3, y=date(1944, 4, 4), z=None),
                Other(x=4.4, y=date(1955, 5, 5), z=None),
            ],
            qux=77,
        )

    def jsonic():
        return {
            "foo":
            False,
            "bar": [
                {
                    "x": 3.3,
                    "y": "1944-04-04",
                    "z": None
                },
                {
                    "x": 4.4,
                    "y": "1955-05-05",
                    "z": None
                },
            ],
            "qux":
            77,
        }

    assert encoder(pythonic()) == jsonic()
    assert decoder(jsonic()) == pythonic()
Beispiel #5
0
def test_convert_unions(verb, typ, subj, expect):
    "Test that the unions rule is able to convert possible types."

    action = std_ruleset().lookup(verb=verb, typ=typ)

    assert action(subj) == expect
Beispiel #6
0
def test_simple(typ, py, js):
    rs = std_ruleset()
    act = rs.lookup(verb=PY2JSON, typ=typ)
    assert act(py) == js
    act = rs.lookup(verb=JSON2PY, typ=typ)
    assert act(js) == py
Beispiel #7
0
def test_check_unions(verb, typ, subj):
    "Test that the unions rule is able to verify possible types."

    action = std_ruleset().lookup(verb=verb, typ=typ)

    assert action(subj)