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 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 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_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_round_normalizer_no_match(): path = Path("a") selector = ListLastComponentSelector(["b"]) normalizer = FloatRoundNormalizer(2, selectors=selector) result = normalizer.normalize(1.0001, path, normalizers=[normalizer]) assert result == 1.0001
def test_round_normalizer_no_selector(): path = Path("a") normalizer = FloatRoundNormalizer(2) result = normalizer.normalize(1.0001, path, normalizers=[normalizer]) assert result == 1.00
def test_json_encoder_float(): result = Sorter.sorted(LEFT_FLOAT, normalizers=FloatRoundNormalizer(places=2)) assert result[0] == 1.12