Exemplo n.º 1
0
def test_config():
    arg_parser = get_arg_parser()
    core_add_args(arg_parser)
    arg_parser.parse_known_args()
    cfg = Config("test")
    cfg.add_config(ConfigTest)
    cfg.init_default_config()
    assert Config.dict() == {
        "configtest": {
            "testvar1": "testing123",
            "testvar2": 12345,
            "testvar3": {
                "mydict": {
                    "foo": "bar",
                    "abc": {
                        "def": "ghi"
                    }
                },
                "myint": 0,
                "mystr": "Hello",
            },
        }
    }
    cfg2 = Config("test2")
    assert cfg.configtest.testvar1 == cfg2.configtest.testvar1
    assert cfg.configtest.testvar1 == Config.configtest.testvar1
    assert cfg.configtest.testvar2 == cfg2.configtest.testvar2
    assert cfg.configtest.testvar3 == cfg2.configtest.testvar3
    Config.configtest.testvar2 += 1
    assert cfg.configtest.testvar2 == 12346
    with pytest.raises(ConfigNotFoundError):
        Config.does_not_exist.foo = "bar"
    with pytest.raises(ConfigNotFoundError):
        cfg.does_not_exist.foo = "bar"
Exemplo n.º 2
0
def test_config_override():
    arg_parser = get_arg_parser()
    core_add_args(arg_parser)
    arg_parser.parse_known_args()
    cfg = Config("test")
    cfg.add_config(ConfigTest)
    cfg.init_default_config()
    assert Config.dict() == {
        "configtest": {
            "testvar1": "testing123",
            "testvar2": 12346,
            "testvar3": {
                "mydict": {
                    "foo": "bar",
                    "abc": {
                        "def": "ghi"
                    }
                },
                "myint": 0,
                "mystr": "Hello",
            },
        }
    }
    ArgumentParser.args.config_override = [
        "configtest.testvar1=testing124",
        "configtest.testvar3.myint=1",
        "configtest.testvar3.mystr=World",
        "configtest.testvar3.mydict.foo=baz",
        "configtest.testvar3.mydict.abc.def=jkl",
    ]
    cfg.override_config(cfg.running_config)
    assert Config.dict() == {
        "configtest": {
            "testvar1": "testing124",
            "testvar2": 12346,
            "testvar3": {
                "mydict": {
                    "foo": "baz",
                    "abc": {
                        "def": "jkl"
                    }
                },
                "myint": 1,
                "mystr": "World",
            },
        }
    }