Python deepdiff.DeepDiff() Examples

The following are 30 code examples of deepdiff.DeepDiff(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module deepdiff , or try the search function .
Example #1
Source File: helpers.py    From moler with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def compare_objects(first_object, second_object, ignore_order=False, report_repetition=False, significant_digits=None,
                    exclude_paths=None, exclude_types=None, verbose_level=2):
    """
    Return difference between two objects.
    :param first_object: first object to compare
    :param second_object: second object to compare
    :param ignore_order: ignore difference in order
    :param report_repetition: report when is repetition
    :param significant_digits: use to properly compare numbers(float arithmetic error)
    :param exclude_paths: path which be excluded from comparison
    :param exclude_types: types which be excluded from comparison
    :param verbose_level: higher verbose level shows you more details - default 0.
    :return: difference between two objects
    """
    if exclude_paths is None:
        exclude_paths = set()
    if exclude_types is None:
        exclude_types = set()

    diff = deepdiff.DeepDiff(first_object, second_object, ignore_order=ignore_order,
                             report_repetition=report_repetition, significant_digits=significant_digits,
                             exclude_paths=exclude_paths, exclude_types=exclude_types, verbose_level=verbose_level)
    return diff 
Example #2
Source File: test_delta.py    From deepdiff with MIT License 6 votes vote down vote up
def test_apply_delta_to_incompatible_object10_ignore_order(self, mock_logger):
        t1 = [1, 2, 'B']
        t2 = [1, 2]
        t3 = [1, 2, 'C']

        diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True)

        # when verify_symmetry=False, we still won't remove the item that is different
        # than what we expect specifically when ignore_order=True when generating the diff.
        # The reason is that when ignore_order=True, we can' rely too much on the index
        # of the item alone to delete it. We need to make sure we are deleting the correct value.
        # The expected behavior is exactly the same as when verify_symmetry=True
        # specifically for when ignore_order=True AND an item is removed.
        delta = Delta(diff, raise_errors=False, verify_symmetry=False)
        t4 = delta + t3

        assert [1, 2, 'C'] == t4
        expected_msg = FAIL_TO_REMOVE_ITEM_IGNORE_ORDER_MSG.format(2, 'root', 'B', 'C')
        mock_logger.assert_called_once_with(expected_msg) 
Example #3
Source File: test_delta.py    From deepdiff with MIT License 6 votes vote down vote up
def test_delta_dump_and_read3(self):
        t1 = [1, 2]
        t2 = [1, 2, 3, 5]
        diff = DeepDiff(t1, t2)
        delta_content = Delta(diff).dumps()
        path = '/tmp/delta_test2.delta'
        with open(path, 'wb') as the_file:
            the_file.write(delta_content)
        with pytest.raises(ValueError) as excinfo:
            with open(path, 'r') as the_file:
                delta = Delta(delta_file=the_file)
        assert BINIARY_MODE_NEEDED_MSG[:20] == str(excinfo.value)[:20]
        with open(path, 'rb') as the_file:
            delta = Delta(delta_file=the_file)
        os.remove(path)
        assert delta + t1 == t2 
Example #4
Source File: test_cache.py    From deepdiff with MIT License 6 votes vote down vote up
def test_cache_deeply_nested_a1(self, nested_a_t1, nested_a_t2, nested_a_result):

        diff = DeepDiff(nested_a_t1, nested_a_t2, ignore_order=True,
                        cache_size=5000, cache_tuning_sample_size=280,
                        cutoff_intersection_for_pairs=1)

        stats = diff.get_stats()
        expected_stats = {
            'PASSES COUNT': 1772,
            'DIFF COUNT': 9206,
            'DISTANCE CACHE HIT COUNT': 3442,
            'MAX PASS LIMIT REACHED': False,
            'MAX DIFF LIMIT REACHED': False
        }
        assert expected_stats == stats
        assert nested_a_result == diff
        diff_of_diff = DeepDiff(nested_a_result, diff.to_dict(), ignore_order=False, ignore_type_in_groups=[(dict, OrderedDictPlus)])
        assert not diff_of_diff 
Example #5
Source File: test_ignore_order.py    From deepdiff with MIT License 6 votes vote down vote up
def test_nested_list_ignore_order_report_repetition_wrong_currently(self):
        t1 = [1, 2, [3, 4]]
        t2 = [[4, 3, 3], 2, 1]
        ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True)
        result = {
            'repetition_change': {
                'root[2][0]': {
                    'old_repeat': 1,
                    'new_indexes': [1, 2],
                    'old_indexes': [1],
                    'value': 3,
                    'new_repeat': 2
                }
            }
        }
        assert result != ddiff 
