Beispiel #1
0
def test_structure_forward_ref(class_with_forward_ref_attr, strat):
    """
    Classes with forward_ref field can be unstructured and structured.
    """
    converter = Converter(unstruct_strat=strat)

    unstructured_expected = converter.unstructure(class_with_forward_ref_attr)
    structured = converter.structure(unstructured_expected, C)
    unstructured_actual = converter.unstructure(structured)

    assert structured == class_with_forward_ref_attr
    assert unstructured_actual == unstructured_expected
Beispiel #2
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
Beispiel #3
0
def test_mapping_unstructure(map_and_type, dump_strat):
    """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)
Beispiel #4
0
def test_seq_of_simple_classes_unstructure(cls_and_vals, seq_type):
    """Dumping a sequence of primitives is a simple copy operation."""
    converter = Converter()

    inputs = seq_type(cl(*vals) for cl, vals in cls_and_vals)
    outputs = converter.unstructure(inputs)
    assert type(outputs) == seq_type
    assert all(type(e) is dict for e in outputs)
Beispiel #5
0
def test_enum_unstructure(enum, dump_strat, choice):
    # type: (EnumMeta, UnstructureStrategy) -> None
    """Dumping enums of primitives converts them to their primitives."""
    converter = Converter(unstruct_strat=dump_strat)

    member = choice(list(enum.__members__.values()))

    assert converter.unstructure(member) == member.value
Beispiel #6
0
def test_set_unstructure(set_and_type, dump_strat):
    """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)
Beispiel #7
0
def test_seq_unstructure(seq_and_type, dump_strat):
    """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)
Beispiel #8
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
Beispiel #9
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 _: "test")

    assert converter.unstructure(inst) == "test"
Beispiel #10
0
def enrich_unstructured_wildcat(converter: Converter, obj: WC,
                                unstructured_obj_dict: dict) -> dict:
    wildcat_attrs_names = get_attrs_names(type(obj))
    wildcat_nonattrs_dict = {
        key: converter.unstructure(obj[key])
        for key in obj if key not in wildcat_attrs_names
    }
    # note that typed entries take absolute precedence over untyped in case of collisions.
    # these collisions should generally be prevented at runtime by the wildcat
    # logic that is injected into the type, but if something were to sneak through
    # we would prefer whatever had been set via the attribute.
    return {**wildcat_nonattrs_dict, **unstructured_obj_dict}
Beispiel #11
0
def test_structure_union_explicit(cl_and_vals_a, cl_and_vals_b):
    """Structuring of manually-disambiguable unions works."""
    converter = Converter()
    cl_a, vals_a = cl_and_vals_a
    cl_b, vals_b = cl_and_vals_b

    def dis(obj, _):
        return converter.structure(obj, cl_a)

    converter.register_structure_hook(Union[cl_a, cl_b], dis)

    inst = cl_a(*vals_a)

    assert inst == converter.structure(converter.unstructure(inst),
                                       Union[cl_a, cl_b])
Beispiel #12
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)
Beispiel #13
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)