def test_basic(self): d1 = dict(a='foo') d2 = dict(a='foo') self.assertTrue(dict_compare(d1, d2)[0]) torf, txt = dict_compare(d1, dict(a='bar')) self.assertFalse(torf) self.assertEqual("""--- dict1 +++ dict2 < dict1.a: foo > dict2.a: bar """, txt)
def test_depth(self): d1 = dict(a=dict(b=dict(c="c1", d="d1"))) self.assertEqual("""--- deep1 +++ deep2 + deep1.a.b.d: d1 """, dict_compare(d1, dict(a=dict(b=dict(c="c1"))), "deep1", "deep2")[1])
def test_add(self): d1 = dict(a='foo', b=1, c=[1, 2, 3]) self.assertEqual("""--- a and b +++ empty + a and b.a: foo + a and b.b: 1 + a and b.c: [1, 2, 3] """, dict_compare(d1, dict(), "a and b", "empty")[1])
def compare_output(opts: argparse.Namespace, dict_schema: dict, outfile: str) -> bool: compare_file = os.path.join(opts.comparedir, os.path.split(outfile)[1]) try: json_dict = json.loads(open(compare_file, encoding="utf-8").read()) except FileNotFoundError: print("**** File not found: %s" % compare_file) return False dc_result, match_result = dict_compare( dict_schema, json_dict, d1name="Input", d2name="Expected", filtr=compare_filter ) if not dc_result: print("FILE: %s" % os.path.split(outfile)[1], file=sys.stderr) print(match_result, file=sys.stderr) return dc_result
def test_filtr(self): d1 = dict(a='foo', b=dict(c=1, d=2), f=dict(c=1, d=17), d=111) d2 = dict(a='bar', b=dict(c=0, d=3), f=dict(c=1, d=17), d=43) def filtr(kv1, kv2) -> bool: return (kv1 and kv1[0].split('.')[-1] == 'd') or (kv2 and kv2[0].split('.')[-1] == 'd') self.assertEqual("""--- dict1 +++ dict2 < dict1.a: foo > dict2.a: bar < dict1.b.c: 1 > dict2.b.c: 0 """, dict_compare(d1, d2, filtr=filtr)[1])
def compare_output(opts: argparse.Namespace, dict_schema: dict, outfile: str) -> bool: compare_file = os.path.join(opts.comparedir, os.path.split(outfile)[1]) try: json_dict = json.loads(open(compare_file, encoding='utf-8').read()) except FileNotFoundError: print("**** File not found: %s" % compare_file) return False dc_result, match_result = dict_compare(dict_schema, json_dict, d1name="Input", d2name="Expected", filtr=compare_filter) if not dc_result: print("FILE: %s" % os.path.split(outfile)[1], file=sys.stderr) print(match_result, file=sys.stderr) return dc_result