Example #6
Source File: security_group.py    From Particle-Cloud-Framework with Apache License 2.0 6 votes vote down vote up
def is_state_definition_equivalent(self):
        """
        Compares the desired state and current state definition and returns whether they are equivalent
        Only considers fields defined in the desired definition
        All fields not specified are left alone in the current state (excluding rules)
        Both rules lists must be defined even when empty

        Returns:
            bool
        """
        self.sync_state()
        # use filters to remove any extra information
        desired_config = pcf_util.param_filter(self.desired_state_definition.get("custom_config", {}),
                                               SecurityGroup.DEFINITION_FILTER)
        current_config = pcf_util.param_filter(self.get_current_state_definition(), desired_config.keys())
        diff = DeepDiff(current_config, desired_config, ignore_order=True)
        return diff == {} 
Example #7
Source File: test_ignore_order.py    From deepdiff with MIT License 6 votes vote down vote up
def test_list_of_unhashable_difference_ignore_order_report_repetition2(
            self):
        t1 = [{"a": 2}, {"a": 2}]
        t2 = [{"a": 2}]
        ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True)
        result = {
            'repetition_change': {
                'root[0]': {
                    'old_repeat': 2,
                    'new_indexes': [0],
                    'old_indexes': [0, 1],
                    'value': {
                        'a': 2
                    },
                    'new_repeat': 1
                }
            }
        }
        assert result == ddiff 
Example #8
Source File: test_ignore_order.py    From deepdiff with MIT License 6 votes vote down vote up
def test_list_of_unhashable_difference_ignore_order3(self):
        t1 = [1, {"a": 2}, {"a": 2}, {"b": [3, 4, {1: 1}]}, "B"]
        t2 = [{"b": [3, 4, {1: 1}]}, {1: 1}]
        ddiff = DeepDiff(t1, t2, ignore_order=True)
        result = {
            'values_changed': {
                'root[1]': {
                    'new_value': {
                        1: 1
                    },
                    'old_value': {
                        'a': 2
                    }
                }
            },
            'iterable_item_removed': {
                'root[0]': 1,
                'root[4]': 'B'
            }
        }
        assert result == ddiff 
Example #9
Source File: test_ignore_order.py    From deepdiff with MIT License 6 votes vote down vote up
def test_list_of_unhashable_difference_ignore_order_report_repetition(
            self):
        t1 = [1, {"a": 2}, {"a": 2}, {"b": [3, 4, {1: 1}]}, "B"]
        t2 = [{"b": [3, 4, {1: 1}]}, {1: 1}]
        ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True)
        result = {
            'iterable_item_added': {
                'root[1]': {
                    1: 1
                }
            },
            'iterable_item_removed': {
                'root[4]': 'B',
                'root[0]': 1,
                'root[1]': {
                    'a': 2
                },
                'root[2]': {
                    'a': 2
                }
            }
        }
        assert result == ddiff 
Example #10
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_item_added_and_removed(self):
        t1 = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
        t2 = {'one': 1, 'two': 4, 'three': 3, 'five': 5, 'six': 6}
        ddiff = DeepDiff(t1, t2, view='tree')
        assert set(ddiff.keys()) == {
            'dictionary_item_added', 'dictionary_item_removed',
            'values_changed'
        }
        assert len(ddiff['dictionary_item_added']) == 2
        assert len(ddiff['dictionary_item_removed']) == 1 
Example #11
Source File: test_ignore_order.py    From deepdiff with MIT License 5 votes vote down vote up
def test_nested_list_ignore_order(self):
        t1 = [1, 2, [3, 4]]
        t2 = [[4, 3, 3], 2, 1]
        ddiff = DeepDiff(t1, t2, ignore_order=True)
        assert {} == ddiff 
Example #12
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_item_added_and_removed2(self):
        t1 = {2: 2, 4: 4}
        t2 = {2: "b", 5: 5}
        ddiff = DeepDiff(t1, t2, view='tree')
        assert set(ddiff.keys()), {
            'dictionary_item_added', 'dictionary_item_removed',
            'type_changes'
        }
        assert len(ddiff['dictionary_item_added']) == 1
        assert len(ddiff['dictionary_item_removed']) == 1 
