Пример #1
0
def test_set_nested():
    config = Config(CONFIG_NAME)
    with config.set({'abc': {'x': 123}}):
        assert config.config['abc'] == {'x': 123}
        with config.set({'abc.y': 456}):
            assert config.config['abc'] == {'x': 123, 'y': 456}
        assert config.config['abc'] == {'x': 123}
    assert 'abc' not in config.config
Пример #2
0
def test_set_kwargs():
    config = Config(CONFIG_NAME)
    with config.set(foo__bar=1, foo__baz=2):
        assert config.config["foo"] == {"bar": 1, "baz": 2}
    assert "foo" not in config.config

    # Mix kwargs and dict, kwargs override
    with config.set({"foo.bar": 1, "foo.baz": 2}, foo__buzz=3, foo__bar=4):
        assert config.config["foo"] == {"bar": 4, "baz": 2, "buzz": 3}
    assert "foo" not in config.config

    # Mix kwargs and nested dict, kwargs override
    with config.set({"foo": {"bar": 1, "baz": 2}}, foo__buzz=3, foo__bar=4):
        assert config.config["foo"] == {"bar": 4, "baz": 2, "buzz": 3}
    assert "foo" not in config.config
Пример #3
0
def test_get_set_canonical_name():
    c = {'x-y': {'a_b': 123}}
    config = Config(CONFIG_NAME)
    config.update(c)

    keys = ['x_y.a_b', 'x-y.a-b', 'x_y.a-b']
    for k in keys:
        assert config.get(k) == 123

    with config.set({'x_y': {'a-b': 456}}):
        for k in keys:
            assert config.get(k) == 456

    # No change to new keys in sub dicts
    with config.set({'x_y': {'a-b': {'c_d': 1}, 'e-f': 2}}):
        assert config.get('x_y.a-b') == {'c_d': 1}
        assert config.get('x_y.e_f') == 2
Пример #4
0
def test_set():
    config = Config(CONFIG_NAME)
    with config.set(abc=123):
        assert config.config['abc'] == 123
        with config.set(abc=456):
            assert config.config['abc'] == 456
        assert config.config['abc'] == 123

    assert 'abc' not in config.config

    with config.set({'abc': 123}):
        assert config.config['abc'] == 123
    assert 'abc' not in config.config

    with config.set({'abc.x': 1, 'abc.y': 2, 'abc.z.a': 3}):
        assert config.config['abc'] == {'x': 1, 'y': 2, 'z': {'a': 3}}
    assert 'abc' not in config.config

    config.config = {}
    config.set({'abc.x': 123})
    assert config.config['abc']['x'] == 123
Пример #5
0
def test_get_set_roundtrip(key):
    value = 123
    config = Config(CONFIG_NAME)
    with config.set({key: value}):
        assert config.get('custom_key') == value
        assert config.get('custom-key') == value
Пример #6
0
def test_set_hard_to_copyables():
    import threading
    config = Config(CONFIG_NAME)
    with config.set(x=threading.Lock()):
        with config.set(y=1):
            pass