Skip to content
Snippets Groups Projects
Commit d0cba1fc authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

TEST: test config file templating, and attribute assignment restriction

parent 74b21b0e
No related branches found
No related tags found
1 merge request!4ENH: Config file templating
...@@ -189,6 +189,39 @@ def test_Config_load_config_file(): ...@@ -189,6 +189,39 @@ def test_Config_load_config_file():
assert result[k] == v assert result[k] == v
def test_Config_load_config_file_jinja2_vars():
configtoml = tw.dedent("""
param1 = 1
param2 = {{VAR0}}
[abc]
param3 = {{VAR1}}
param4 = 4
[def]
param5 = 5
{{VAR2}} = 6
""").strip()
env = {
'VAR0' : '2',
'VAR1' : '3',
'VAR2' : 'param6',
}
exp = {
'param1' : '1',
'param2' : '2',
'abc_param3' : '3',
'abc_param4' : '4',
'def_param5' : '5',
'def_param6' : '6',
}
with tempdir():
open('config.toml', 'wt').write(configtoml)
cfg = config.Config.load_config_file('config.toml', env=env)
def test_Config_load_config_file_main_relabelling(): def test_Config_load_config_file_main_relabelling():
configtoml = tw.dedent(""" configtoml = tw.dedent("""
param1 = 1 param1 = 1
...@@ -349,28 +382,34 @@ def test_Config_access(): ...@@ -349,28 +382,34 @@ def test_Config_access():
cfg = config.Config('.') cfg = config.Config('.')
assert cfg.cfgdir == td assert cfg.cfgdir == td
assert len(cfg) == len(exp) assert len(cfg) == len(exp)
assert sorted(cfg.keys()) == sorted(exp.keys()) assert sorted(cfg.keys()) == sorted(exp.keys())
assert sorted(cfg.items()) == sorted(exp.items()) assert sorted(cfg.items()) == sorted(exp.items())
# Order of exp is intentional, to # Order of exp is intentional, to
# match config construction order. # match config construction order.
assert [v1 == v2 for v1, v2 in zip(cfg.values(), exp.values())] assert [v1 == v2 for v1, v2 in zip(cfg.values(), exp.values())]
for k, v in exp.items(): for k, v in exp.items():
assert k in cfg assert k in cfg
assert cfg[k] == v assert cfg[k] == v
assert getattr(cfg, k) == v assert getattr(cfg, k) == v
assert cfg.get(k) == v assert cfg.get(k) == v
assert cfg.getall(param1=0, param2=1, param4=3) == \
{'param1' : 1, 'param2' : 'abc', 'param4' : 3}
assert cfg.getall(param1=0, param2=1, param4=3) == \ assert cfg.gettuple(param1=0, param2=1, param4=3) == (1, 'abc', 3)
{'param1' : 1, 'param2' : 'abc', 'param4' : 3}
assert cfg.gettuple(param1=0, param2=1, param4=3) == (1, 'abc', 3) assert cfg.getall('abc', param1=0, param2=1, param3=3) == \
{'param1' : True, 'param2' : 0.4, 'param3' : 3}
assert cfg.gettuple('abc', param1=0, param2=1, param4=3) == \
(True, 0.4, 3)
assert cfg.getall('abc', param1=0, param2=1, param3=3) == \ # Can add new attributes as normal
{'param1' : True, 'param2' : 0.4, 'param3' : 3} cfg.new_param = 12345
assert cfg.gettuple('abc', param1=0, param2=1, param4=3) == \ # But cannot override old attributes
(True, 0.4, 3) with pytest.raises(config.ConfigError):
cfg.param1 = 6
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment