def test_31_dump_and_load__to_from_stream(self): with TT.open(self.a_path, mode='w') as strm: TT.dump(self.dic, strm) self.assertTrue(self.a_path.exists()) with TT.open(self.a_path) as strm: res = TT.load(strm, ac_parser="json") self.assert_dicts_equal(res, self.dic)
def test_31_dump_and_load__to_from_stream(self): with TT.open(self.a_path, mode='w') as strm: TT.dump(self.dic, strm) self.assertTrue(os.path.exists(self.a_path)) with TT.open(self.a_path) as strm: res = TT.load(strm, ac_parser="json") self.assert_dicts_equal(res, self.dic)
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 _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_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_50_multi_load__templates(self): if not anyconfig.template.SUPPORTED: return ctx = self.dic.copy() TT.merge(ctx, self.upd, ac_merge=TT.MS_DICTS) a_path = self.a_path.replace(".json", ".yml") b_path = self.b_path.replace(".json", ".yml") g_path = self.g_path.replace(".json", ".yml") TT.open(a_path, mode='w').write(CNF_TMPL_1) TT.open(b_path, mode='w').write(CNF_TMPL_2) opts = dict(ac_merge=TT.MS_DICTS, ac_template=True, ac_context=ctx) try: res0 = TT.multi_load(g_path, **opts) res1 = TT.multi_load([g_path, b_path], **opts) except (TT.UnknownProcessorTypeError, TT.UnknownFileTypeError): return self.assert_dicts_equal(res0, self.exp) self.assert_dicts_equal(res1, self.exp)
def test_30_dump_and_multi_load__to_from_stream(self): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) res = TT.multi_load([TT.open(self.a_path), TT.open(self.b_path)]) self.assert_dicts_equal(res, self.exp)
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)