Beispiel #1
0
def test_contains():
    test_config = Config(config_name)
    test_config.config = {'x': 1, 'y': {'a': 2}}

    assert 'x' in test_config
    assert 'y.a' in test_config
    assert 'y.b' not in test_config
Beispiel #2
0
def test_collect():
    a = {'x': 1, 'y': {'a': 1}}
    b = {'x': 2, 'z': 3, 'y': {'b': 2}}
    env = {ENV_PREFIX + 'W': 4}

    expected = {
        'w': 4,
        'x': 2,
        'y': {
            'a': 1,
            'b': 2
        },
        'z': 3,
    }

    config = Config(CONFIG_NAME)
    with tmpfile(extension='yaml') as fn1:
        with tmpfile(extension='yaml') as fn2:
            with open(fn1, 'w') as f:
                yaml.dump(a, f)
            with open(fn2, 'w') as f:
                yaml.dump(b, f)

            config = config.collect([fn1, fn2], env=env)
            assert config == expected
Beispiel #3
0
def test_env_var_normalization(monkeypatch):
    value = 3
    monkeypatch.setenv('TEST_A_B', str(value))
    config = Config(config_name)

    assert config.get('a_b') == value
    assert config.get('a-b') == value
Beispiel #4
0
def test_env_var_canonical_name(monkeypatch):
    value = 3
    monkeypatch.setenv(ENV_PREFIX + 'A_B', str(value))
    config = Config(CONFIG_NAME)

    assert config.get('a_b') == value
    assert config.get('a-b') == value
Beispiel #5
0
def test_contains():
    test_config = Config(CONFIG_NAME)
    test_config.config = {'x': 1, 'y': {'a': 2}}

    assert 'x' in test_config
    assert 'y.a' in test_config
    assert 'y.b' not in test_config
Beispiel #6
0
def test_collect_env_none():
    os.environ[ENV_PREFIX + 'FOO'] = 'bar'
    config = Config(CONFIG_NAME)
    try:
        config = config.collect([])
        assert config == {'foo': 'bar'}
    finally:
        del os.environ[ENV_PREFIX + 'FOO']
Beispiel #7
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
Beispiel #8
0
def test_collect_env_none():
    os.environ['TEST_FOO'] = 'bar'
    config = Config(config_name)
    try:
        config = config.collect([])
        assert config == {'foo': 'bar'}
    finally:
        del os.environ['TEST_FOO']
Beispiel #9
0
def test_to_dict():
    test_config = Config(CONFIG_NAME)
    test_config.config = {'x': 1, 'y': {'a': 2}}
    d = test_config.to_dict()
    assert d == test_config.config
    # make sure we copied
    d['z'] = 3
    d['y']['b'] = 4
    assert d != test_config.config
    assert d['y'] != test_config.config['y']
Beispiel #10
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
Beispiel #11
0
def test_ensure_file_directory(mkdir, tmpdir):
    a = {'x': 1, 'y': {'a': 1}}

    source = os.path.join(str(tmpdir), 'source.yaml')
    dest = os.path.join(str(tmpdir), 'dest')

    with open(source, 'w') as f:
        yaml.dump(a, f)

    if mkdir:
        os.mkdir(dest)

    config = Config(CONFIG_NAME)
    config.ensure_file(source=source, destination=dest)

    assert os.path.isdir(dest)
    assert os.path.exists(os.path.join(dest, 'source.yaml'))
Beispiel #12
0
def test_refresh():
    defaults = []
    config = Config(CONFIG_NAME, defaults=defaults)

    config.update_defaults({'a': 1})
    assert config.config == {'a': 1}

    config.refresh(paths=[], env={ENV_PREFIX + 'B': '2'})
    assert config.config == {'a': 1, 'b': 2}

    config.refresh(paths=[], env={ENV_PREFIX + 'C': '3'})
    assert config.config == {'a': 1, 'c': 3}
Beispiel #13
0
def test_refresh():
    defaults = []
    config = Config(config_name, defaults=defaults)

    config.update_defaults({'a': 1})
    assert config.config == {'a': 1}

    config.refresh(paths=[], env={'TEST_B': '2'})
    assert config.config == {'a': 1, 'b': 2}

    config.refresh(paths=[], env={'TEST_C': '3'})
    assert config.config == {'a': 1, 'c': 3}
Beispiel #14
0
def test_get():
    test_config = Config(CONFIG_NAME)
    test_config.config = {'x': 1, 'y': {'a': 2}}

    assert test_config.get('x') == 1
    assert test_config['x'] == 1
    assert test_config.get('y.a') == 2
    assert test_config['y.a'] == 2
    assert test_config.get('y.b', 123) == 123
    with pytest.raises(KeyError):
        test_config.get('y.b')
    with pytest.raises(KeyError):
        test_config['y.b']
Beispiel #15
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
Beispiel #16
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
Beispiel #17
0
def test_ensure_file(tmpdir):
    a = {'x': 1, 'y': {'a': 1}}
    b = {'x': 123}

    source = os.path.join(str(tmpdir), 'source.yaml')
    dest = os.path.join(str(tmpdir), 'dest')
    destination = os.path.join(dest, 'source.yaml')

    with open(source, 'w') as f:
        yaml.dump(a, f)

    config = Config(CONFIG_NAME)
    config.ensure_file(source=source, destination=dest, comment=False)

    with open(destination) as f:
        result = yaml.safe_load(f)
    assert result == a

    # don't overwrite old config files
    with open(source, 'w') as f:
        yaml.dump(b, f)

    config.ensure_file(source=source, destination=dest, comment=False)

    with open(destination) as f:
        result = yaml.safe_load(f)
    assert result == a

    os.remove(destination)

    # Write again, now with comments
    config.ensure_file(source=source, destination=dest, comment=True)

    with open(destination) as f:
        text = f.read()
    assert '123' in text

    with open(destination) as f:
        result = yaml.safe_load(f)
    assert not result
Beispiel #18
0
def test_ensure_file_defaults_to_TEST_CONFIG_directory(tmpdir):
    a = {'x': 1, 'y': {'a': 1}}
    source = os.path.join(str(tmpdir), 'source.yaml')
    with open(source, 'w') as f:
        yaml.dump(a, f)

    config = Config('test')
    destination = os.path.join(str(tmpdir), 'test')
    PATH = config.main_path
    try:
        config.main_path = destination
        config.ensure_file(source=source)
    finally:
        config.main_path = PATH

    assert os.path.isdir(destination)
    [fn] = os.listdir(destination)
    assert os.path.split(fn)[1] == os.path.split(source)[1]
Beispiel #19
0
def test_pprint(capsys):
    test_config = Config(CONFIG_NAME)
    test_config.config = {'x': 1, 'y': {'a': 2}}
    test_config.pprint()
    cap_out = capsys.readouterr()[0]
    assert cap_out == """{'x': 1, 'y': {'a': 2}}\n"""
Beispiel #20
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
Beispiel #21
0
def test_rename():
    config = Config(config_name)
    aliases = {'foo-bar': 'foo.bar'}
    config.config = {'foo-bar': 123}
    config.rename(aliases)
    assert config.config == {'foo': {'bar': 123}}
Beispiel #22
0
def test_rename():
    config = Config(CONFIG_NAME)
    aliases = {'foo_bar': 'foo.bar'}
    config.config = {'foo-bar': 123}
    config.rename(aliases)
    assert config.config == {'foo': {'bar': 123}}
Beispiel #23
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