Exemplo n.º 1
0
def test_cast_to_hit(test_input, hint, expected, caplog):
    with caplog.at_level(logging.WARNING, logger=""):
        casted = cast_to_hint(value=test_input, hint=hint)
    assert casted == expected

    # Check there were no warnings
    assert not caplog.records, "\n".join(r.message for r in caplog.records)
Exemplo n.º 2
0
def test_cast_to_annotation_with_warnings(test_input, hint, expected, caplog):
    with caplog.at_level(logging.WARNING, logger=""):
        casted = cast_to_hint(value=test_input, hint=hint)
        assert casted == expected

        # Check there were some warnings
        assert caplog.records
Exemplo n.º 3
0
def test_cast_to_hint_apis():
    root_config = cast_to_hint({"apis": {
        "my_api": {
            "rpc_timeout": 1
        }
    }}, RootConfig)
    assert root_config.apis["my_api"].rpc_timeout == 1
Exemplo n.º 4
0
    def load_dict(cls, config: dict, set_defaults=True):
        """Instantiate the config from a dictionary"""
        from .structure import RootConfig

        config = config.copy()
        if set_defaults:
            config = set_default_config(config)
        validate_config(config)

        return cls(root_config=cast_to_hint(config, RootConfig))
Exemplo n.º 5
0
def test_cast_to_annotation_custom_class_with_magic_method_on_mapping():
    casted = cast_to_hint({"a": {
        "b": {
            "value": "abc"
        }
    }},
                          hint=NamedTupleWithMappingToCustomObject)
    assert isinstance(casted, NamedTupleWithMappingToCustomObject)
    assert isinstance(casted.a, Mapping)
    assert isinstance(casted.a["b"], CustomClassWithMagicMethod)
    assert casted.a["b"].value == "abc"
Exemplo n.º 6
0
def test_date_parsing_using_dateutil_library():
    assert isinstance(cast_to_hint("2050-09-12T00:00:00+00:00", datetime), datetime)
    assert isinstance(cast_to_hint("2050-09-12T00:00:00+00:00", date), date)
    # The dateutil lib also successfully parses less strictly formatted dates
    assert isinstance(cast_to_hint("2020-01-01 00:00", datetime), datetime)
    assert isinstance(cast_to_hint("2020-01-01 00:00", date), date)
    assert isinstance(cast_to_hint("2050-09-12T00:00:00+00:00", datetime), datetime)
    assert isinstance(cast_to_hint("2050-09-12T00:00:00+00:00", date), date)
Exemplo n.º 7
0
def test_cast_to_hint_validate():
    root_config = cast_to_hint(
        {
            "apis": {
                "my_api": {
                    "validate": {
                        "incoming": True,
                        "outgoing": False
                    }
                }
            }
        }, RootConfig)
    assert root_config.apis["my_api"].validate.incoming == True
    assert root_config.apis["my_api"].validate.outgoing == False
Exemplo n.º 8
0
def test_cast_to_annotation_custom_class_with_magic_method():
    casted = cast_to_hint(value={"value": "abc"},
                          hint=CustomClassWithMagicMethod)
    assert isinstance(casted, CustomClassWithMagicMethod)
    assert casted.value == "abc"
Exemplo n.º 9
0
def test_cast_to_hint_unknown_property():
    root_config = cast_to_hint({"bus": {"foo": "xyz"}}, RootConfig)
    assert not hasattr(root_config.bus, "foo")
Exemplo n.º 10
0
def test_cast_to_hint_ok():
    root_config = cast_to_hint({"bus": {"log_level": "warning"}}, RootConfig)
    assert root_config.bus.log_level == LogLevelEnum.WARNING
Exemplo n.º 11
0
def test_cast_to_annotation(test_input, hint, expected):
    casted = cast_to_hint(value=test_input, hint=hint)
    assert casted == expected
Exemplo n.º 12
0
def test_date_parsing_using_builtin_fromisoformat():
    # Can manage parsing of strictly correct ISO formats
    assert isinstance(cast_to_hint("2050-09-12T00:00:00+00:00", datetime), datetime)
    assert isinstance(cast_to_hint("2050-09-12T00:00:00+00:00", date), date)
Exemplo n.º 13
0
def test_cast_to_annotation_custom_class_with_magic_method_non_mapping():
    casted = cast_to_hint(value="abc", hint=CustomClassWithMagicMethodNonMapping)
    assert isinstance(casted, CustomClassWithMagicMethodNonMapping)
    assert casted.value == "abc"