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
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
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
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
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
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']
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
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']
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']
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
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'))
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}
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}
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']
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
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
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
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]
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"""
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
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}}
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}}
def test_set_hard_to_copyables(): import threading config = Config(CONFIG_NAME) with config.set(x=threading.Lock()): with config.set(y=1): pass