def test_main_create_template_evaluate(files, string_io, evaluate): o_name = "xe-template.zirkon" s_name = "xe.zirkon-schema" with files.tmp(o_name): o_file = files[o_name] s_file = files[s_name] assert not os.path.exists(files[o_name]) args = ["create", "-s", s_file, "-o", o_file] if evaluate: args.append("-e") log_stream, out_stream = run(args) config = Config() config.read(o_file, fmt="zirkon") config.dump(string_io) if evaluate: assert string_io.getvalue() == """\ a = 10 b = 20 c = 200 """ else: assert string_io.getvalue() == """\
def test_Config_read_merge_False(tmpdir, cfg_source, cfg2): config = Config(cfg_source) tmpfile = tmpdir.join("read_nomerge.config").strpath cfg2.write(tmpfile) config.read(tmpfile, merge=False) assert compare_dicts(config, cfg2) assert len(config) == 2
def test_Schema_PATH_add_default(tmpdir, defaults): fn_config = tmpdir.join("PATH_add_default.config").strpath fn_schema = tmpdir.join("PATH_add_default.schema").strpath dirname = os.path.dirname(fn_config) config = Config(defaults=defaults) schema = Schema() with open(fn_config, "w") as f_out: f_out.write("""\ [sub] """) config.read(fn_config, merge=True) with open(fn_schema, "w") as f_out: f_out.write("""\ a = Str(default=PATH.dirname + 'a') [sub] b = Str(default=PATH.dirname + 'b') """) schema.read(fn_schema, merge=True) schema.validate(config) assert config['a'] == dirname + 'a' assert config['sub']['b'] == dirname + 'b'
def files(tmpdir): fls = Files(temporary_dir=tmpdir.strpath) os.chdir(tmpdir.strpath) fls.add('x.zirkon') with open(fls["x.zirkon"], "w") as f_out: f_out.write("""\ a = 100 b = 1.4 [sub] name = "xyz" x = 10 [fun0] enable = True value = 0.4 """) config = Config() config.read(fls["x.zirkon"], protocol="zirkon") fls.add("x.json") config.write(fls["x.json"], protocol="json") fls.add("xwrong.zirkon") config.write(fls["xwrong.zirkon"], protocol="json") fls.add('x.zirkon-schema') with open(fls["x.zirkon-schema"], "w") as f_out: f_out.write("""\ a = Int() b = Float() c = Float(default=ROOT['a'] * ROOT['b']) [sub] name = Str(min_len=1) x = Float(min=0.0) y = Float(min=0.0) [fun0] enable = Bool() value = Float() """) schema = Schema.from_file(fls["x.zirkon-schema"], protocol="zirkon") fls.add("x.s-json") schema.to_file(fls["x.s-json"], protocol="json") with open(fls.add("x-def-le.zirkon"), "w") as f_out: f_out.write("""\ n = 10 [sub] n1 = ROOT['n'] + 1 [sub] n2 = ROOT['n'] * ROOT['sub']['n1'] """) with open(fls.add("x-def-ee.zirkon"), "w") as f_out: f_out.write("""\ n = 10 [sub] n1 = 11 [sub] n2 = 110 """) return fls
def test_Config_read_merge_default(tmpdir, cfg_source, cfg2): config = Config(cfg_source) tmpfile = tmpdir.join("read_nomerge.config").strpath cfg2.write(tmpfile) config.read(tmpfile) # config.dump() # print("...") # cfg2.dump() assert compare_dicts(config, cfg2) assert len(config) == 2
def test_Config_read_merge_True(tmpdir, cfg_source, cfg2): cfg1 = Config(cfg_source) config = Config(cfg_source) tmpfile = tmpdir.join("read_nomerge.config").strpath cfg2.write(tmpfile) config.read(tmpfile, merge=True) assert config != cfg2 assert len(config) == 4 assert config["a"] == cfg1["a"] assert config["b"]["b0"] == cfg1["b"]["b0"] assert config["b"]["b1"] == cfg2["b"]["b1"] assert config["c"] == cfg1["c"] assert config["d"] == cfg2["d"]
def test_main_create_template(files, string_io): o_name = "x-template.zirkon" s_name = "x.zirkon-schema" with files.tmp(o_name): o_file = files[o_name] s_file = files[s_name] assert not os.path.exists(files[o_name]) args = ["create", "-s", s_file, "-o", o_file] log_stream, out_stream = run(args) config = Config() config.read(o_file, protocol="zirkon") config.dump(string_io) assert string_io.getvalue() == """\
def test_Config_from_file_default(defconfig, fmt, tmpdir): tmpfile = tmpdir.join("from_file_default.{}".format(fmt)).strpath defconfig.to_file(tmpfile, fmt=fmt) defconfig1 = Config.from_file(tmpfile) assert defconfig1 == defconfig tmpfile1 = tmpdir.join("from_file_default1.{}".format(fmt)).strpath defconfig1.to_file(tmpfile1) if fmt == "pickle": mode = "rb" else: mode = "r" with open(tmpfile, mode) as f_in, open(tmpfile1, mode) as f_in1: if fmt == "toml": assert sorted(f_in.readlines()) == sorted(f_in1.readlines()) else: assert f_in.read() == f_in1.read() defconfig2 = Config() defconfig2.read(tmpfile) assert defconfig2 == defconfig