def test_30_loads_with_order_kept(self): cnf = self.psr.loads(self.cnf_s, ac_ordered=True) if self.is_order_kept: self.assertTrue(dicts_equal(cnf, self.cnf, ordered=True), "\n %r\nvs.\n %r" % (cnf, self.cnf)) else: self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_22_dump_w_special_option(self): self.psr.dump(self.cnf, self.cpath, use_single_float=True) cnf = self.psr.load(self.cpath) ref = copy.deepcopy(self.cnf) ref[_bytes("a")] = cnf[_bytes("a")] # single float value. self.assertFalse(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(dicts_equal(cnf, ref), str(cnf))
def test_42_dump_w_special_option(self): TT.Parser().dump(self.cnf, self.cpath, use_single_float=True) cnf = TT.Parser().load(self.cpath) ref = copy.deepcopy(self.cnf) ref[_bytes("a")] = cnf[_bytes("a")] # single float value. self.assertFalse(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(dicts_equal(cnf, ref), str(cnf))
def test_40_update_w_merge__primitives(self): dic = self.mk_mdict(dict(a=1, b="b")) upd = self.mk_mdict(dict(a=2, b="B", c=[1, 2, 3])) dic2 = TT.MergeableDict(**dic.copy()) ref = TT.MergeableDict(**dic.copy()) ref["c"] = upd["c"] dic.update_w_merge(upd) self.assertTrue(dicts_equal(dic, upd)) dic2.update_w_merge(upd, keep=True) self.assertTrue(dicts_equal(dic2, ref))
def test_40_gen_schema__primitive_types(self): self.assertEqual(TT.gen_schema(None), {'type': 'null'}) self.assertEqual(TT.gen_schema(0), {'type': 'integer'}) self.assertEqual(TT.gen_schema("aaa"), {'type': 'string'}) scm = TT.gen_schema([1]) ref_scm = {'items': {'type': 'integer'}, 'type': 'array'} self.assertTrue(dicts_equal(scm, ref_scm)) scm = TT.gen_schema({'a': 1}) ref_scm = {'properties': {'a': {'type': 'integer'}}, 'type': 'object'} self.assertTrue(dicts_equal(scm, ref_scm))
def test_19_dump_and_single_load_with_validation(self): cnf = CNF_0 scm = SCM_0 cnf_path = os.path.join(self.workdir, "cnf_19.json") scm_path = os.path.join(self.workdir, "scm_19.json") TT.dump(cnf, cnf_path) TT.dump(scm, scm_path) self.assertTrue(os.path.exists(cnf_path)) self.assertTrue(os.path.exists(scm_path)) cnf_1 = TT.single_load(cnf_path, ac_schema=scm_path) self.assertFalse(cnf_1 is None) # Validation should succeed. self.assertTrue(dicts_equal(cnf_1, cnf), cnf_1) cnf_2 = cnf.copy() cnf_2["a"] = "aaa" # It's type should be integer not string. cnf_2_path = os.path.join(self.workdir, "cnf_19_2.json") TT.dump(cnf_2, cnf_2_path) self.assertTrue(os.path.exists(cnf_2_path)) cnf_3 = TT.single_load(cnf_2_path, ac_schema=scm_path) self.assertTrue(cnf_3 is None) # Validation should fail.
def test_50_dump__to_stream(self): cpath = self.cpath + ".new.2" with open(cpath, 'wb') as stream: self.psr.dump(self.cnf, stream) cnf = self.psr.load(cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_40_dump__to_stream(self): with self.psr.wopen(self.cpath) as strm: self.psr.dump(self.cnf, strm) cnf = self.psr.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
def test_30_update__wo_replace(self): dic = self.mk_mdict(dict(a=1, b=dict(b=[1, 2], c="C"))) upd = self.mk_mdict(dict(name="foo", a=2, b=dict(b=[3, 4, 5], d="D"))) ref = TT.MergeableDict(**dic.copy()) ref['name'] = upd['name'] dic.update(upd, TT.MS_NO_REPLACE) self.assertTrue(dicts_equal(dic, ref))
def test_10_dump_and_single_load(self): cpath = os.path.join(self.workdir, "a.json") TT.dump(self.cnf, cpath) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(cpath) self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1))
def test_11_dump_and_single_load__to_from_stream(self): cpath = os.path.join(self.workdir, "a.json") TT.dump(self.cnf, open(cpath, 'w')) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(open(cpath)) self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1))
def test_40_gen_schema__primitive_types(self): self.assertEqual(_gen_scm(None), {'type': 'null'}) self.assertEqual(_gen_scm(0), {'type': 'integer'}) self.assertEqual(_gen_scm("aaa"), {'type': 'string'}) scm = _gen_scm([1]) ref_scm = {'items': {'type': 'integer'}, 'type': 'array', 'minItems': 1, 'uniqueItems': True} self.assertTrue(dicts_equal(scm, ref_scm)) scm = _gen_scm(["aaa", "bbb", "aaa"]) ref_scm = {'items': {'type': 'string'}, 'type': 'array', 'minItems': 3, 'uniqueItems': False} self.assertTrue(dicts_equal(scm, ref_scm)) scm = _gen_scm({'a': 1}) ref_scm = {'properties': {'a': {'type': 'integer'}}, 'type': 'object', 'required': ['a']} self.assertTrue(dicts_equal(scm, ref_scm))
def test_39_single_load__w_validation(self): (cnf, scm) = (CNF_0, SCM_0) cpath = os.path.join(self.workdir, "cnf.json") spath = os.path.join(self.workdir, "scm.json") TT.dump(cnf, cpath) TT.dump(scm, spath) cnf1 = TT.single_load(cpath, ac_schema=spath) self.assertTrue(dicts_equal(cnf, cnf1), str(cnf1))
def test_50_update__w_merge_dicts_and_lists(self): dic = self.mk_mdict(dict(name="a", a=1, b=dict(b=[1, 2], c="C"))) upd = self.mk_mdict(dict(a=2, b=dict(b=[3, 4], d="D", e=[1, 2]))) ref = TT.MergeableDict(**dic.copy()) ref['a'] = 2 ref['b'] = TT.MergeableDict(b=[1, 2, 3, 4], c="C", d="D", e=[1, 2]) dic.update(upd, TT.MS_DICTS_AND_LISTS) self.assertTrue(dicts_equal(dic, ref))
def test_42_update_w_merge__lists(self): dic = TT.MergeableDict(a=[1, 2, 3]) upd = TT.MergeableDict(a=[1, 4, 5]) upd2 = TT.MergeableDict(a=1) dic2 = TT.MergeableDict(**dic.copy()) ref = TT.MergeableDict(**dic.copy()) ref["a"] = [1, 2, 3, 4, 5] dic.update_w_merge(upd) self.assertTrue(dicts_equal(dic, upd)) dic2.update_w_merge(upd, merge_lists=True) self.assertTrue(dicts_equal(dic2, ref)) dic2.update_w_merge(upd2, merge_lists=True, keep=True) self.assertTrue(dicts_equal(dic2, ref)) dic2.update_w_merge(upd2, merge_lists=True, keep=False) self.assertTrue(dicts_equal(dic2, upd2))
def test_20_update__w_replace(self): dic = self.mk_mdict(dict(name="a", a=1, b=dict(b=[1, 2], c="C"))) upd = self.mk_mdict(dict(a=2, b=dict(b=[3, 4, 5], d="D"))) ref = TT.MergeableDict(**dic.copy()) ref['a'] = 2 ref['b'] = upd['b'] ref['b']['c'] = dic['b']['c'] dic.update(upd, TT.MS_REPLACE) self.assertTrue(dicts_equal(dic, ref))
def test_48_update_w_merge_dicts__complex_case(self): dic = self.mk_mdict(dict(name="a", a=1, b=dict(b=[1, 2], c="C"), e=[3, 4])) upd = self.mk_mdict(dict(a=2, b=dict(b=[1, 2, 3], d="D"))) ref = TT.MergeableDict(**dic.copy()) ref['a'] = 2 ref['b'] = TT.MergeableDict(b=[1, 2, 3], c="C", d="D") ref['e'] = [3, 4] dic.update_w_merge(upd) self.assertTrue(dicts_equal(dic, ref))
def test_40_gen_schema__primitive_types(self): self.assertEqual(_gen_scm(None), {'type': 'null'}) self.assertEqual(_gen_scm(0), {'type': 'integer'}) self.assertEqual(_gen_scm("aaa"), {'type': 'string'}) scm = _gen_scm([1]) ref_scm = { 'items': { 'type': 'integer' }, 'type': 'array', 'minItems': 1, 'uniqueItems': True } self.assertTrue(dicts_equal(scm, ref_scm)) scm = _gen_scm(["aaa", "bbb", "aaa"]) ref_scm = { 'items': { 'type': 'string' }, 'type': 'array', 'minItems': 3, 'uniqueItems': False } self.assertTrue(dicts_equal(scm, ref_scm)) scm = _gen_scm({'a': 1}) ref_scm = { 'properties': { 'a': { 'type': 'integer' } }, 'type': 'object', 'required': ['a'] } self.assertTrue(dicts_equal(scm, ref_scm))
def test_31_dump_and_load__to_from_stream(self): cnf = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") cpath = os.path.join(self.workdir, "a.json") with open(cpath, 'w') as strm: TT.dump(cnf, strm) self.assertTrue(os.path.exists(cpath)) with open(cpath, 'r') as strm: cnf1 = TT.load(strm, ac_parser="json") self.assertTrue(dicts_equal(cnf, cnf1), "cnf vs. cnf1: %s\n\n%s" % (str(cnf), str(cnf1)))
def test_16_single_load__template(self): if not anyconfig.template.SUPPORTED: return cpath = os.path.join(self.workdir, "a.yaml") open(cpath, 'w').write(CNF_TMPL_0) cnf = TT.single_load(cpath, ac_template=True, ac_context=self.cnf) self.assertTrue(dicts_equal(self.cnf, cnf), str(cnf)) spath = os.path.join(self.workdir, "scm.json") TT.dump(dict(type="integer"), spath) # Validation should fail. cnf2 = TT.single_load(cpath, ac_template=True, ac_context=self.cnf, ac_schema=spath) self.assertTrue(cnf2 is None)
def test_18_single_load__templates(self): if not anyconfig.template.SUPPORTED: return a_path = os.path.join(self.workdir, "a.yml") b_path = os.path.join(self.workdir, "b.yml") a2_path = os.path.join(self.workdir, "x/y/z", "a.yml") open(a_path, 'w').write("{% include 'b.yml' %}") open(b_path, 'w').write(CNF_TMPL_0) os.makedirs(os.path.dirname(a2_path)) open(a2_path, 'w').write("a: 'xyz'") cnf1 = TT.single_load(a_path, ac_template=True, ac_context=self.cnf) self.assertTrue(dicts_equal(self.cnf, cnf1), str(cnf1)) cnf2 = TT.single_load(a2_path, ac_template=True) self.assertEqual(cnf2["a"], "xyz")
def test_12_etree_to_container(self): cnf = TT.etree_to_container(self.root, to_container) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_32_dumps_and_loads__w_options__no_dumper(self): cnf = TT.loads(TT.dumps(self.cnf, "type_not_exist"), "json") self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_30_dumps_and_loads__w_options(self): cnf = TT.loads(TT.dumps(self.cnf, "json", indent=2), "json", ensure_ascii=False) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_12_load__w_options(self): cnf = self.psr.load(self.cpath, use_list=False) ref = copy.deepcopy(self.cnf) ref[_bytes("sect0")][_bytes("c")] = (_bytes("x"), _bytes("y"), _bytes("z")) self.assertTrue(dicts_equal(cnf, ref), str(cnf))
def test_12_loads__safe(self): cnf = TT.Parser.loads(self.cnf_s, safe=True) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_30_dumps_and_loads(self): cnf = TT.loads(TT.dumps(self.cnf, "json"), "json") self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_22_load__safe(self): cnf = TT.Parser.load(self.cpath, safe=True) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_32_dumps__safe(self): cnf = TT.Parser.loads(TT.Parser.dumps(self.cnf, safe=True)) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_12_loads__w_options(self): cnf = self.psr.loads(self.cnf_s, ac_parse_value=True, **self.load_options) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
def test_22_dumps__w_options(self): cnf = self.psr.loads(self.psr.dumps(self.cnf, **self.dump_options), ac_parse_value=True) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
def test_20_dump(self): self.psr.dump(self.cnf, self.cpath) cnf = self.psr.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
def test_20_dumps(self): cnf_s = self.psr.dumps(self.cnf) self.assertTrue(cnf_s) cnf = self.psr.loads(cnf_s) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
def test_50_load_with_order_kept(self): cnf = self.psr.load(self.cpath, ac_ordered=True) if self.is_order_kept: self.assertTrue(list(cnf.keys()), list(self.cnf.keys())) else: self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_12_load__w_options(self): cnf = self.psr.load(self.cpath, parse_int=None) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_42_dump__safe(self): TT.Parser.dump(self.cnf, self.cpath, safe=True) cnf = TT.Parser.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_22_dump__w_special_option(self): self.psr.dump(self.cnf, self.cpath, parse_int=None, indent=3) cnf = self.psr.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_20_array_to_schema_node(self): scm = TT.array_to_schema_node([1], TT._SIMPLETYPE_MAP) ref_scm = {'type': 'integer'} self.assertTrue(dicts_equal(scm, ref_scm), scm)
def test_20_load(self): cnf = TT.Parser.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_30_load__from_stream(self): with self.psr.ropen(self.cpath) as strm: cnf = self.psr.load(strm) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(isinstance(cnf, UpdateWithReplaceDict))
def test_20_load__w_options(self): cnf = TT.Parser.load(self.cpath, Loader=yaml.loader.Loader) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_20_array_to_schema(self): scm = TT.array_to_schema([1]) ref = dict(items=dict(type="integer"), type="array") self.assertTrue(dicts_equal(scm, ref), scm)
def test_40_dump(self): TT.Parser.dump(self.cnf, self.cpath) cnf = TT.Parser.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_22_array_to_schema__empty_array(self): scm = TT.array_to_schema([]) ref = dict(items=dict(type="string"), type="array") self.assertTrue(dicts_equal(scm, ref), scm)
def test_40_dump__w_options(self): TT.Parser.dump(self.cnf, self.cpath, Dumper=yaml.dumper.Dumper) cnf = TT.Parser.load(self.cpath) self.assertTrue(dicts_equal(cnf, self.cnf), str(cnf))
def test_30_object_to_schema_nodes_iter(self): scm = TT.object_to_schema({'a': 1}) ref = dict(type="object", properties=dict(a=dict(type="integer"))) self.assertTrue(dicts_equal(scm, ref), scm)
def test_22_array_to_schema_node__empty_array(self): scm = TT.array_to_schema_node([], TT._SIMPLETYPE_MAP) ref_scm = {'type': 'string'} self.assertTrue(dicts_equal(scm, ref_scm), scm)
def test_44_gen_schema__complex_types(self): scm = TT.gen_schema(self.obj2) self.assertTrue(dicts_equal(scm, self.ref_scm))
def test_22_dumps__w_options(self): cnf = self.psr.loads(self.psr.dumps(self.cnf, use_single_float=True)) ref = copy.deepcopy(self.cnf) ref[_bytes("a")] = cnf[_bytes("a")] # single float value. self.assertFalse(dicts_equal(cnf, self.cnf), str(cnf)) self.assertTrue(dicts_equal(cnf, ref), str(cnf))