def test_10_dump_and_multi_load__default_merge_strategy(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") g_path = os.path.join(self.workdir, "*.json") TT.dump(a, a_path) TT.dump(b, b_path) a0 = TT.multi_load(g_path) a02 = TT.multi_load([g_path, b_path]) self.assertEqual(a0["name"], a["name"]) self.assertEqual(a0["a"], b["a"]) self.assertEqual(a0["b"]["b"], b["b"]["b"]) self.assertEqual(a0["b"]["c"], a["b"]["c"]) self.assertEqual(a0["b"]["d"], b["b"]["d"]) self.assertEqual(a02["name"], a["name"]) self.assertEqual(a02["a"], b["a"]) self.assertEqual(a02["b"]["b"], b["b"]["b"]) self.assertEqual(a02["b"]["c"], a["b"]["c"]) self.assertEqual(a02["b"]["d"], b["b"]["d"]) a1 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS) self.assertEqual(a1["name"], a["name"]) self.assertEqual(a1["a"], b["a"]) self.assertEqual(a1["b"]["b"], b["b"]["b"]) self.assertEqual(a1["b"]["c"], a["b"]["c"]) self.assertEqual(a1["b"]["d"], b["b"]["d"])
def test_40_multi_load__templates(self): if not anyconfig.template.SUPPORTED: return a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) ma = TT.to_container(a, ac_merge=TT.MS_DICTS) ma.update(b) a_path = os.path.join(self.workdir, "a.yml") b_path = os.path.join(self.workdir, "b.yml") g_path = os.path.join(self.workdir, "*.yml") open(a_path, 'w').write(CNF_TMPL_1) open(b_path, 'w').write(CNF_TMPL_2) a0 = TT.multi_load(g_path, ac_merge=TT.MS_DICTS, ac_template=True, ac_context=ma) a02 = TT.multi_load([g_path, b_path], ac_merge=TT.MS_DICTS, ac_template=True, ac_context=ma) self.assertEqual(a0["name"], a["name"]) self.assertEqual(a0["a"], b["a"]) self.assertEqual(a0["b"]["b"], b["b"]["b"]) self.assertEqual(a0["b"]["c"], a["b"]["c"]) self.assertEqual(a0["b"]["d"], b["b"]["d"]) self.assertEqual(a02["name"], a["name"]) self.assertEqual(a02["a"], b["a"]) self.assertEqual(a02["b"]["b"], b["b"]["b"]) self.assertEqual(a02["b"]["c"], a["b"]["c"]) self.assertEqual(a02["b"]["d"], b["b"]["d"])
def test_60_wrong_merge_strategy(self): cpath = self.workdir / "a.json" TT.dump(dict(a=1, b=2), cpath) try: TT.multi_load([cpath, cpath], ac_merge="merge_st_not_exist") raise RuntimeError("Wrong merge strategy was not handled!") except ValueError: self.assertTrue(bool(1)) # To suppress warn of pylint.
def test_60_wrong_merge_strategy(self): cpath = os.path.join(self.workdir, "a.json") TT.dump(dict(a=1, b=2), cpath) try: TT.multi_load([cpath, cpath], ac_merge="merge_st_not_exist") raise RuntimeError("Wrong merge strategy was not handled!") except ValueError: self.assertTrue(bool(1)) # To suppress warn of pylint.
def test_10_multi_load_w_validation_for_partial_single_config_files(self): cpaths = [os.path.join(resdir(), "00-00-cnf.json"), os.path.join(resdir(), "00-01-cnf.json"), os.path.join(resdir(), "00-02-cnf.json")] spath = os.path.join(resdir(), "00-scm.json") cnf = TT.multi_load(cpaths, ac_schema=spath) ref = TT.multi_load(cpaths) self.assert_dicts_equal(cnf, ref, ordered=False)
def test_10_multi_load_w_validation_for_partial_single_config_files(self): cpaths = [respath('00-00-cnf.json'), respath('00-01-cnf.json'), respath('00-02-cnf.json')] spath = respath('00-scm.json') cnf = TT.multi_load(cpaths, ac_schema=spath) ref = TT.multi_load(cpaths) self.assert_dicts_equal(cnf, ref, ordered=False)
def test_40_multi_load__ignore_missing(self): cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEqual(TT.multi_load([cpath], ac_parser="ini", ac_ignore_missing=True), NULL_CNTNR) # It will be remove after 'ignore_missing' was deprecated and removed. self.assertEqual(TT.multi_load([cpath], ac_parser="ini", ignore_missing=True), NULL_CNTNR)
def test_40_multi_load__ignore_missing(self): cpath = pathlib.Path(os.curdir) / "conf_file_should_not_exist" assert not cpath.exists() self.assertEqual(TT.multi_load([cpath], ac_parser="ini", ac_ignore_missing=True), NULL_CNTNR) # It will be remove after 'ignore_missing' was deprecated and removed. self.assertEqual(TT.multi_load([cpath], ac_parser="ini", ignore_missing=True), NULL_CNTNR)
def test_20_dump_and_multi_load(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") A.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) A.dump(b, b_path) self.assertTrue(os.path.exists(b_path)) a1 = A.multi_load([a_path, b_path], merge=A.MS_DICTS) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], b["a"]) self.assertEquals(a1["b"]["b"], b["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"]) self.assertEquals(a1["b"]["d"], b["b"]["d"]) a2 = A.multi_load([a_path, b_path], merge=A.MS_DICTS_AND_LISTS) self.assertEquals(a2["name"], a["name"]) self.assertEquals(a2["a"], b["a"]) self.assertEquals(a2["b"]["b"], [0, 1, 2, 3, 4, 5]) self.assertEquals(a2["b"]["c"], a["b"]["c"]) self.assertEquals(a2["b"]["d"], b["b"]["d"]) a3 = A.multi_load(os.path.join(self.workdir, "*.json")) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], b["a"]) self.assertEquals(a1["b"]["b"], b["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"]) self.assertEquals(a1["b"]["d"], b["b"]["d"]) a4 = A.multi_load([a_path, b_path], merge=A.MS_REPLACE) self.assertEquals(a4["name"], a["name"]) self.assertEquals(a4["a"], b["a"]) self.assertEquals(a4["b"]["b"], b["b"]["b"]) self.assertFalse("c" in a4["b"]) self.assertEquals(a4["b"]["d"], b["b"]["d"]) a5 = A.multi_load([a_path, b_path], merge=A.MS_NO_REPLACE) self.assertEquals(a5["name"], a["name"]) self.assertEquals(a5["a"], a["a"]) self.assertEquals(a5["b"]["b"], a["b"]["b"]) self.assertEquals(a5["b"]["c"], a["b"]["c"]) self.assertFalse("d" in a5["b"])
def test_24_multi_load__templates(self): if not AT.SUPPORTED: return a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) ma = TT.container.create(a) ma.update(b, TT.MS_DICTS) a_path = os.path.join(self.workdir, "a.yml") b_path = os.path.join(self.workdir, "b.yml") g_path = os.path.join(self.workdir, "*.yml") open(a_path, 'w').write("""\ a: {{ a }} b: b: {% for x in b.b -%} - {{ x }} {% endfor %} c: {{ b.c }} name: {{ name }} """) open(b_path, 'w').write("""\ a: {{ a }} b: b: {% for x in b.b -%} - {{ x }} {% endfor %} d: {{ b.d }} """) a0 = TT.multi_load(g_path, merge=TT.MS_DICTS, ac_template=True, ac_context=ma) a02 = TT.multi_load([g_path, b_path], merge=TT.MS_DICTS, ac_template=True, ac_context=ma) self.assertEquals(a0["name"], a["name"]) self.assertEquals(a0["a"], b["a"]) self.assertEquals(a0["b"]["b"], b["b"]["b"]) self.assertEquals(a0["b"]["c"], a["b"]["c"]) self.assertEquals(a0["b"]["d"], b["b"]["d"]) self.assertEquals(a02["name"], a["name"]) self.assertEquals(a02["a"], b["a"]) self.assertEquals(a02["b"]["b"], b["b"]["b"]) self.assertEquals(a02["b"]["c"], a["b"]["c"]) self.assertEquals(a02["b"]["d"], b["b"]["d"])
def _check_multi_load_with_strategy(self, exp, merge=TT.MS_DICTS): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) self.assertTrue(os.path.exists(self.a_path)) self.assertTrue(os.path.exists(self.b_path)) res0 = TT.multi_load(self.g_path, ac_merge=merge) res1 = TT.multi_load([self.g_path, self.b_path], ac_merge=merge) self.assertTrue(res0) self.assertTrue(res1) self.assert_dicts_equal(res0, exp) self.assert_dicts_equal(res1, exp)
def _check_multi_load_with_strategy(self, exp, merge=TT.MS_DICTS): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) self.assertTrue(self.a_path.exists()) self.assertTrue(self.b_path.exists()) res0 = TT.multi_load(self.g_path, ac_merge=merge) res1 = TT.multi_load([self.g_path, self.b_path], ac_merge=merge) self.assertTrue(res0) self.assertTrue(res1) self.assert_dicts_equal(res0, exp) self.assert_dicts_equal(res1, exp)
def test_60_multi_load__w_ac_dict_option(self): TT.dump(self.dic, self.a_path) TT.dump(self.upd, self.b_path) res = TT.multi_load(self.g_path, ac_dict=MyODict) self.assert_dicts_equal(res, self.exp) self.assertTrue(isinstance(res, MyODict))
def test_40_multi_load__ignore_missing(self): cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEqual(TT.multi_load([cpath], ac_parser="ini", ignore_missing=True), NULL_CNTNR)
def test_30_multi_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.multi_load([cpath], ac_parser="ini", ignore_missing=True), null_cntnr)
def test_30_multi_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.multi_load([cpath], ac_parser="ini", ignore_missing=True), null_cntnr)
def test_22_multi_load__ignore_missing(self): null_cntnr = A.container() cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEquals( A.multi_load([cpath], forced_type="ini", ignore_missing=True), null_cntnr)
def test_22_multi_load__ignore_missing(self): null_cntnr = A.container() cpath = os.path.join(os.curdir, "conf_file_should_not_exist") assert not os.path.exists(cpath) self.assertEquals(A.multi_load([cpath], forced_type="ini", ignore_missing=True), null_cntnr)
def test_12_dump_and_multi_load__default_merge_strategy(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") g_path = os.path.join(self.workdir, "*.json") TT.dump(a, a_path) TT.dump(b, b_path) a2 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS_AND_LISTS) self.assertEqual(a2["name"], a["name"]) self.assertEqual(a2["a"], b["a"]) self.assertEqual(a2["b"]["b"], [0, 1, 2, 3, 4, 5]) self.assertEqual(a2["b"]["c"], a["b"]["c"]) self.assertEqual(a2["b"]["d"], b["b"]["d"]) a3 = TT.multi_load(g_path) self.assertEqual(a3["name"], a["name"]) self.assertEqual(a3["a"], b["a"]) self.assertEqual(a3["b"]["b"], b["b"]["b"]) self.assertEqual(a3["b"]["c"], a["b"]["c"]) self.assertEqual(a3["b"]["d"], b["b"]["d"]) a4 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_REPLACE) self.assertEqual(a4["name"], a["name"]) self.assertEqual(a4["a"], b["a"]) self.assertEqual(a4["b"]["b"], b["b"]["b"]) self.assertFalse("c" in a4["b"]) self.assertEqual(a4["b"]["d"], b["b"]["d"]) a5 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_NO_REPLACE) self.assertEqual(a5["name"], a["name"]) self.assertEqual(a5["a"], a["a"]) self.assertEqual(a5["b"]["b"], a["b"]["b"]) self.assertEqual(a5["b"]["c"], a["b"]["c"]) self.assertFalse("d" in a5["b"])
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 = os.path.join(resdir(), "30-00-template-cnf.json") b_path = os.path.join(resdir(), "30-10-template-cnf.json") g_path = os.path.join(resdir(), "30-*-template-cnf.json") 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_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 = respath('30-00-template-cnf.json') b_path = respath('30-10-template-cnf.json') g_path = respath('30-*-template-cnf.json') 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_20_dump_and_multi_load__mixed_file_types(self): c_path = os.path.join(self.workdir, "c.yml") TT.dump(self.dic, self.a_path) # JSON try: TT.dump(self.upd, c_path) # YAML except (TT.UnknownProcessorTypeError, TT.UnknownFileTypeError): return # YAML backend is not available in this env. self.assertTrue(os.path.exists(self.a_path)) self.assertTrue(os.path.exists(c_path)) res = TT.multi_load([self.a_path, c_path]) self.assert_dicts_equal(res, self.exp)
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_20_dump_and_multi_load__mixed_file_types(self): c_path = self.workdir / "c.yml" TT.dump(self.dic, self.a_path) # JSON try: TT.dump(self.upd, c_path) # YAML except (TT.UnknownProcessorTypeError, TT.UnknownFileTypeError): return # YAML backend is not available in this env. self.assertTrue(self.a_path.exists()) self.assertTrue(c_path.exists()) res = TT.multi_load([self.a_path, c_path]) self.assert_dicts_equal(res, self.exp)
def test_16_dump_and_multi_load__mixed_file_types(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.yml") TT.dump(a, a_path) TT.dump(b, b_path) cnf = TT.multi_load([a_path, b_path]) self.assertEqual(cnf["name"], a["name"]) self.assertEqual(cnf["a"], b["a"]) self.assertEqual(cnf["b"]["b"], b["b"]["b"]) self.assertEqual(cnf["b"]["c"], a["b"]["c"]) self.assertEqual(cnf["b"]["d"], b["b"]["d"])
def test_18_dump_and_multi_load__namedtuple(self): a = convert_to(dict(a=1, b=dict(b=[0, 1], c="C"), name="a"), ac_namedtuple=True) b = convert_to(dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")), ac_namedtuple=True) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.yml") TT.dump(a, a_path) TT.dump(b, b_path) cnf = TT.multi_load([a_path, b_path], ac_namedtuple=True) self.assertEqual(cnf.name, a.name) self.assertEqual(cnf.a, b.a) self.assertEqual(cnf.b.b, b.b.b) self.assertEqual(cnf.b.c, a.b.c) self.assertEqual(cnf.b.d, b.b.d)
def test_20_dump_and_multi_load__to_from_stream(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") TT.dump(a, open(a_path, 'w')) TT.dump(b, open(b_path, 'w')) self.assertTrue(os.path.exists(a_path)) self.assertTrue(os.path.exists(b_path)) cnf = TT.multi_load([open(a_path), open(b_path)]) self.assertEqual(cnf["name"], a["name"]) self.assertEqual(cnf["a"], b["a"]) self.assertEqual(cnf["b"]["b"], b["b"]["b"]) self.assertEqual(cnf["b"]["c"], a["b"]["c"]) self.assertEqual(cnf["b"]["d"], b["b"]["d"])
def test_14_multi_load__wrong_merge_strategy(self): try: TT.multi_load("/dummy/*.json", ac_merge="merge_st_not_exist") raise RuntimeError("Wrong merge strategy was not handled!") except ValueError: self.assertTrue(1 == 1) # To suppress warn of pylint.
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_10_multi_load__empty_path_list(self): self.assertEqual(TT.multi_load([]), NULL_CNTNR)
def test_15_multi_load__empty_path_list(self): self.assertEqual(TT.multi_load([]), TT.to_container())
def test_20_dump_and_multi_load(self): a = dict(a=1, b=dict(b=[0, 1], c="C"), name="a") b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D")) a_path = os.path.join(self.workdir, "a.json") b_path = os.path.join(self.workdir, "b.json") g_path = os.path.join(self.workdir, "*.json") A.dump(a, a_path) self.assertTrue(os.path.exists(a_path)) A.dump(b, b_path) self.assertTrue(os.path.exists(b_path)) a0 = A.multi_load(g_path, merge=A.MS_DICTS) a02 = A.multi_load([g_path, b_path], merge=A.MS_DICTS) self.assertEquals(a0["name"], a["name"]) self.assertEquals(a0["a"], b["a"]) self.assertEquals(a0["b"]["b"], b["b"]["b"]) self.assertEquals(a0["b"]["c"], a["b"]["c"]) self.assertEquals(a0["b"]["d"], b["b"]["d"]) self.assertEquals(a02["name"], a["name"]) self.assertEquals(a02["a"], b["a"]) self.assertEquals(a02["b"]["b"], b["b"]["b"]) self.assertEquals(a02["b"]["c"], a["b"]["c"]) self.assertEquals(a02["b"]["d"], b["b"]["d"]) a1 = A.multi_load([a_path, b_path], merge=A.MS_DICTS) self.assertEquals(a1["name"], a["name"]) self.assertEquals(a1["a"], b["a"]) self.assertEquals(a1["b"]["b"], b["b"]["b"]) self.assertEquals(a1["b"]["c"], a["b"]["c"]) self.assertEquals(a1["b"]["d"], b["b"]["d"]) a2 = A.multi_load([a_path, b_path], merge=A.MS_DICTS_AND_LISTS) self.assertEquals(a2["name"], a["name"]) self.assertEquals(a2["a"], b["a"]) self.assertEquals(a2["b"]["b"], [0, 1, 2, 3, 4, 5]) self.assertEquals(a2["b"]["c"], a["b"]["c"]) self.assertEquals(a2["b"]["d"], b["b"]["d"]) a3 = A.multi_load(os.path.join(self.workdir, "*.json")) self.assertEquals(a3["name"], a["name"]) self.assertEquals(a3["a"], b["a"]) self.assertEquals(a3["b"]["b"], b["b"]["b"]) self.assertEquals(a3["b"]["c"], a["b"]["c"]) self.assertEquals(a3["b"]["d"], b["b"]["d"]) a4 = A.multi_load([a_path, b_path], merge=A.MS_REPLACE) self.assertEquals(a4["name"], a["name"]) self.assertEquals(a4["a"], b["a"]) self.assertEquals(a4["b"]["b"], b["b"]["b"]) self.assertFalse("c" in a4["b"]) self.assertEquals(a4["b"]["d"], b["b"]["d"]) a5 = A.multi_load([a_path, b_path], merge=A.MS_NO_REPLACE) self.assertEquals(a5["name"], a["name"]) self.assertEquals(a5["a"], a["a"]) self.assertEquals(a5["b"]["b"], a["b"]["b"]) self.assertEquals(a5["b"]["c"], a["b"]["c"]) self.assertFalse("d" in a5["b"])