コード例 #1
0
def test_enum_unstructure(enum, dump_strat, data):
    """Dumping enums of primitives converts them to their primitives."""
    converter = Converter(unstruct_strat=dump_strat)

    member = data.draw(sampled_from(list(enum.__members__.values())))

    assert converter.unstructure(member) == member.value
コード例 #2
0
def test_mapping_unstructure(map_and_type, dump_strat):
    # type: (Any, UnstructureStrategy) -> None
    """Dumping a mapping of primitives is a simple copy operation."""
    converter = Converter(unstruct_strat=dump_strat)
    mapping = map_and_type[0]
    dumped = converter.unstructure(mapping)
    assert dumped == mapping
    assert dumped is not mapping
    assert type(dumped) is type(mapping)
コード例 #3
0
def test_roundtrip(cl_and_vals):
    """We dump the class, then we load it."""
    converter = Converter()
    cl, vals = cl_and_vals
    obj = cl(*vals)

    dumped = converter.unstructure(obj)
    loaded = converter.structure(dumped, cl)

    assert obj == loaded
コード例 #4
0
def test_unstructure_hooks(cl_and_vals):
    """
    Unstructure hooks work.
    """
    converter = Converter()
    cl, vals = cl_and_vals
    inst = cl(*vals)

    converter.register_unstructure_hook(cl, lambda val: "test")

    assert converter.unstructure(inst) == "test"
コード例 #5
0
def test_set_unstructure(set_and_type, dump_strat):
    # type: (Any, UnstructureStrategy) -> None
    """Dumping a set of primitives is a simple copy operation."""
    converter = Converter(unstruct_strat=dump_strat)
    assert converter.unstruct_strat is dump_strat
    set = set_and_type[0]
    dumped = converter.unstructure(set)
    assert dumped == set
    if set:
        assert dumped is not set
    assert type(dumped) is type(set)
コード例 #6
0
def test_seq_unstructure(seq_and_type, dump_strat):
    # type: (Any, UnstructureStrategy) -> None
    """Dumping a sequence of primitives is a simple copy operation."""
    converter = Converter(unstruct_strat=dump_strat)
    assert converter.unstruct_strat is dump_strat
    seq = seq_and_type[0]
    dumped = converter.unstructure(seq)
    assert dumped == seq
    if not isinstance(seq, tuple):
        assert dumped is not seq
    assert type(dumped) is type(seq)
コード例 #7
0
def test_attrs_astuple_unstructure(nested_class):
    # type: (Type) -> None
    """Our dumping should be identical to `attrs`."""
    converter = Converter(unstruct_strat=UnstructureStrategy.AS_TUPLE)
    instance = nested_class[0]()
    assert converter.unstructure(instance) == astuple(instance)
コード例 #8
0
def test_attrs_asdict_unstructure(nested_class):
    """Our dumping should be identical to `attrs`."""
    converter = Converter()
    instance = nested_class[0]()
    assert converter.unstructure(instance) == asdict(instance)