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_Schema_from_file_configobj(simple_schema, tmp_text_file): tmp_text_file.write(SIMPLE_SCHEMA_CONFIGOBJ_SERIALIZATION) tmp_text_file.flush() tmp_text_file.seek(0) schema = Schema.from_file(filename=tmp_text_file.name, fmt="configobj") assert schema == simple_schema
def test_Schema_from_file_json(simple_schema, tmp_text_file): tmp_text_file.write(SIMPLE_SCHEMA_JSON_SERIALIZATION) tmp_text_file.flush() tmp_text_file.seek(0) schema = Schema.from_file(filename=tmp_text_file.name, fmt="json") assert schema == simple_schema
def test_main_read_write(files, i_name_protocol, o_name_protocol, s_name_protocol, v_name_protocol, defaults): i_name, i_protocol, i_hints = i_name_protocol o_name, o_protocol, o_hints = o_name_protocol s_name, s_protocol, s_hints = s_name_protocol v_name, v_protocol, v_hints = v_name_protocol if o_protocol is None: o_protocol = i_protocol with files.tmp(o_name), files.tmp(v_name): i_file_arg = files[i_name] + i_hints o_file_arg = files[o_name] + o_hints if o_name: assert not os.path.exists(files[o_name]) if v_name: assert not os.path.exists(files[v_name]) args = ["read", "-i", i_file_arg, "-o", o_file_arg] if s_name is not None: args.extend(["-s", s_name + s_hints]) if v_name is not None: args.extend(["-V", v_name + v_hints]) if defaults is not None: args.append("--defaults={!r}".format(defaults)) log_stream, out_stream = run(args) if o_name: assert os.path.exists(files[o_name]) config_args = {} if defaults is not None: config_args['defaults'] = defaults print("read in {}:{}".format(files[i_name], i_protocol)) i_config = Config.from_file(files[i_name], protocol=i_protocol, **config_args) print("read out {}:{}".format(files[o_name], o_protocol)) schema = None if s_name is not None: schema = Schema.from_file(files[s_name], protocol=s_protocol) validation = schema.validate(i_config) if v_name is not None: assert os.path.exists(v_name) v_data = Validation.from_file(files[v_name], protocol=v_protocol) print("===") validation.dump() print("===") v_data.dump() assert validation.to_string(protocol="zirkon") == v_data.to_string(protocol="zirkon") if o_name: o_config = Config.from_file(files[o_name], protocol=o_protocol, **config_args) else: o_config = Config.from_string(out_stream.getvalue(), protocol=o_protocol, **config_args) if schema: o_v_data = schema.validate(o_config) assert i_config == o_config out_x_json_path = files[o_name] assert not o_name in files assert not os.path.exists(out_x_json_path)
def test_main_program_schema_ofile(tmpdir): ofile = tmpdir.join("pack0.schema").strpath args = ["program", '-m', 'tests/pack0', '--schema', '-o', ofile] log_stream, out_stream = run(args) schema = Schema.from_file(ofile) assert 'pack0' in schema
def test_Schema_from_file_configobj(simple_schema, tmp_text_file): tmp_text_file.write(SIMPLE_SCHEMA_CONFIGOBJ_SERIALIZATION) tmp_text_file.flush() tmp_text_file.seek(0) schema = Schema.from_file(filename=tmp_text_file.name, protocol="configobj") assert schema == simple_schema
def test_Schema_from_file_json(simple_schema, tmp_text_file): tmp_text_file.write(SIMPLE_SCHEMA_JSON_SERIALIZATION) tmp_text_file.flush() tmp_text_file.seek(0) schema = Schema.from_file(filename=tmp_text_file.name, protocol="json") assert schema == simple_schema