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_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_16_single_load__template(self): if not anyconfig.template.SUPPORTED: return cpath = os.path.join(resdir(), "30-00-template-cnf.json") cnf = TT.single_load(cpath, ac_template=True, ac_context=self.cnf) self.assert_dicts_equal(cnf, self.cnf) spath = os.path.join(resdir(), "30-00-template-cnf-ng-scm.json") # 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_22_dump_and_single_load__w_ac_dict_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(os.path.exists(self.a_path)) res = TT.single_load(self.a_path, ac_dict=MyODict) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, MyODict))
def test_14_single_load__ignore_missing(self): null_cntnr = TT.container() cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEquals(TT.single_load(cpath, "ini", ignore_missing=True), null_cntnr)
def test_14_single_load__ignore_missing(self): null_cntnr = TT.to_container() cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEqual(TT.single_load(cpath, "ini", ignore_missing=True), null_cntnr)
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 AT.SUPPORTED: return a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) a_path = os.path.join(self.workdir, "a.yml") b_path = os.path.join(self.workdir, "b.yml") open(a_path, 'w').write("{% include 'b.yml' %}") open(b_path, 'w').write("""name: {{ name|default('a') }} a: {{ a }} b: b: {% for x in b.b -%} - {{ x }} {% endfor %} c: {{ b.c }} """) a1 = TT.single_load(a_path, ac_template=True, ac_context=a) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], a["a"]) self.assertEquals(a1["b"]["b"], a["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"])
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_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_20_dump_and_single_load__w_ordered_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(os.path.exists(self.a_path)) # It works w/ JSON backend but some backend cannot keep the order of # items and the tests might fail. res = TT.single_load(self.a_path, ac_ordered=True) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, anyconfig.compat.OrderedDict))
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_16_single_load__template(self): if not anyconfig.template.SUPPORTED: return cpath = os.path.join(self.workdir, "a.yaml") TT.open(cpath, mode='w').write(CNF_TMPL_0) cnf = TT.single_load(cpath, ac_template=True, ac_context=self.cnf) self.assert_dicts_equal(cnf, self.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_20_dump_and_single_load__w_ordered_option(self): TT.dump(self.cnf, self.a_path) self.assertTrue(self.a_path.exists()) # It works w/ JSON backend but some backend cannot keep the order of # items and the tests might fail. res = TT.single_load(self.a_path, ac_ordered=True) self.assert_dicts_equal(res, self.cnf, ordered=True) self.assertTrue(isinstance(res, collections.OrderedDict))
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_15_single_load__fail_to_render_template(self): if not anyconfig.template.SUPPORTED: return cnf_s = "name: '{{ name'" # Should cause template renering error. cpath = os.path.join(self.workdir, "a.yaml") TT.open(cpath, mode='w').write(cnf_s) cnf = TT.single_load(cpath, ac_template=True, ac_context=dict(a=1)) self.assertEqual(cnf["name"], "{{ name")
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.assert_dicts_equal(cnf, cnf1)
def test_39_single_load__w_validation(self): (cnf, scm) = (CNF_0, SCM_0) cpath = self.workdir / "cnf.json" spath = self.workdir / "scm.json" TT.dump(cnf, cpath) TT.dump(scm, spath) cnf1 = TT.single_load(cpath, ac_schema=spath) self.assert_dicts_equal(cnf, cnf1)
def test_15_single_load__fail_to_render_template(self): if not anyconfig.template.SUPPORTED: return cnf_s = "name: '{{ name'" # Should cause template renering error. cpath = self.workdir / "a.yaml" cpath.write_text(cnf_s) cnf = TT.single_load(cpath, ac_template=True, ac_context=dict(a=1)) self.assertEqual(cnf["name"], "{{ name")
def test_13_dump_and_single_load__namedtuple(self): if not IS_PYTHON_3: # TODO: it does not work with python3. cpath = os.path.join(self.workdir, "a.json") cnf = OrderedDict(sorted(self.cnf.items())) cnf0 = convert_to(cnf, ac_namedtuple=True) TT.dump(cnf0, cpath) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(cpath, ac_namedtuple=True) self.assertTrue(cnf0 == cnf1, "\n%r ->\n%r" % (cnf0, cnf1))
def test_10_single_load_w_validation(self): cnf_path = self.workdir / "cnf.json" scm_path = self.workdir / "scm.json" TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.single_load(cnf_path, ac_context={}, ac_schema=scm_path) self.assertEqual(cnf_2["name"], CNF_0["name"]) self.assertEqual(cnf_2["a"], CNF_0["a"]) self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"]) self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
def test_10_single_load_w_validation(self): cnf_path = os.path.join(self.workdir, "cnf.json") scm_path = os.path.join(self.workdir, "scm.json") TT.dump(CNF_0, cnf_path) TT.dump(SCM_0, scm_path) cnf_2 = TT.single_load(cnf_path, ac_context={}, ac_schema=scm_path) self.assertEqual(cnf_2["name"], CNF_0["name"]) self.assertEqual(cnf_2["a"], CNF_0["a"]) self.assertEqual(cnf_2["b"]["b"], CNF_0["b"]["b"]) self.assertEqual(cnf_2["b"]["c"], CNF_0["b"]["c"])
def test_10_dump_and_single_load(self): a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) a_path = os.path.join(self.workdir, "a.json") A.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) a1 = A.single_load(a_path) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], a["a"]) self.assertEquals(a1["b"]["b"], a["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"])
def _load_and_dump_with_opened_files(self, filename, rmode='r', wmode='w', **oopts): cpath = 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.assertEqual(*cpair)
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_single_load_w_query(self): cpath = os.path.join(self.workdir, "cnf.json") TT.dump(CNF_0, cpath) try: if TT.query.jmespath: self.assertEqual(TT.single_load(cpath, ac_query="a"), 1) self.assertEqual(TT.single_load(cpath, ac_query="b.b"), [1, 2]) self.assertEqual(TT.single_load(cpath, ac_query="b.b[1]"), 2) self.assertEqual(TT.single_load(cpath, ac_query="b.b[1:]"), [2]) self.assertEqual(TT.single_load(cpath, ac_query="b.b[::-1]"), [2, 1]) self.assertEqual(TT.single_load(cpath, ac_query="length(b.b)"), 2) except (NameError, AttributeError): pass # jmespath is not available.
def test_20_single_load_w_query(self): cpath = self.workdir / "cnf.json" TT.dump(CNF_0, cpath) try: if TT.query.jmespath: self.assertEqual(TT.single_load(cpath, ac_query="a"), 1) self.assertEqual(TT.single_load(cpath, ac_query="b.b"), [1, 2]) self.assertEqual(TT.single_load(cpath, ac_query="b.b[1]"), 2) self.assertEqual(TT.single_load(cpath, ac_query="b.b[1:]"), [2]) self.assertEqual(TT.single_load(cpath, ac_query="b.b[::-1]"), [2, 1]) self.assertEqual(TT.single_load(cpath, ac_query="length(b.b)"), 2) except (NameError, AttributeError): pass # jmespath is not available.
def test_14_single_load__ignore_missing(self): cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEqual(TT.single_load(cpath, "ini", ac_ignore_missing=True), NULL_CNTNR)
def test_14__single_load__ac_parser_by_id(self): cid = anyconfig.backend.json.Parser.cid() res = TT.single_load(self.a_path, ac_parser=cid) self.assert_dicts_equal(res, self.cnf)
def test_20__single_load__stream(self): res = TT.single_load(open(self.a_path), ac_parser="json") self.assert_dicts_equal(res, self.cnf)
def test_14_single_load__ignore_missing(self): cpath = pathlib.Path(os.curdir) / "conf_file_should_not_exist" assert not cpath.exists() self.assertEqual(TT.single_load(cpath, "ini", ac_ignore_missing=True), NULL_CNTNR)
def test_11_dump_and_single_load__to_from_stream(self): TT.dump(self.cnf, self.a_path.open('w')) self.assertTrue(self.a_path.exists()) res = TT.single_load(self.a_path.open()) self.assert_dicts_equal(res, self.cnf)
def test_12_dump_and_single_load__no_parser(self): self.assertEquals(A.single_load("dummy.ext_not_exist"), None)
def test_10__single_load(self): res = TT.single_load(self.a_path) self.assert_dicts_equal(res, self.cnf)
def test_12__single_load__ac_parser(self): res = TT.single_load(self.a_path, ac_parser="json") self.assert_dicts_equal(res, self.cnf)
def test_30__single_load__pathlib(self): if self.pathlib: pobj = self.pathlib.Path(self.a_path) res = TT.single_load(pobj) self.assert_dicts_equal(res, self.cnf)
def test_10_dump_and_single_load(self): TT.dump(self.cnf, self.a_path) self.assertTrue(os.path.exists(self.a_path)) res = TT.single_load(self.a_path) self.assert_dicts_equal(res, self.cnf)
def test_30__single_load__pathlib(self): pobj = pathlib.Path(self.a_path) res = TT.single_load(pobj) self.assert_dicts_equal(res, self.cnf)
def test_10_dump_and_single_load(self): TT.dump(self.cnf, self.a_path) self.assertTrue(self.a_path.exists()) res = TT.single_load(self.a_path) self.assert_dicts_equal(res, self.cnf)
def test_11_dump_and_single_load__to_from_stream(self): TT.dump(self.cnf, TT.open(self.a_path, mode='w')) self.assertTrue(os.path.exists(self.a_path)) res = TT.single_load(TT.open(self.a_path)) self.assert_dicts_equal(res, self.cnf)