def test_text_diff_disabled_when_ON_disabled(): # noqa N802 a = ['a'] b = ['a\nb'] assert {'D': [{'O': 'a'}]} == diff(a, b, N=False, text_diff_ctx=3) assert {'D': [{'N': 'a\nb'}]} == diff(a, b, O=False, text_diff_ctx=3) assert {} == diff(a, b, N=False, O=False, text_diff_ctx=3)
def test_local_objects(): def local_function_cant_be_pickled(): pass a = [local_function_cant_be_pickled] b = [] with pytest.raises(Exception): # Can't pickle local object diff(a, b)
def test_dict_diff__keys_sorted(): a = {'1': 1, '2': {'9': 9, '10': 10}, '3': 3} b = {'1': 1, '2': {'9': 8, '10': 10}, '4': 4} d = diff(a, b) expected = [ (d, '1', d['D']['1']), (d['D']['1'], None, None), (d, '2', d['D']['2']), (d['D']['2'], '10', d['D']['2']['D']['10']), (d['D']['2']['D']['10'], None, None), (d['D']['2'], '9', d['D']['2']['D']['9']), (d['D']['2']['D']['9'], None, None), (d, '3', d['D']['3']), (d['D']['3'], None, None), (d, '4', d['D']['4']), (d['D']['4'], None, None), ] got = list(Iterator(sort_keys=True).iterate(d)) assert expected == got
def test_dict_diff(): a = {'1': 1, '2': {'9': 9, '10': 10}, '3': 3} b = {'1': 1, '2': {'9': 8, '10': 10}, '4': 4} d = diff(a, b) expected = [ (d, '1', d['D']['1'], 0), (d['D']['1'], None, None, 1), (d, '2', d['D']['2'], 0), (d['D']['2'], '10', d['D']['2']['D']['10'], 1), (d['D']['2']['D']['10'], None, None, 2), (d['D']['2'], '9', d['D']['2']['D']['9'], 1), (d['D']['2']['D']['9'], None, None, 2), (d, '3', d['D']['3'], 0), (d['D']['3'], None, None, 1), (d, '4', d['D']['4'], 0), (d['D']['4'], None, None, 1), ] got = list(Iterator().iterate(d)) for i in expected: assert i in got assert len(got) == 11
def test_diff(name): a = TESTS[name]['a'] b = TESTS[name]['b'] expected = TESTS[name]['diff'] opts = TESTS[name].get('diff_opts', {}) got = diff(a, b, **opts) assert expected == got
def test_nested_derivatives(): original = {} a = {'k': original} derivative = SubclassedDict() b = {'k': derivative} d = diff(a, b) assert {'D': {'k': {'N': derivative, 'O': original}}} == d
def test_different_object_attributes(): a = SubclassedDict() a.arbitrary_attr = True b = SubclassedDict() b.arbitrary_attr = False d = diff(a, b) assert {'N': b, 'O': a} == d
def test_scalar_diff(): a = 0 b = 1 d = diff(a, b) expected = [(d, None, None, 0)] got = list(Iterator().iterate(d)) assert expected == got
def __diff__(self, other, **kwargs): ret = diff(self.value, other.value, **kwargs) if 'U' in ret: ret = {'U': self} elif ret: ret = {'D': self.__class__(ret)} return ret
def test_patch_without_diff_method(): a = Custom(value={'old': 0}) b = Custom(value={'new': 1}) diff_ = diff(a, b) a = patch(a, diff_, patch_method='__patch__') assert isinstance(a, Custom) assert {'new': 1} == a.value
def test_patch_with_diff_method(): a = Custom(value=0) b = Custom(value=1) diff_ = diff(a, b, diff_method='__diff__') a = patch(a, diff_, patch_method='__patch__') assert isinstance(a, Custom) assert 1 == a.value
def test_diff_opts(): a = Custom(value=0) b = Custom(value=1) result = diff(a, b, O=False, diff_method='__diff__') # noqa: E741 subdiff = result.pop('D') assert isinstance(subdiff, Custom) assert {'N': 1} == subdiff.value assert {} == result
def test_diff_different(): a = Custom(value=0) b = Custom(value=1) result = diff(a, b, diff_method='__diff__') subdiff = result.pop('D') assert isinstance(subdiff, Custom) assert {'N': 1, 'O': 0} == subdiff.value assert {} == result
def test_patch(name): target = TESTS[name]['a'] try: expected = TESTS[name]['patched'] except KeyError: expected = TESTS[name]['b'] got = patch(target, TESTS[name]['diff']) assert not diff(expected, got, U=False)
def test_set_diff(): a = {0, 1} b = {0, 2} d = diff(a, b) expected = [ ({'D': [{'U': 0}, {'R': 1}, {'A': 2}], 'E': set()}, None, None), ] got = list(Iterator().iterate(d)) assert expected == got
def do_test_function(test, func): if 'raises' in test: with test['raises']: func(test) else: # ensure diff is correct diff_opts = test.get('diff_opts', {}) diff_should_be = nested_diff.diff(test['a'], test['b'], **diff_opts) if test.get('diff', {}) != diff_should_be: raise ValueError(diff_should_be) assert test['result'] == func(test)
def test_list_diff__noU(): # noqa N802 a = [0, [1], 3] b = [0, [1, 2], 3] d = diff(a, b, U=False) expected = [ (d, 1, d['D'][0], 0), (d['D'][0], 1, d['D'][0]['D'][0], 1), (d['D'][0]['D'][0], None, None, 2), ] got = list(Iterator().iterate(d)) assert expected == got
def diff(self, a, b): """ Return diff for passed objects :param a: First object to diff. :param b: Second object to diff. """ return nested_diff.diff( a, b, multiline_diff_context=self.args.text_ctx, A=self.args.A, N=self.args.N, O=self.args.O, # noqa: E741 R=self.args.R, U=self.args.U, )
def test_bad_structure(config, bad_structure, config_class): # should create a test delattr(config_class, '_inst') #print(config) rewrite_config(bad_structure) with pytest.warns(BadStructureWarning): new_config = config_class() d = diff(new_config._default_structure, new_config._configuration_dict) # make sure we have the default config now assert "D" not in d
def do_test_function(test, func): if 'raises' in test: with test['raises']: func(test) else: try: a = test['a'] b = test['b'] except KeyError: # initial values may absent in some test cases pass else: # ensure diff is correct diff_opts = test.get('diff_opts', {}) diff_should_be = nested_diff.diff(a, b, **diff_opts) if test.get('diff', {}) != diff_should_be: raise ValueError(diff_should_be) assert test['result'] == func(test)
def test_list_diff(): a = [0, [1], 3] b = [0, [1, 2], 3] d = diff(a, b) expected = [ (d, 0, d['D'][0], 0), (d['D'][0], None, None, 1), (d, 1, d['D'][1], 0), (d['D'][1], 0, d['D'][1]['D'][0], 1), (d['D'][1]['D'][0], None, None, 2), (d['D'][1], 1, d['D'][1]['D'][1], 1), (d['D'][1]['D'][1], None, None, 2), (d, 2, d['D'][2], 0), (d['D'][2], None, None, 1), ] got = list(Iterator().iterate(d)) assert expected == got
def _check_same_structure(self, user_config_dict: dict) -> bool: """ Return True if d1 and d2 have the same keys structure (same set of keys, and all nested dictionaries have the same structure) :param d1: dictionary 1 :param d2: dictionary 2 :return: True or False """ d1 = self._subs_values_with_none(sorted(user_config_dict)) d2 = self._subs_values_with_none(sorted(self._default_structure)) difference = diff(d1, d2) if "D" in difference: return False return True
def test_diff_equal(): a = Custom(value=[0]) b = Custom(value=[0]) assert {'U': a} == diff(a, b, diff_method='__diff__')