def check_differences(dataset_before, dataset_after):

    # Must compare sorted versions of json struct
    if ordered_json(dataset_after) == ordered_json(dataset_before):
        compare_status = "Identical"
        udiff_output = ""
    else:
        compare_status = "Changed"

        # Analyze difference only if changed

        if debug:
            print(
                "\nStarting json_delta.udiff()"
                + " for "
                + dataset_before["identifier"]
                + " at "
                + str(datetime.datetime.utcnow())
            )

        udiff_list = json_delta.udiff(dataset_before, dataset_after)
        udiff_output = "\n".join(udiff_list)
        udiff_output = beautify_diff(udiff_output)

        if debug:
            print("Finished json_delta.udiff()" + " at " + str(datetime.datetime.utcnow()) + "\n")

    return (compare_status, udiff_output)
Beispiel #2
0
    def diff_config(self, app, verbose=False):
        """
        Print the diff between current and desired job config
        """
        print(">>>>>>>> Job config diff for %s <<<<<<<<" % app.name)
        cfg_dicts = []
        factory = TSimpleJSONProtocolFactory()
        for cfg in app.current_job_config, app.desired_job_config:
            if cfg:
                cfg_json = TSerialization.serialize(cfg,
                                                    protocol_factory=factory)
                cfg_dict = json.loads(cfg_json)

                # Unset task resources to avoid confusing the job config differ
                cfg_dict["taskConfig"]["resources"] = None
            else:
                cfg_dict = {}
            cfg_dicts.append(cfg_dict)

        if verbose:
            for cfg_dict in cfg_dicts:
                print(json.dumps(cfg_dict, indent=4, sort_keys=True))

        for line in json_delta.udiff(cfg_dicts[0], cfg_dicts[1]):
            print(line)
Beispiel #3
0
def check_differences(dataset_before, dataset_after):

    # Must compare sorted versions of json struct
    if ordered_json(dataset_after) == ordered_json(dataset_before):
        compare_status = "Identical"
        udiff_output = ''
    else:
        compare_status = "Changed"

        # Analyze difference only if changed

        if debug:
            print("\nStarting json_delta.udiff()" + " for " +
                  dataset_before['identifier'] + " at " +
                  str(datetime.datetime.utcnow()))

        udiff_list = json_delta.udiff(dataset_before, dataset_after)
        udiff_output = '\n'.join(udiff_list)
        udiff_output = beautify_diff(udiff_output)

        if debug:
            print("Finished json_delta.udiff()" + " at " +
                  str(datetime.datetime.utcnow()) + "\n")

    return (compare_status, udiff_output)
Beispiel #4
0
def assert_json_contains(expected, actual, msg="JSON mismatch"):
    """
    This method asserts that `expected` is a subset of `actual`
    parameters:
        @expected:
        @actual:
        @msg:
    """
    __tracebackhide__ = True

    subset = _extract_subset(actual, expected)
    if subset != expected:
        diff = '\n'.join(udiff(expected, subset, indent=2))
        raise AssertionError("{msg}:\n{diff}".format(msg=msg, diff=diff))
Beispiel #5
0
def test_request(service, path):
    ref = requests.get(REF[service] + path)
    actual = requests.get(TESTED[service] + path)
    assert actual.status_code == ref.status_code
    assert ref.status_code == 200
    json_diff = diff(actual.json(),
                     ref.json(),
                     array_align=False,
                     verbose=False)
    if (path == '/get_witnesses'):
        # Filter fields not stable between calls.
        json_diff = [
            d for d in json_diff if len(d[0]) != 3
            or d[0][2] not in ['last_confirmed_block_num', 'last_aslot']
        ]
    assert not json_diff, '\n'.join(udiff(actual.json(), ref.json(),
                                          json_diff))
