diff --git a/bip/tests/__init__.py b/bip/tests/__init__.py index 4428cf8d0e915a7d908a4b61ea333583fbf3f39f..cc9623cc080ddfb54f39f6f50597381f170ea0ac 100644 --- a/bip/tests/__init__.py +++ b/bip/tests/__init__.py @@ -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 diff --git a/bip/tests/test_bip_utils_config.py b/bip/tests/test_bip_utils_config.py index 6ef4744713ff1b93d1b0d39ecde6f440941cb073..7718106c045400b430a0f53b78a24c410114d7cf 100644 --- a/bip/tests/test_bip_utils_config.py +++ b/bip/tests/test_bip_utils_config.py @@ -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)