def test_Schema_validate_raise(): schema = Schema() schema["x"] = IntChoice(choices=(1, 2)) config = Config() config["x"] = 3 with pytest.raises(InvalidChoiceError): validation = schema.validate(config, raise_on_error=True)
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 test_Schema_validate_no_raise(): schema = Schema() schema["x"] = IntChoice(choices=(1, 2)) config = Config() config["x"] = 3 validation = schema.validate(config, raise_on_error=False) assert isinstance(validation["x"], InvalidChoiceError)
def test_Schema_unexpected_section_error(): schema = Schema() schema['x'] = Int() config = Config() config['x'] = {} with pytest.raises(UnexpectedSectionError): schema.validate(config, raise_on_error=True)
def simple_validation(simple_schema_content, simple_section_content): schema = Schema(simple_schema_content) simple_section_content['a'] = -10 simple_section_content['sub']['sa'] = 100.3 simple_section_content['sub']['sb'] = "abc" simple_section_content['sub']['sc'] = True simple_section_content['sub']['subsub']['ssx'] = "omega" simple_section_content['sub']['subsub']['ssy'] = [] config = Config(simple_section_content) return schema.validate(config)
def test_main_read_write_2files(tmpdir): fn_cfg1 = tmpdir.join("2files_cfg1.config").strpath fn_cfg2 = tmpdir.join("2files_cfg2.config").strpath fn_sch1 = tmpdir.join("2files_sch1.schema").strpath fn_sch2 = tmpdir.join("2files_sch2.schema").strpath cfg1 = Config.from_string("""\ a = 10 [c] c1 = 20 """) cfg2 = Config.from_string("""\ b = 1.5 [d] d1 = 2.5 """) sch1 = Schema.from_string("""\ a = Int() [d] d1 = Float() d2 = Float(default=3.5) """) sch2 = Schema.from_string("""\ b = Float() [c] c1 = Int() c2 = Int(default=30) """) cfg1.to_file(fn_cfg1) cfg2.to_file(fn_cfg2) sch1.to_file(fn_sch1) sch2.to_file(fn_sch2) args = ["read", "-i", fn_cfg1, "-i", fn_cfg2, "-s", fn_sch1, "-s", fn_sch2, "--defaults"] log_stream, out_stream = run(args) reference = """\ a = 10 [c] c1 = 20 c2 = 30 b = 1.5 [d] d1 = 2.5 d2 = 3.5 """ # print("=== reference:") # print(reference) # print("=== result:") # print(out_stream.getvalue()) assert out_stream.getvalue() == reference assert log_stream.getvalue() == ""
def test_Config_err_disabled_macros_on_validation(): from zirkon.config import ROOT config = Config(macros=False) schema = Schema() schema['x'] = Int() schema['y'] = Int(min=ROOT['x'] + 2) config['x'] = 3 config['y'] = 5 with pytest.raises(ValueError) as exc_info: validation = schema.validate(config) assert str(exc_info.value) == "cannot evaluate ROOT['x'] + 2: macros are not enabled"
def test_Config_err_disabled_macros_on_validation(): from zirkon.config import ROOT config = Config(macros=False) schema = Schema() schema['x'] = Int() schema['y'] = Int(min=ROOT['x'] + 2) config['x'] = 3 config['y'] = 5 with pytest.raises(ValueError) as exc_info: validation = schema.validate(config) assert str(exc_info.value ) == "cannot evaluate ROOT['x'] + 2: macros are not enabled"
def test_Schema_defaults_defined_in_defaults(use_defaults, has_default): config = Config(defaults=True) config.defaults["x"] = 10 schema = Schema(use_defaults=use_defaults) int_kwargs = {} if has_default: int_kwargs["default"] = 100 schema["x"] = Int(**int_kwargs) assert not schema.validate(config) assert "x" in config.defaults assert config["x"] == 10
def simple_schema(): return Schema.from_string("""\ alpha = Int(default=4) beta = Float(default=2.3) gamma = Str() delta = Bool(default=True) """, fmt="zirkon")
def test_Schema_validate_missing(): schema = Schema.from_string("""\ a = Int(default=1) b = Int() c = Int() [sub] a = Float(default=0.1) b = Float() c = Float() """) config = Config() config['b'] = 2 config['sub'] = {'b': 0.2} validation_result = schema.validate(config, raise_on_error=False) assert 'a' in config assert config['a'] == 1 assert 'b' in config assert config['b'] == 2 assert 'c' not in config assert 'a' in config['sub'] assert config['sub']['a'] == 0.1 assert 'b' in config['sub'] assert config['sub']['b'] == 0.2 assert 'c' not in config['sub'] assert 'a' not in validation_result assert 'b' not in validation_result assert 'c' in validation_result assert isinstance(validation_result['c'], MissingRequiredOptionError) assert 'a' not in validation_result['sub'] assert 'b' not in validation_result['sub'] assert 'c' in validation_result['sub'] assert isinstance(validation_result['sub']['c'], MissingRequiredOptionError)
def test_compile_error(): schema = Schema.from_string("""\ x = Any() """, fmt="zirkon") with pytest.raises(TypeError) as exc_info: parser = create_argparser(schema) assert str(exc_info.value) == "cannot compile validator Any()"
def test_Schema_default_section_subsection_option(): schema = Schema.from_string("""\ [filesystems] [__default_section__] path = Str() [size] min_size_gb = Float(default=0.5) max_size_gb = Float(default=1.5) [files] __default_option__ = Str() [__default_section__] name = Str() __default_option__ = Int() """) config = Config.from_string("""\ [filesystems] [home] path = "/home/user" [files] x = "x.txt" y = "y.txt" z = "y.txt" [a] name = "alpha" v0 = 10 v1 = 20 v2 = 30 [b] name = "beta" v0 = 5 [c] v0 = 1 v1 = "g" v2 = 2 [work] path = "/tmp/user" [size] min_size_gb = 0.0 """) validation = schema.validate(config) assert len(validation) == 1 assert 'filesystems' in validation assert len(validation['filesystems']) == 1 assert 'home' in validation['filesystems'] assert len(validation['filesystems']['home']) == 1 assert 'c' in validation['filesystems']['home'] assert len(validation['filesystems']['home']['c']) == 2 assert 'name' in validation['filesystems']['home']['c'] assert 'v1' in validation['filesystems']['home']['c'] assert validation assert config['filesystems']['home']['size']['min_size_gb'] == 0.5 assert config['filesystems']['home']['size']['max_size_gb'] == 1.5 assert 'a' in config['filesystems']['home'] assert 'b' in config['filesystems']['home'] assert 'c' in config['filesystems']['home'] assert config['filesystems']['work']['size']['min_size_gb'] == 0.0 assert config['filesystems']['work']['size']['max_size_gb'] == 1.5
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 macro_schema(): schema = Schema() schema['a'] = Int() schema['b'] = Int() schema['c'] = IntChoice(choices=(SECTION['a'], SECTION['b'])) schema['sub'] = {} schema['sub']['d'] = Int(min=ROOT['a'], max=ROOT['b']) schema['sub']['e'] = Int(default=ROOT['a'] + ROOT['b'] + SECTION['d']) return schema
def test_create_template_from_schema(string_io): schema = Schema() schema['m0'] = Int(default=3) schema['m1'] = Int(default=ROOT['m0'] + 1) config = Config() create_template_from_schema(schema, config=config) config.dump(stream=string_io) assert string_io.getvalue() == """\
def schema_de(): schema = Schema() schema['alpha'] = Int(default=10) schema['beta'] = Int(default=ROOT['alpha'] - 3) schema['sub'] = {} schema['sub']['x'] = Int(min=ROOT['beta']) schema['sub']['y'] = Int(default=ROOT['alpha'] + SECTION['x']) schema['sub']['z'] = Int(default=0) return schema
def test_Schema_PATH_list(tmpdir, defaults): schema = Schema.from_string("""\ dirs = StrList(default=[PATH.cwd + PATH.sep + "xdir"]) files = StrList(default=[PATH.cwd + PATH.sep + "x.txt"]) """) config = Config.from_string("""\ dirs = ["xdir", PATH.cwd + PATH.sep + "ydir"] """, schema=schema) assert config["dirs"] == ["xdir", os.path.abspath("ydir")] assert config["files"] == [os.path.abspath("x.txt")]
def test_create_argparser_prefix_str(prefix): fqname = _make_fqname(prefix) + ("xval",) argname = argument_name(fqname) argdest = argument_dest(fqname) schema = Schema.from_string("""\ xval = Int(doc="x value") """, fmt="zirkon") parser = create_argparser(schema, prefix=prefix) namespace = parser.parse_args(["{}=2".format(argname)]) assert getattr(namespace, argdest) == 2
def test_Config_use_defaults_False(string_io, use_defaults): config = Config() schema = Schema(use_defaults=use_defaults) schema['x'] = Int(default=10) schema['sub'] = {'y': Int(default=20)} schema['sub']['sub'] = {'z': Int(default=30)} validation = schema.validate(config) assert config['x'] == 10 assert config['sub']['y'] == 20 assert config['sub']['sub']['z'] == 30 assert not validation config.dump(string_io) if use_defaults: assert string_io.getvalue() == """\ [sub] [sub] """ else: assert string_io.getvalue() == """\
def test_main_read_write_evaluate(tmpdir, evaluate, defaults): fn_cfg1 = tmpdir.join("evaluate_cfg1.config").strpath fn_sch1 = tmpdir.join("evaluate_sch1.schema").strpath cfg1 = Config.from_string("""\ a1 = 10 [b] b1 = ROOT['a1'] + 1 b2 = PATH.filename + PATH.extsep + 'b2' """, defaults=defaults) sch1 = Schema.from_string("""\ a1 = Int() a2 = Int(default=SECTION['a1'] * 2) [b] b1 = Int() b2 = Str() b3 = Str(default=PATH.dirname + PATH.sep + "b3") """) cfg1.to_file(fn_cfg1) sch1.to_file(fn_sch1) args = ["read", "-i", fn_cfg1, "-s", fn_sch1, "--defaults"] if evaluate: args.append("-e") log_stream, out_stream = run(args) if evaluate: reference = """\ a1 = 10 [b] b1 = 11 b2 = {b2!r} b3 = {b3!r} a2 = 20 """.format(b2=fn_cfg1 + os.path.extsep + 'b2', b3=os.path.dirname(fn_cfg1) + os.path.sep + 'b3') else: reference = """\ a1 = 10 [b] b1 = ROOT['a1'] + 1 b2 = PATH.filename + PATH.extsep + 'b2' b3 = PATH.dirname + PATH.sep + 'b3' a2 = SECTION['a1'] * 2 """ print("=== evaluate={}".format(evaluate)) print("=== reference:") print(reference) print("=== result:") print(out_stream.getvalue()) assert out_stream.getvalue() == reference assert log_stream.getvalue() == ""
def test_read_write_schema_de(protocol, schema_de): schema_de_string = schema_de.to_string(protocol=protocol) ref_schema_de_string = SCHEMA_DE_STRING.get(protocol, None) if ref_schema_de_string: assert schema_de_string == ref_schema_de_string schema_de_reloaded = Schema.from_string(schema_de_string, protocol=protocol) assert schema_de == schema_de_reloaded schema_de_reloaded_string = schema_de_reloaded.to_string(protocol=protocol) if ref_schema_de_string: assert schema_de_reloaded_string == schema_de_string else: assert schema_de_reloaded.to_string(protocol='zirkon') == schema_de.to_string(protocol='zirkon')
def test_read_write_schema_de(fmt, schema_de): schema_de_string = schema_de.to_string(fmt=fmt) ref_schema_de_string = SCHEMA_DE_STRING.get(fmt, None) if ref_schema_de_string: assert schema_de_string == ref_schema_de_string schema_de_reloaded = Schema.from_string(schema_de_string, fmt=fmt) assert schema_de == schema_de_reloaded schema_de_reloaded_string = schema_de_reloaded.to_string(fmt=fmt) if ref_schema_de_string: assert schema_de_reloaded_string == schema_de_string else: assert compare_dicts(schema_de_reloaded, schema_de)
def schema(): schema = Schema() schema['filename'] = Str() schema['run_mode'] = StrChoice(choices=("alpha", "beta", "gamma")) schema['values'] = {} schema['values']['x'] = Float(min=10.0, max=20.0) schema['values']['y'] = Float(min=10.0, max=20.0) schema['values']['z'] = Float(min=10.0, max=20.0) schema['operation'] = StrChoice(choices=("+", "-")) schema['parameters'] = {} schema['parameters']['max_iterations'] = Int(default=100) schema['parameters']['frequencies'] = FloatList(min_len=2) return schema
def test_Schema_validation_order(): schema = Schema.from_string("""\ __default_option__ = Str() homedir = Str(default="/home/user") """) config = Config.from_string("""\ workdir = SECTION["homedir"] + "/work" """) validation = schema.validate(config) assert not validation assert config["homedir"] == "/home/user" assert config["workdir"] == "/home/user/work"
def test_Schema_defaults_undefined(defaults, use_defaults, has_default): config = Config(defaults=defaults) schema = Schema(use_defaults=use_defaults) int_kwargs = {} if has_default: int_kwargs["default"] = 100 schema["x"] = Int(**int_kwargs) if has_default: assert not schema.validate(config) else: assert schema.validate(config) if has_default: assert "x" in config if defaults: if use_defaults: assert "x" in config.defaults else: assert "x" not in config.defaults else: assert "x" not in config
def test_read_write_schema_de(protocol, schema_de): schema_de_string = schema_de.to_string(protocol=protocol) ref_schema_de_string = SCHEMA_DE_STRING.get(protocol, None) if ref_schema_de_string: assert schema_de_string == ref_schema_de_string schema_de_reloaded = Schema.from_string(schema_de_string, protocol=protocol) assert schema_de == schema_de_reloaded schema_de_reloaded_string = schema_de_reloaded.to_string(protocol=protocol) if ref_schema_de_string: assert schema_de_reloaded_string == schema_de_string else: assert schema_de_reloaded.to_string( protocol='zirkon') == schema_de.to_string(protocol='zirkon')
def test_tdata_namespace(tdata): schema = Schema.from_string( """\ {} = {} """.format(tdata.name, tdata.validator_source), fmt="zirkon") parser = create_argparser(schema) for args, value, parsing_error, validation_error in tdata.checks: try: namespace = parser.parse_args(args) except SystemExit: assert parsing_error continue else: assert not parsing_error assert getattr(namespace, tdata.name) == value
def test_create_template_from_schema(string_io): schema = Schema() schema['m0'] = Int(default=3) schema['m1'] = Int() schema['alpha'] = Float(default=0.5) schema['input_file'] = Str(min_len=1) schema['data'] = {} schema['data']['i'] = Int(max=10) schema['data']['isub'] = {} schema['data']['isub']['a'] = Str() schema['data']['isub']['b'] = Str(default='bc') schema['data']['isub']['c'] = Str(default=SECTION['b'] * ROOT['m0']) schema['data']['isub']['d'] = Str(default=SECTION['b'] * ROOT['m1']) schema['data']['j'] = Int(default=2) config = create_template_from_schema(schema) config.dump(stream=string_io) assert string_io.getvalue() == """\
def tschema(): return Schema.from_string("""\ [pack0] db_url = Int(default=10, doc="the db url") [filesystems] homedir = Str(default="/home/user") __default_option__ = Str() [comp0] alpha = Float(default=1.0, doc="alpha value") beta = Float(default=1.0) [mod0] x0 = Str(default="") [mod1] x1 = Str(default="") [comp1] [mod0] x0 = Str(default="") """, fmt="zirkon")
def test_Schema_default_option(): schema = Schema.from_string("""\ [filesystems] __default_option__ = Str() """) config = Config.from_string("""\ [filesystems] home = "/home/user" work = "/tmp/user" data = 6.5 """) validation = schema.validate(config) assert validation assert len(validation) == 1 assert 'filesystems' in validation assert len(validation['filesystems']) == 1 assert 'data' in validation['filesystems'] assert config['filesystems']['home'] == "/home/user" assert config['filesystems']['work'] == "/tmp/user"
def test_Schema_default_section(): schema = Schema.from_string("""\ [filesystems] [__default_section__] path = Str() max_size_gb = Float(default=1.0) """) config = Config.from_string("""\ [filesystems] [home] path = "/home/user" [work] path = "/tmp/user" max_size_gb = 6.5 """) validation = schema.validate(config) assert not validation assert config['filesystems']['home']['max_size_gb'] == 1.0 assert config['filesystems']['work']['max_size_gb'] == 6.5
def test_create_template_from_schema_with_defaults(include_default_kwargs): schema = Schema.from_string("""\ [a] x = Int(default=11) __default_option__ = Str() [__default_section__] y = Float(default=5.5) """, fmt="zirkon") include_default_sections = include_default_kwargs.get("include_default_sections", False) include_default_options = include_default_kwargs.get("include_default_options", False) print(include_default_kwargs) config = create_template_from_schema(schema, **include_default_kwargs) config.dump() if include_default_options: assert "__default_option__" in config["a"] else: assert not "__default_option__" in config["a"] if include_default_sections: assert "__default_section__" in config else: assert not "__default_section__" in config
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_Schema_create_dictionary_init(dictionary, simple_schema_content, string_io): schema = Schema(dictionary=dictionary, init=simple_schema_content) schema.dump(stream=string_io) assert string_io.getvalue() == SIMPLE_SCHEMA_DUMP assert len(dictionary) > 0
def test_Schema_create_dictionary(dictionary, string_io): schema = Schema(dictionary=dictionary) schema.dump(stream=string_io) assert string_io.getvalue() == ""
def test_Schema_get_serializer_json(): assert isinstance(Schema.get_serializer("json"), JSONSerializer)
def test_Schema_get_serializer_configobj(): assert isinstance(Schema.get_serializer("configobj"), ConfigObjSerializer)
def test_Schema_get_serializer_pickle(): assert isinstance(Schema.get_serializer("pickle"), PickleSerializer)
def test_Schema_get_serializer_zirkon(): assert isinstance(Schema.get_serializer("zirkon"), ZirkonSerializer)
def test_Schema_create_empty(string_io): schema = Schema() schema.dump(stream=string_io) assert string_io.getvalue() == ""
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_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 simple_schema(dictionary, simple_schema_content): schema = Schema(dictionary=dictionary, init=simple_schema_content) return 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