1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
from typing import List
from dacite import from_dict, Config
from .fixtures import data, classes
def test_basic_scenario(benchmark):
result = benchmark(from_dict, classes.Simple, data.simple_data)
assert result == classes.Simple(
attr_string=data.simple_data["attr_string"],
attr_string_opt=data.simple_data["attr_string_opt"],
attr_int=data.simple_data["attr_int"],
attr_set=data.simple_data["attr_set"],
attr_list=data.simple_data["attr_list"],
)
def test_union_matching(benchmark):
result = benchmark(from_dict, classes.LongUnion, data.long_union_data)
assert isinstance(result, classes.LongUnion)
assert result.simple_type_union
assert isinstance(result.unrealistically_complex_union, classes.Nested)
assert len(result.unrealistically_complex_union2) == 3
for r in result.unrealistically_complex_union2:
assert isinstance(r, classes.Nested)
def test_strict_unions_match(benchmark):
result = benchmark(from_dict, classes.LongUnion, data.long_union_data, config=Config(strict_unions_match=True))
assert isinstance(result, classes.LongUnion)
assert result.simple_type_union
assert isinstance(result.unrealistically_complex_union, classes.Nested)
assert len(result.unrealistically_complex_union2) == 3
for r in result.unrealistically_complex_union2:
assert isinstance(r, classes.Nested)
def test_collection_of_union(benchmark):
result = benchmark(from_dict, classes.UnionCollection, data.union_collection_data)
assert result == classes.UnionCollection(collection=data.union_collection_data["collection"])
def test_type_hooks(benchmark):
result = benchmark(
from_dict,
classes.LongUnion,
data.long_union_data,
config=Config(type_hooks={str: str.upper, float: lambda v: v * 123.123, list: lambda v: v * 2}),
)
assert isinstance(result, classes.LongUnion)
assert result.simple_type_union
assert isinstance(result.unrealistically_complex_union, classes.Nested)
assert len(result.unrealistically_complex_union2) == 3
for r in result.unrealistically_complex_union2:
assert isinstance(r, classes.Nested)
def test_casting(benchmark):
result = benchmark(from_dict, data_class=classes.Cast, data=data.cast_data, config=Config(cast=[classes.E]))
assert result == classes.Cast(e=classes.E.Z)
def test_forward_references(benchmark):
result = benchmark(
from_dict,
classes.ForwardRef,
data.forward_ref_data,
config=Config(forward_references={"LongUnion": classes.LongUnion}),
)
assert isinstance(result, classes.ForwardRef)
assert isinstance(result.inner, classes.LongUnion)
def test_parsing_multiple_items(benchmark):
def parse_iterable(data_class, elements: List[dict]):
return [from_dict(data_class=data_class, data=item) for item in elements]
result = benchmark(parse_iterable, classes.LongUnion, [data.long_union_data] * 25)
assert len(result) == 25
for r in result:
assert isinstance(r, classes.LongUnion)
|