def test_22__process_elem_text__wo_attrs_and_children_parse(self): (elem, dic, subdic) = (TT.ET.XML("<a>A</a>"), {}, {}) TT._process_elem_text(elem, dic, subdic, text="#text", ac_parse_value=True) self.assertTrue(dicts_equal(dic, {"a": 'A'})) self.assertTrue(not subdic) (elem, dic, subdic) = (TT.ET.XML("<a>1</a>"), {}, {}) TT._process_elem_text(elem, dic, subdic, text="#text", ac_parse_value=True) self.assertTrue(dicts_equal(dic, {"a": 1})) self.assertTrue(not subdic)
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_36__process_elem_attrs__wo_text_and_children_parse(self): (elem, dic, subdic) = (TT.ET.XML("<a id='1'/>"), {}, {}) TT._process_elem_attrs(elem, dic, subdic, ac_parse_value=True) self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, {"@attrs": {"id": 1}})) (elem, dic, subdic) = (TT.ET.XML("<a id='A'/>"), {}, {}) TT._process_elem_attrs(elem, dic, subdic, ac_parse_value=True) self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, {"@attrs": {"id": 'A'}})) (elem, dic, subdic) = (TT.ET.XML("<a id='true'/>"), {}, {}) TT._process_elem_attrs(elem, dic, subdic, ac_parse_value=True) self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, {"@attrs": {"id": True}}))
def test_20_json_pointer(self): # test case in rfc, http://tools.ietf.org/html/rfc6901 dic = { "foo": ["bar", "baz"], "": 0, "a/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, r"i\\j": 5, r'k\"l': 6, " ": 7, "m~n": 8 } self.assertTrue(dicts_equal(TT.get(dic, "")[0], dic)) self.assertEqual(TT.get(dic, "/foo")[0], ["bar", "baz"]) self.assertEqual(TT.get(dic, "/foo/0")[0], "bar") self.assertEqual(TT.get(dic, "/")[0], 0) self.assertEqual(TT.get(dic, "/a~1b")[0], 1) self.assertEqual(TT.get(dic, "/c%d")[0], 2) self.assertEqual(TT.get(dic, "/e^f")[0], 3) self.assertEqual(TT.get(dic, "/g|h")[0], 4) self.assertEqual(TT.get(dic, r"/i\\j")[0], 5) self.assertEqual(TT.get(dic, r'/k\"l')[0], 6) self.assertEqual(TT.get(dic, "/ ")[0], 7) self.assertEqual(TT.get(dic, "/m~0n")[0], 8)
def test_46__process_children_elems__w_merge_attrs(self): elem = TT.ET.XML("<a z='Z'><x>X</x><y>Y</y></a>") dic = {"a": {"@attrs": {"z": "Z"}}} subdic = dic["a"]["@attrs"] TT._process_children_elems(elem, dic, subdic, merge_attrs=True) self.assertTrue(dicts_equal(dic, {"a": {"x": "X", "y": "Y", "z": "Z"}}), dic)
def test_20_update_wo_replace(self): dic = copy.deepcopy(self.dic) exp = copy.deepcopy(self.dic) exp["e"] = self.upd["e"] TT.merge(dic, self.upd, ac_merge=TT.MS_NO_REPLACE) self.assertTrue(dicts_equal(dic, exp))
def test_10_update_with_replace(self): dic = copy.deepcopy(self.dic) exp = copy.deepcopy(self.upd) exp["name"] = dic["name"] TT.merge(dic, self.upd, ac_merge=TT.MS_REPLACE) self.assertTrue(dicts_equal(dic, exp))
def test_10__namespaces_from_file(self): ref = { "http://example.com/ns/config": '', "http://example.com/ns/config/val": "val" } xmlfile = anyconfig.compat.StringIO(XML_W_NS_S) self.assertTrue(dicts_equal(TT._namespaces_from_file(xmlfile), ref))
def test_10__namespaces_from_file(self): ref = { "http://example.com/ns/config": '', "http://example.com/ns/config/val": "val" } xmlfile = anyconfig.compat.StringIO(TBC.read_from_res("20-00-cnf.xml")) self.assertTrue(dicts_equal(TT._namespaces_from_file(xmlfile), ref))
def test_20_json_pointer(self): # test case in rfc, http://tools.ietf.org/html/rfc6901 dic = {"foo": ["bar", "baz"], "": 0, "a/b": 1, "c%d": 2, "e^f": 3, "g|h": 4, r"i\\j": 5, r'k\"l': 6, " ": 7, "m~n": 8} self.assertTrue(dicts_equal(TT.get(dic, "")[0], dic)) self.assertEqual(TT.get(dic, "/foo")[0], ["bar", "baz"]) self.assertEqual(TT.get(dic, "/foo/0")[0], "bar") self.assertEqual(TT.get(dic, "/")[0], 0) self.assertEqual(TT.get(dic, "/a~1b")[0], 1) self.assertEqual(TT.get(dic, "/c%d")[0], 2) self.assertEqual(TT.get(dic, "/e^f")[0], 3) self.assertEqual(TT.get(dic, "/g|h")[0], 4) self.assertEqual(TT.get(dic, r"/i\\j")[0], 5) self.assertEqual(TT.get(dic, r'/k\"l')[0], 6) self.assertEqual(TT.get(dic, "/ ")[0], 7) self.assertEqual(TT.get(dic, "/m~0n")[0], 8)
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_30_update_with_merge(self): dic = copy.deepcopy(self.dic) exp = copy.deepcopy(self.upd) exp["b"]["c"] = dic["b"]["c"] exp["name"] = dic["name"] TT.merge(dic, self.upd, ac_merge=TT.MS_DICTS) self.assertTrue(dicts_equal(dic, exp))
def test_42__process_children_elems__w_attr(self): (elem, dic) = (TT.ET.XML("<list id='xyz'><i>A</i><i>B</i></list>"), {}) subdic = {"id": "xyz"} ref = subdic.copy() ref.update({"#children": [{"i": "A"}, {"i": "B"}]}) TT._process_children_elems(elem, dic, subdic, children="#children") self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, ref), subdic)
def _try_load(self, fname="0.msgpack"): try: filepath = os.path.join(_CURDIR, "res", fname) cnf = anyconfig.load(filepath, ac_parser="msgpack") except anyconfig.UnknownFileTypeError: print("all types=%r" % anyconfig.list_types()) raise self.assertTrue(dicts_equal(cnf, CNF_0), "%r vs. %r" % (cnf, CNF_0))
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_50_update_with_custom_merge(self): def set_none_merge_strat(self, other, key, *args, **kwargs): for k in self: self[k] = None dic = copy.deepcopy(self.dic) exp = dict(zip(dic.keys(), [None for _ in dic])) TT.merge(dic, self.upd, ac_merge=set_none_merge_strat) self.assertTrue(dicts_equal(dic, exp))
def test_22_load__explicit_use(self): cnf = self._load_helper(ac_parser=_CID) ref = { _b('a'): 0, _b('b'): _b('bbb'), _b('c'): 5, _b('sect0'): { _b('d'): [_b('x'), _b('y'), _b('z')] } } self.assertTrue(dicts_equal(cnf, ref))
def test_20_load(self): cnf = self._load_helper() ref = { _b('a'): 0, _b('b'): _b('bbb'), _b('c'): 5, _b('sect0'): { _b('d'): [_b('x'), _b('y'), _b('z')] } } self.assertTrue(dicts_equal(cnf, ref))
def test_18_single_load__templates(self): if not anyconfig.template.SUPPORTED: return a_path = os.path.join(resdir(), "30-00-cnf.json") a2_path = os.path.join(resdir(), "00-00-cnf.json") # Not a template 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"], 1)
def test_20_load(self): try: cnf = anyconfig.load(self.conf_path) except anyconfig.UnknownFileTypeError: for entry in pkg_resources.iter_entry_points("anyconfig_backends"): print("%r" % entry) psr = entry.load() print("%r" % psr) print("type=%r, exts=%r" % (psr.type(), psr.extensions())) print("all types=%r" % anyconfig.list_types()) raise self.assertTrue(dicts_equal(cnf, _CNF_0))
def _load_and_dump_with_opened_files(self, filename, rmode='r', wmode='w', **oopts): cpath = os.path.join(self.workdir, filename) with TT.open(cpath, 'w', **oopts) as out: TT.dump(self.cnf, out) self.assertTrue(_is_file_object(out)) self.assertEqual(out.mode, wmode) with TT.open(cpath, 'rb', **oopts) as inp: cnf1 = TT.single_load(inp) self.assertTrue(_is_file_object(inp)) self.assertEqual(inp.mode, rmode) cpair = (self.cnf, cnf1) self.assertTrue(dicts_equal(*cpair), "%r vs. %r" % cpair)
def test_20_load(self): try: cnf = anyconfig.load(self.conf_path) except anyconfig.UnknownFileTypeError: print("all types=%r" % anyconfig.list_types()) raise ref = { _b('a'): 0, _b('b'): _b('bbb'), _b('c'): 5, _b('sect0'): { _b('d'): [_b('x'), _b('y'), _b('z')] } } self.assertTrue(dicts_equal(cnf, ref))
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_30__process_elem_attrs__wo_text_and_children(self): (elem, dic, subdic) = (TT.ET.XML("<a id='A'/>"), {}, {}) TT._process_elem_attrs(elem, dic, subdic) self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, {"@attrs": {"id": 'A'}}))
def test_24__process_elem_text__w_children(self): (elem, dic, subdic) = (TT.ET.XML("<a>A<b/></a>"), {}, {}) TT._process_elem_text(elem, dic, subdic, text="#text") self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, {"#text": 'A'}))
def assert_dicts_equal(self, dic, ref, ordered=False): self.assertTrue(dicts_equal(dic, ref, ordered=ordered), "%r%s vs.%s%r" % (dic, os.linesep, os.linesep, ref))
def test_40__process_children_elems__root(self): (elem, dic, subdic) = (TT.ET.XML("<list><i>A</i><i>B</i></list>"), {}, {}) TT._process_children_elems(elem, dic, subdic) self.assertTrue(dicts_equal(dic, {"list": [{"i": "A"}, {"i": "B"}]})) self.assertTrue(not subdic)
def test_44__process_children_elems__w_children_have_unique_keys(self): (elem, dic, subdic) = (TT.ET.XML("<a><x>X</x><y>Y</y></a>"), {}, {}) TT._process_children_elems(elem, dic, subdic) self.assertTrue(dicts_equal(dic, {"a": {"x": "X", "y": "Y"}})) self.assertTrue(not subdic)
def test_10__namespaces_from_file(self): ref = {"http://example.com/ns/config": '', "http://example.com/ns/config/val": "val"} xmlfile = anyconfig.compat.StringIO(TBC.read_from_res("20-00-cnf.xml")) self.assertTrue(dicts_equal(TT._namespaces_from_file(xmlfile), ref))
def test_34__process_elem_attrs__merge_attrs(self): (elem, dic, subdic) = (TT.ET.XML("<a id='A'/>"), {}, {}) TT._process_elem_attrs(elem, dic, subdic, merge_attrs=True) self.assertTrue(dicts_equal(dic, {"a": {"id": 'A'}})) self.assertTrue(not subdic)
def test_10_empty_path(self): dic = dict(a=1, b=[1, 2]) (dic2, err) = TT.get(dic, '') self.assertEqual(err, '') self.assertTrue(dicts_equal(dic2, dic))
def test_44_gen_schema__complex_types(self): scm = _gen_scm(self.obj2) self.assertTrue(dicts_equal(scm, self.ref_scm))
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_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_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_32__process_elem_attrs__w_text(self): (elem, dic, subdic) = (TT.ET.XML("<a id='A'>AAA</a>"), {}, {}) TT._process_elem_attrs(elem, dic, subdic) self.assertTrue(not dic) self.assertTrue(dicts_equal(subdic, {"@attrs": {"id": 'A'}}))
def _co_assert_equals(self, cnf): self.assertTrue(dicts_equal(cnf, self.conf_ref.dict()))