def test_load_dict_into_dataclass_type_is_list():
    data = settings.load_dict_into_dataclass(
        DummyData4,
        {
            "a": ["Euler", "Leonhard"],
            "b": [{
                "required": 123,
                "a": "Test"
            }, {
                "required": 256,
                "b": 666
            }],
        },
    )

    assert data.a == ["Euler", "Leonhard"]
    assert isinstance(data.b, list)
    assert len(data.b) == 2

    assert data.b[0].required == 123.0
    assert data.b[0].a == "Test"
    assert data.b[0].b == 123

    assert data.b[1].required == 256.0
    assert data.b[1].a == "Riemann"
    assert data.b[1].b == 666
def test_load_dict_into_dataclass_type_conversion_fails():
    with raises(settings.SettingsError):
        data = settings.load_dict_into_dataclass(DummyData, {
            "required": 1.23,
            "a": "Euler",
            "b": "hej"
        })
def test_load_dict_into_dataclass_enum_converted():
    data = settings.load_dict_into_dataclass(DummyData3, {
        "val": "A",
    })

    assert data.val == DummyEnum.A

    data = settings.load_dict_into_dataclass(DummyData3, {
        "val": "B",
    })

    assert data.val == DummyEnum.B

    with raises(settings.SettingsError):
        data = settings.load_dict_into_dataclass(DummyData3, {
            "val": "C",
        })
Esempio n. 4
0
    def create_from_dict(cls, data: Mapping[str, Any]):
        from interstate_love_song.settings import load_dict_into_dataclass

        settings = load_dict_into_dataclass(SimpleMapperSettings, data)
        return cls(
            settings.username,
            settings.password_hash,
            settings.resources,
            settings.domains,
        )
def test_load_dict_into_dataclass_converts_type():
    data = settings.load_dict_into_dataclass(DummyData, {
        "required": 1.23,
        "a": 123,
        "b": "321"
    })

    assert abs(data.required - 1.23) < 0.001
    assert data.a == "123"
    assert data.b == 321
def test_load_dict_into_dataclass_defaults_overridden():
    data = settings.load_dict_into_dataclass(DummyData, {
        "required": 1.23,
        "a": "Euler",
        "b": 321
    })

    assert abs(data.required - 1.23) < 0.001
    assert data.a == "Euler"
    assert data.b == 321
def test_load_dict_into_dataclass_type_is_dataclass():
    data = settings.load_dict_into_dataclass(DummyData2, {
        "a": "Euler",
        "b": {
            "required": 1.23,
            "a": 123,
            "b": "321"
        }
    })

    assert data.a == "Euler"
    assert isinstance(data.b, DummyData)
    assert abs(data.b.required - 1.23) < 0.001
    assert data.b.a == "123"
    assert data.b.b == 321
def test_load_dict_into_dataclass_raises_on_missing_property():
    with raises(settings.SettingsError):
        data = settings.load_dict_into_dataclass(DummyData, {})
def test_load_dict_into_dataclass_defaults_set():
    data = settings.load_dict_into_dataclass(DummyData, {"required": 1.23})

    assert abs(data.required - 1.23) < 0.001
    assert data.a == "Riemann"
    assert data.b == 123