def compare(local_path, remote_url):
    """Downloads and compares a local JSON file with a remote one. If there is
    a difference, notifies the user and prompts them if they want to see the
    diff"""
    remote_response = requests.get(remote_url)
    if remote_response.status_code == 404:
        click.echo("Nonexistent: " + remote_url)
    else:
        remote = remote_response.json()
        with open(local_path) as f:
            local = json.load(f)

        if remote != local:
            click.echo("Content differs: {} {}".format(local_path, remote_url))
            if click.confirm("Show diff?"):
                diffs_str = '\n'.join(udiff(remote, local))
                click.echo_via_pager(diffs_str)
def compare(local_path, remote_url, prompt=True):
    """Downloads and compares a local JSON file with a remote one. If there is
    a difference, notifies the user and prompts them if they want to see the
    diff"""
    remote = path_to_json(remote_url)
    if remote is None:
        logger.warn("Nonexistent: %s", remote_url)
        return None

    with open(local_path) as fp:
        local = json.load(fp)

    if remote != local:
        click.echo("Content differs: {} {}".format(local_path, remote_url))
        if not prompt or click.confirm("Show diff?"):
            diffs_str = '\n'.join(udiff(remote, local))
            echo = click.echo_via_pager if prompt else click.echo
            echo(diffs_str)
            return diffs_str
Beispiel #8
0
def compare(local_path, remote_url, prompt=True):
    """Downloads and compares a local JSON file with a remote one. If there is
    a difference, notifies the user and prompts them if they want to see the
    diff"""
    remote = path_to_json(remote_url)
    if remote is None:
        logger.warning("Nonexistent: %s", remote_url)
        return None

    with open(local_path) as fp:
        local = json.load(fp)

    if remote != local:
        click.echo("Content differs: {} {}".format(local_path, remote_url))
        if not prompt or click.confirm("Show diff?"):
            diffs_str = '\n'.join(udiff(remote, local))
            echo = click.echo_via_pager if prompt else click.echo
            echo(diffs_str)
            return diffs_str
Beispiel #9
0
def compare_json(j1, j2):
    diff = json_delta.diff(j1, j2, False, False)

    if not diff:
        return EQUAL, None
    else:
        try:
            return DIFFER, '\n'.join(json_delta.udiff(j1, j2, diff, 2))
        except:
            print("################ EXCEPTION ################")
            print("#                                         #")
            print("# json_delta raised an exception          #")
            print("# using fallback of difflib.unified()     #")
            print("#                                         #")
            print("###########################################")

            diff = difflib.unified_diff(
                json.dumps(j1, indent=2).split('\n'),
                json.dumps(j2, indent=2).split('\n'))
            return DIFFER, '\n'.join(diff)
def compare_json(j1, j2):

    if j1 == j2:
        return EQUAL, None

    # If the contents are not equal, use json_delta.diff to calculate the exact
    # differences.  Some of our yaml files include sets, which json_delta
    # cannot handle, so in those cases, fall back to doing a diff of the
    # formatted json.
    try:
        diff = json_delta.diff(j1, j2, False, False)
        return DIFFER, '\n'.join(json_delta.udiff(j1, j2, diff, 2))
    except Exception:
        print("################ EXCEPTION ################")
        print("#                                         #")
        print("# json_delta raised an exception          #")
        print("# using fallback of difflib.unified()     #")
        print("#                                         #")
        print("###########################################")

        diff = difflib.unified_diff(
            json.dumps(j1, indent=2, cls=SetEncoder).split('\n'),
            json.dumps(j2, indent=2, cls=SetEncoder).split('\n'))
        return DIFFER, '\n'.join(diff)
Beispiel #11
0
def print_json_diff(json_source, json_dest):
    diff_lines = json_delta.udiff(json_source, json_dest)
    for line in diff_lines:
        print line
    return diff_lines
def dict_eq_(test_case, dict1, dict2):
    diff = json_delta.udiff(dict1, dict2, entry=True)
    diff_lines = ''
    for line in diff:
        diff_lines = '{}{}\n'.format(diff_lines, line)
        test_case.assertEqual(dict2, dict1, diff_lines)
Beispiel #13
0
 def assert_json(self, expected, actual):
     diff = "\n".join(udiff(expected, actual))
     eq_(diff, ' {...}', diff)