Example #13
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_non_subscriptable_iterable(self):
        t1 = (i for i in [42, 1337, 31337])
        t2 = (i for i in [
            42,
            1337,
        ])
        ddiff = DeepDiff(t1, t2, view='tree')
        (change, ) = ddiff['iterable_item_removed']

        assert set(ddiff.keys()) == {'iterable_item_removed'}
        assert len(ddiff['iterable_item_removed']) == 1

        assert change.up.t1 == t1
        assert change.up.t2 == t2
        assert change.report_type == 'iterable_item_removed'
        assert change.t1 == 31337
        assert change.t2 is notpresent

        assert isinstance(change.up.t1_child_rel,
                          NonSubscriptableIterableRelationship)
        assert change.up.t2_child_rel is None 
Example #14
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_significant_digits(self):
        ddiff = DeepDiff(
            [0.012, 0.98],
            [0.013, 0.99],
            significant_digits=1,
            view='tree')
        assert ddiff == {} 
Example #15
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_significant_digits_with_sets(self):
        ddiff = DeepDiff(
            {0.012, 0.98},
            {0.013, 0.99},
            significant_digits=1,
            view='tree')
        assert ddiff == {} 
Example #16
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_significant_digits_with_ignore_order(self):
        ddiff = DeepDiff(
            [0.012, 0.98], [0.013, 0.99],
            significant_digits=1,
            ignore_order=True,
            view='tree')
        assert ddiff == {} 
Example #17
Source File: test_ignore_order.py    From deepdiff with MIT License 5 votes vote down vote up
def test_ignore_order_depth6(self):
        t1 = [[1, 2, 3, 4], [4, 2, 2, 1]]
        t2 = [[4, 1, 1, 1], [1, 3, 2, 4]]
        ddiff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True)
        expected = {
            'iterable_item_removed': {
                'root[1][1]': 2,
                'root[1][2]': 2
            },
            'repetition_change': {
                'root[1][3]': {
                    'old_repeat': 1,
                    'new_repeat': 3,
                    'old_indexes': [3],
                    'new_indexes': [1, 2, 3],
                    'value': 1
                }
            }
        }

        assert expected == ddiff 
Example #18
Source File: test_ignore_order.py    From deepdiff with MIT License 5 votes vote down vote up
def test_list_difference_of_bool_only_ignore_order(self, t1_0, t2_0):
        t1 = [t1_0]
        t2 = [t2_0]
        ddiff = DeepDiff(t1, t2, ignore_order=True)
        result = {'values_changed': {'root[0]': {'new_value': t2_0, 'old_value': t1_0}}}
        assert result == ddiff 
Example #19
Source File: test_ignore_order.py    From deepdiff with MIT License 5 votes vote down vote up
def test_list_difference_ignore_order(self):
        t1 = {1: 1, 4: {"a": "hello", "b": [1, 2, 3]}}
        t2 = {1: 1, 4: {"a": "hello", "b": [1, 3, 2, 3]}}
        ddiff = DeepDiff(t1, t2, ignore_order=True)
        assert {} == ddiff 
Example #20
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_item_added_extensive(self):
        t1 = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
        t2 = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'new': 1337}
        ddiff = DeepDiff(t1, t2)
        res = ddiff.tree
        (key, ) = res.keys()
        assert key == 'dictionary_item_added'
        assert len(res['dictionary_item_added']) == 1

        (added1, ) = res['dictionary_item_added']

        # assert added1 DiffLevel chain is valid at all
        assert added1.up.down == added1
        assert added1.down is None
        assert added1.up.up is None
        assert added1.all_up == added1.up
        assert added1.up.all_down == added1
        assert added1.report_type == 'dictionary_item_added'

        # assert DiffLevel chain points to the objects we entered
        assert added1.up.t1 == t1
        assert added1.up.t2 == t2

        assert added1.t1 is notpresent
        assert added1.t2 == 1337

        # assert DiffLevel child relationships are correct
        assert added1.up.t1_child_rel is None
        assert isinstance(added1.up.t2_child_rel, DictRelationship)
        assert added1.up.t2_child_rel.parent == added1.up.t2
        assert added1.up.t2_child_rel.child == added1.t2
        assert added1.up.t2_child_rel.param == 'new'

        assert added1.up.path() == "root"
        assert added1.path() == "root['new']" 
