def test_diff_example_1(): data_dpath = Path(__file__).parent with (data_dpath / "data" / "1-left-response.json").open("rt") as fp: left_dpath = json.loads(fp.read()) with (data_dpath / "data" / "1-right-response.json").open("rt") as fp: right_dpath = json.loads(fp.read()) differ = Differ() # Select all fields that the last element name ends with '_date' date_selector = DateSelector() # The documents still have their dates as strings. Run the nomalizer that checks to see # if it can parse the string as a date and convert it to today() as a date string. date_normalizer = StrTodayDateNormalizer(selectors=date_selector) # Normalize floats to have only 3 significant digits. float_normalizer = FloatRoundNormalizer(3) result = differ.diff( left_dpath, right_dpath, normalizers=[float_normalizer, date_normalizer], max_col_width=50, ) assert result print(result.support)
def selector_multiple_match(): differ = Differ() a_selector = ListLastComponentSelector(component_names=["a"]) b_selector = ListLastComponentSelector(component_names=["b"]) float_round_normalizer = FloatRoundNormalizer( places=1, selectors=[a_selector, b_selector]) result = differ.diff(OBJ_1, OBJ_2, normalizers=[float_round_normalizer]) assert result print(result.support)
def float_two_precision_match(): differ = Differ() # Normalize the 'a' element to 1 decimal place. a_selector = ListLastComponentSelector(component_names=["a"]) one_float_round_normalizer = FloatRoundNormalizer(places=1, selectors=[a_selector]) # Normalize the 'b' element to 2 decimal places. b_selector = ListLastComponentSelector(component_names=["b"]) two_float_round_normalizer = FloatRoundNormalizer(places=2, selectors=[b_selector]) result = differ.diff( OBJ_1, OBJ_2, normalizers=[two_float_round_normalizer, one_float_round_normalizer], ) assert result print(result.support)
def test_exception(capsys): journal_init(JOURNAL_CFG_FPATH, context) with pytest.raises(ZeroDivisionError): div_zero() captured = capsys.readouterr() # Clean up the output as logging subsystem adds exception information to the output. msg = captured[0].split("\n")[0] expected = { "tag": "JOURNAL_MSG_JSON", "format": "0.2.0", "objective": "int_add_objective", "context": { "service_ctx": { "name": "Test Service", "version": "0.1.0" }, "implementation_ctx": { "name": "Simple Model", "version": "0.1.0" }, }, "arguments": {}, "exception": { "type": "ZeroDivisionError", "msg": "division by zero", "file": "/home/some_user/projects/callable-journal/test/journal_test.py", "line": "52", }, } msg = json.loads(msg) selector = EndsWithSelector("exception/file") normalizer = PathNormalizer(num_components=3, selectors=selector) result = Differ().diff(expected, msg, normalizers=normalizer, max_col_width=50) print(result.support) if not result: print(result.support) assert False
def test_default(capsys): journal_init(JOURNAL_CFG_FPATH, context) a, b = 10, 20 c = int_add(a, b) assert c == a + b captured = capsys.readouterr() msg = captured[0] expected = { "tag": "JOURNAL_MSG_JSON", "format": "0.2.0", "objective": "int_add", "context": { "service_ctx": { "name": "Test Service", "version": "0.1.0" }, "implementation_ctx": { "name": "Simple Model", "version": "0.1.0" }, }, "arguments": { "a": 10, "b": 20 }, "results": { "sum": 30 }, } msg = json.loads(msg) result = Differ().diff(expected, msg) if not result: print(result.support) assert False
def float_mismatch(): differ = Differ() result = differ.diff(OBJ_1, OBJ_2) assert not result print(result.support)
def test_html_diff_dict_fail(): td = copy.deepcopy(TEST_DICT) td["l"] = [] result = Differ.diff(td, SORTED_DICT) assert not result print(result.support)
def float_match(): differ = Differ() float_round_normalizer = FloatRoundNormalizer(places=1) result = differ.diff(OBJ_1, OBJ_2, normalizers=[float_round_normalizer]) assert result print(result.support)
def test_json_encoder(): d1 = {"date": datetime.date.today()} result = Differ.diff(d1, d1, cls=DateJSONEncoder) assert result
def test_diff_list_fail(): tl = copy.deepcopy(TEST_LIST) tl[0] = [] result = Differ.diff(tl, SORTED_LIST) assert not result
def test_diff_list(): result = Differ.diff(TEST_LIST, SORTED_LIST) assert result
def test_diff_dict_fail(): td = copy.deepcopy(TEST_DICT) td["l"] = [] match = Differ.diff(td, SORTED_DICT) assert not match
def test_diff_dict(): match = Differ.diff(TEST_DICT, SORTED_DICT) assert match