Beispiel #1
0
def test_typed_loader(obj):
    yml = (f"name: {obj.name}\n"
           "data:\n"
           f"  a: {obj.data['a']}\n"
           f"  b: {obj.data['b']}\n")
    loaded = load(yml, DataClass)
    assert loaded == obj
Beispiel #2
0
def test_typed_list_loader(obj, seq_type):
    yml = (f"- name: {obj.name}\n"
           "  data:\n"
           f"    a: {obj.data['a']}\n"
           f"    b: {obj.data['b']}\n")
    loaded = load(yml, seq_type[DataClass])
    assert loaded == [obj]
Beispiel #3
0
def test_typed_dict_loader(obj):
    yml = (f"key:\n"
           f"  name: {obj.name}\n"
           "  data:\n"
           f"    a: {obj.data['a']}\n"
           f"    b: {obj.data['b']}\n")
    loaded = load(yml, Dict[str, DataClass])
    assert loaded == {"key": obj}
Beispiel #4
0
def test_nested_with_optional(obj):
    _id = random.randrange(0xffff)
    yml = (f"id: {_id}\n"
           f"sub:\n"
           f"  name: {obj.name}\n"
           f"  data:\n"
           f"    a: {obj.data['a']}\n"
           f"    b: {obj.data['b']}\n")
    loaded = load(yml, _NestedWithOpt)
    assert loaded == _NestedWithOpt(_id, obj)
Beispiel #5
0
def test_optional_in_list():
    @dataclass
    class __Simple:
        a: int
        b: int

    expected = [[{"a": 1, "b": 2}]]
    yml = yaml.safe_dump(expected)
    loaded = load(yml, List[List[Optional[__Simple]]])
    assert loaded == [[__Simple(1, 2)]]
Beispiel #6
0
def test_nested_typed_loader(superobj):
    items = superobj.items
    yml = (f"id: {superobj.id}\n"
           f"items:\n")
    yml = yml + "\n".join(
        f"- name: '{o.name}'\n"
        f"  data:\n"
        f"    a: {o.data['a']}\n"
        f"    b: {o.data['b']}\n"
        for o in items
    )
    loaded = load(yml, DataClassWithDataClass)
    assert loaded == superobj
Beispiel #7
0
def test_with_literal():
    from typing import Literal

    @dataclass
    class WithLiteral:
        default_section: Literal["one", "two", "unknown"] = "unknown"
        other: int = 0

    yml = """
default_section: one
"""

    loaded = load(yml, WithLiteral)

    assert loaded == WithLiteral("one")
Beispiel #8
0
def test_with_literal_heterogeneous():
    from typing import Literal

    @dataclass
    class WithLiteral:
        default_section: Literal[1, "2", {1: 2}] = 1
        other: int = 0

    yml = """
default_section:
    1: 2
"""

    loaded = load(yml, WithLiteral)

    assert loaded == WithLiteral({1: 2})
def _cfg_load(cfg_file: str, cfg_class):
    with open(cfg_file, 'r') as src_cfg:
        configs = tyaml.load(src_cfg,
                             cfg_class)  # type: List[BaseConfiguration]
    result = {cfg.name: cfg for cfg in configs}
    return result
Beispiel #10
0
def test_nested_with_optional_none():
    _id = random.randrange(0xffff)
    yml = f"id: {_id}\n"
    loaded = load(yml, _NestedWithOpt)
    assert loaded == _NestedWithOpt(_id)
Beispiel #11
0
def test_multi_union(invalid_type):
    with raises(TypeError):
        loaded = load("", invalid_type)
        print(loaded)
Beispiel #12
0
def test_ignored_types(typ):
    _id = random.randrange(0xffff)
    yml = f"id: {_id}\n"
    loaded = load(yml, typ)
    assert loaded
    assert isinstance(loaded, dict)