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

TEST: Test Config dict properties

parent 85c49cea
No related branches found
No related tags found
1 merge request!3BIP configuration system
......@@ -11,12 +11,14 @@ import tempfile
def touch(fpath):
"""Create a dummy file at fpath."""
with open(fpath, 'wt') as f:
f.write(fpath)
@contextlib.contextmanager
def tempdir():
"""Create and change into a temp directory, deleting it afterwards."""
prevdir = os.getcwd()
with tempfile.TemporaryDirectory() as td:
os.chdir(td)
......@@ -28,6 +30,7 @@ def tempdir():
@contextlib.contextmanager
def mock_directory(contents):
"""Create a temp directory with dummy contents."""
with tempdir() as td:
for c in contents:
touch(c)
......@@ -35,8 +38,7 @@ def mock_directory(contents):
def dicts_equal(da, db):
"""Compare two dicts, ignoring order."""
da = {k : da[k] for k in sorted(da.keys())}
db = {k : db[k] for k in sorted(db.keys())}
print(da)
print(db)
return da == db
......@@ -245,6 +245,10 @@ def test_Config_create():
for k, v in exp.items():
assert cfg[k] == v
with tempdir():
cfg = config.Config('.')
assert len(cfg) == 0
def test_Config_overrides():
configtoml = tw.dedent("""
......@@ -315,4 +319,50 @@ def test_Config_overrides():
def test_Config_access():
pass
configtoml = tw.dedent("""
param1 = 1
param2 = 'abc'
param3 = [1, 2, 3]
[abc]
param1 = true
param2 = 0.4
""").strip()
exp = {
'param1' : 1,
'param2' : 'abc',
'param3' : [1, 2, 3],
'abc_param1' : True,
'abc_param2' : 0.4,
}
with tempdir() as td:
open('config.toml', 'wt').write(configtoml)
cfg = config.Config('.')
assert cfg.cfgdir == td
assert len(cfg) == len(exp)
assert sorted(cfg.keys()) == sorted(exp.keys())
assert sorted(cfg.items()) == sorted(exp.items())
# Order of exp is intentional, to
# match config construction order.
assert [v1 == v2 for v1, v2 in zip(cfg.values(), exp.values())]
for k, v in exp.items():
assert k in cfg
assert cfg[k] == v
assert getattr(cfg, k) == v
assert cfg.get(k) == v
assert cfg.getall(param1=0, param2=1, param4=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)
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