Example #21
Source File: test_diff_tree.py    From deepdiff with MIT License 5 votes vote down vote up
def test_significant_digits_signed_zero(self):
        t1 = 0.00001
        t2 = -0.0001
        ddiff = DeepDiff(t1, t2, significant_digits=2)
        res = ddiff.tree
        assert res == {}
        t1 = 1 * 10**-12
        t2 = -1 * 10**-12
        ddiff = DeepDiff(t1, t2, significant_digits=10)
        res = ddiff.tree
        assert res == {} 
Example #22
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_distance_of_list_sets(self):
        t1 = [{1, 2, 3}, {4, 5}]
        t2 = [{4, 5, 6}, {1, 2, 3}]
        ddiff = DeepDiff(t1, t2, ignore_order=True, cache_purge_level=0)
        delta = ddiff._to_delta_dict(report_repetition_required=False)
        assert {'set_item_added': {'root[1]': {6}}} == delta
        assert 1 == _get_item_length(ddiff)
        assert '0.05882352' == str(ddiff._get_rough_distance())[:10]
        assert 8 == ddiff._DistanceMixin__get_item_rough_length(ddiff.t1)
        assert 9 == ddiff._DistanceMixin__get_item_rough_length(ddiff.t2) 
Example #23
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_cutoff_distance_for_pairs_range(self):
        with pytest.raises(ValueError) as excinfo:
            DeepDiff(1, 2, cutoff_distance_for_pairs=2)
        assert CUTOFF_RANGE_ERROR_MSG == str(excinfo.value) 
Example #24
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_get_rough_length_after_cache_purge(self):
        diff = DeepDiff([1], ['a'])
        with pytest.raises(RuntimeError) as excinfo:
            diff._get_rough_distance()
        assert DISTANCE_CALCS_NEEDS_CACHE == str(excinfo.value) 
Example #25
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_get_distance_does_not_care_about_the_size_of_string(self):
        t1 = ["a", "b"]
        t2 = ["a", "b", "c", "dddddd"]
        diff = DeepDiff(t1, t2, get_deep_distance=True)
        dist = diff['deep_distance']
        assert str(dist)[:4] == '0.25' 
Example #26
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_get_distance_works_event_when_ignore_order_and_different_hasher(self):
        t1 = ["a", "b", 2]
        t2 = ["a", "b", "c", 2.2]
        diff = DeepDiff(t1, t2, ignore_order=True, get_deep_distance=True,
                        cache_size=100, hasher=sha256hex)
        dist = diff['deep_distance']
        assert str(dist)[:4] == '0.44' 
Example #27
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_get_distance_works_event_when_ignore_order_is_false3(self):
        t1 = ["a", "b"]
        t2 = ["a", "b", "c", "d"]
        diff = DeepDiff(t1, t2, get_deep_distance=True)
        dist = diff['deep_distance']
        assert str(dist)[:4] == '0.25' 
Example #28
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_get_distance_works_event_when_ignore_order_is_false1(self):
        t1 = 10
        t2 = 110
        diff = DeepDiff(t1, t2, get_deep_distance=True)
        dist = diff['deep_distance']
        assert dist == Decimal('0.25') 
Example #29
Source File: test_ignore_order.py    From deepdiff with MIT License 5 votes vote down vote up
def test_list_of_unhashable_difference_ignore_order2(self):
        t1 = [1, {"a": 2}, {"b": [3, 4, {1: 1}]}, "B"]
        t2 = [{"b": [3, 4, {1: 1}]}, {"a": 2}, {1: 1}]
        ddiff = DeepDiff(t1, t2, ignore_order=True)
        result = {
            'iterable_item_added': {
                'root[2]': {
                    1: 1
                }
            },
            'iterable_item_removed': {
                'root[3]': 'B',
                'root[0]': 1
            }
        }
        assert result == ddiff 
Example #30
Source File: test_distance.py    From deepdiff with MIT License 5 votes vote down vote up
def test_distance_of_tuple_in_list(self):
        t1 = {(2,), 4, 5, 6}
        t2 = {'right!', 'hello', 4, 5}
        diff = DeepDiff(t1, t2, ignore_order=True, view=DELTA_VIEW, get_deep_distance=True)
        assert {'set_item_removed': {'root': {(2,), 6}}, 'set_item_added': {'root': {'hello', 'right!'}}} == diff
        # delta view should not have the distance info in it
        assert 'get_deep_distance' not in diff