def test_two_files_identical_values_works_with_custom_merger(self): json_data_merger = merge_results.JSONMerger() json_data_merger.fallback_matcher = json_data_merger.merge_equal self.assertMergeResults( { '/s/fileb1': '{"b": 2}', '/s/fileb2': '{"b": 2}', }, ('/output/out4', ['/s/fileb1', '/s/fileb2']), {'/output/out4': """\ { "b": 2 }"""}, json_data_merger=json_data_merger) self.assertMergeResults( { '/s/filef1b1': 'f1({"b": 2})', '/s/filef1b2': 'f1({"b": 2})', }, ('/output/outf4', ['/s/filef1b1', '/s/filef1b2']), {'/output/outf4': """\ f1({ "b": 2 })"""}, json_data_merger=json_data_merger)
def test_merge_listlike(self): m = merge_results.JSONMerger() tests = [ # expected, (inputa, inputb) ([1, 2], ([1], [2])), ([2, 1], ([2], [1])), ([1, 2, 3], ([], [1, 2, 3])), ([1, 2, 3], ([1], [2, 3])), ([1, 2, 3], ([1, 2], [3])), ([1, 2, 3], ([1, 2, 3], [])), ] for expected, (inputa, inputb) in tests: self.assertListEqual( expected, m.merge_listlike([inputa, inputb])) self.assertListEqual( expected, m.merge([inputa, inputb])) self.assertSequenceEqual( expected, m.merge_listlike([tuple(inputa), tuple(inputb)]), types.TupleType) self.assertSequenceEqual( expected, m.merge([tuple(inputa), tuple(inputb)]), types.TupleType)
def test_custom_match_on_obj_type(self): m = merge_results.JSONMerger() m.add_helper( merge_results.TypeMatch(int), lambda o, name=None: sum(o)) self.assertDictEqual({'a': 3}, m.merge([{'a': 1}, {'a': 2}])) self.assertDictEqual({'b': 3}, m.merge([{'b': 1}, {'b': 2}]))
def test_custom_match_on_obj_value(self): m = merge_results.JSONMerger() m.add_helper(merge_results.ValueMatch(3), lambda o, name=None: sum(o)) self.assertDictEqual({'a': 6}, m.merge([{'a': 3}, {'a': 3}])) self.assertDictEqual({'a': 5}, m.merge([{'a': 2}, {'a': 3}])) self.assertDictEqual({'a': 7}, m.merge([{'a': 3}, {'a': 4}])) with self.assertRaises(merge_results.MergeFailure): m.merge([{'a': 1}, {'a': 2}])
def test_custom_match_on_obj_value(self): m = merge_results.JSONMerger() m.add_helper(merge_results.ValueMatch(3), lambda a, b, name=None: a + b) self.assertDictEqual({'a': 6}, m.merge({'a': 3}, {'a': 3})) self.assertDictEqual({'a': 5}, m.merge({'a': 2}, {'a': 3})) self.assertDictEqual({'a': 7}, m.merge({'a': 3}, {'a': 4})) self.assertRaises(merge_results.MergeFailure, m.merge, {'a': 1}, {'a': 2})
def test_merge_compound_dict(self): m = merge_results.JSONMerger() tests = [ # expected, (inputa, inputb) ({'a': [1, 2]}, ({'a': [1]}, {'a': [2]})), ({'a': [1, 'c', 3]}, ({'a': [1]}, {'a': ['c', 3]})), ({'a': [1], 'b': [2]}, ({'a': [1]}, {'b': [2]})), ({'a': {'b': 1, 'c': 2}}, ({'a': {'b': 1}}, {'a': {'c': 2}})), ] for expected, (inputa, inputb) in tests: self.assertDictEqual(expected, m.merge_dictlike([inputa, inputb]))
def test_custom_match_on_name(self): m = merge_results.JSONMerger() m.add_helper(merge_results.NameRegexMatch('a'), lambda o, name=None: sum(o)) self.assertDictEqual({'a': 3}, m.merge([{'a': 1}, {'a': 2}])) with self.assertRaises(merge_results.MergeFailure): m.merge([{'b': 1}, {'b': 2}]) # Test that helpers that are added later have precedence. m.add_helper(merge_results.NameRegexMatch('b'), lambda o, name=None: sum(o)) m.add_helper(merge_results.NameRegexMatch('b'), lambda o, name=None: o[0] - o[1]) self.assertDictEqual({'b': -1}, m.merge([{'b': 1}, {'b': 2}]))
def test_custom_match_on_name(self): m = merge_results.JSONMerger() m.add_helper(merge_results.NameMatch('a'), lambda a, b, name=None: a + b) self.assertDictEqual({'a': 3}, m.merge({'a': 1}, {'a': 2})) self.assertRaises(merge_results.MergeFailure, m.merge, {'b': 1}, {'b': 2}) # Test that helpers that are added later have precedence. m.add_helper(merge_results.NameMatch('b'), lambda a, b, name=None: a + b) m.add_helper(merge_results.NameMatch('b'), lambda a, b, name=None: a - b) self.assertDictEqual({'b': -1}, m.merge({'b': 1}, {'b': 2}))
def test_merge_simple_dict(self): m = merge_results.JSONMerger() m.fallback_matcher = m.merge_equal tests = [ # expected, (inputa, inputb) ({'a': 1}, ({'a': 1}, {'a': 1})), ({'a': 1, 'b': 2}, ({'a': 1, 'b': 2}, {})), ({'a': 1, 'b': 2}, ({}, {'a': 1, 'b': 2})), ({'a': 1, 'b': 2}, ({'a': 1}, {'b': 2})), ({'a': 1, 'b': 2, 'c': 3}, ({'a': 1, 'b': 2, 'c': 3}, {})), ({'a': 1, 'b': 2, 'c': 3}, ({'a': 1, 'b': 2}, {'c': 3})), ({'a': 1, 'b': 2, 'c': 3}, ({'a': 1}, {'b': 2, 'c': 3})), ({'a': 1, 'b': 2, 'c': 3}, ({}, {'a': 1, 'b': 2, 'c': 3})), ] for expected, (inputa, inputb) in tests: self.assertDictEqual(expected, m.merge_dictlike([inputa, inputb])) self.assertRaises( merge_results.MergeFailure, m.merge_dictlike, [{'a': 1}, {'a': 2}])
def test_merge(self): m = merge_results.JSONMerger() m.fallback_matcher = m.merge_equal tests = [ # expected, (inputa, inputb) (None, (None, None)), ({ 'a': 1 }, ({ 'a': 1 }, None)), ({ 'b': 2 }, (None, { 'b': 2 })), ({ 'a': 1 }, ({ 'a': 1 }, { 'a': 1 })), # "Left side" value is None ({ 'a': None, 'b': 2 }, ({ 'a': None, 'b': 2 }, {})), ({ 'a': None, 'b': 2 }, ({}, { 'a': None, 'b': 2 })), ({ 'a': None, 'b': 2 }, ({ 'a': None }, { 'b': 2 })), ({ 'a': None, 'b': 2, 'c': 3 }, ({ 'a': None, 'b': 2, 'c': 3 }, {})), ({ 'a': None, 'b': 2, 'c': 3 }, ({ 'a': None, 'b': 2 }, { 'c': 3 })), ({ 'a': None, 'b': 2, 'c': 3 }, ({ 'a': None }, { 'b': 2, 'c': 3 })), ({ 'a': None, 'b': 2, 'c': 3 }, ({}, { 'a': None, 'b': 2, 'c': 3 })), # "Right side" value is None ({ 'a': 1, 'b': None }, ({ 'a': 1, 'b': None }, {})), ({ 'a': 1, 'b': None }, ({}, { 'a': 1, 'b': None })), ({ 'a': 1, 'b': None }, ({ 'a': 1 }, { 'b': None })), ({ 'a': 1, 'b': None, 'c': 3 }, ({ 'a': 1, 'b': None, 'c': 3 }, {})), ({ 'a': 1, 'b': None, 'c': 3 }, ({ 'a': 1, 'b': None }, { 'c': 3 })), ({ 'a': 1, 'b': None, 'c': 3 }, ({ 'a': 1 }, { 'b': None, 'c': 3 })), ({ 'a': 1, 'b': None, 'c': 3 }, ({}, { 'a': 1, 'b': None, 'c': 3 })), # Both values non-None ({ 'a': 1, 'b': 2 }, ({ 'a': 1, 'b': 2 }, {})), ({ 'a': 1, 'b': 2 }, ({}, { 'a': 1, 'b': 2 })), ({ 'a': 1, 'b': 2 }, ({ 'a': 1 }, { 'b': 2 })), ({ 'a': 1, 'b': 2, 'c': 3 }, ({ 'a': 1, 'b': 2, 'c': 3 }, {})), ({ 'a': 1, 'b': 2, 'c': 3 }, ({ 'a': 1, 'b': 2 }, { 'c': 3 })), ({ 'a': 1, 'b': 2, 'c': 3 }, ({ 'a': 1 }, { 'b': 2, 'c': 3 })), ({ 'a': 1, 'b': 2, 'c': 3 }, ({}, { 'a': 1, 'b': 2, 'c': 3 })), # Complex values ({ 'a': [1, 2] }, ({ 'a': [1] }, { 'a': [2] })), ({ 'a': [1, 'c', 3] }, ({ 'a': [1] }, { 'a': ['c', 3] })), ({ 'a': [1], 'b': [2] }, ({ 'a': [1] }, { 'b': [2] })), ({ 'a': { 'b': 1, 'c': 2 } }, ({ 'a': { 'b': 1 } }, { 'a': { 'c': 2 } })), ] for expected, (inputa, inputb) in tests: self.assertEqual(expected, m.merge([inputa, inputb])) with self.assertRaises(merge_results.MergeFailure): m.merge([{'a': 1}, {'a': 2}]) # Ordered values a = OrderedDict({'a': 1}) b = OrderedDict({'b': 2}) a_before_b = OrderedDict() a_before_b['a'] = 1 a_before_b['b'] = 2 b_before_a = OrderedDict() b_before_a['b'] = 2 b_before_a['a'] = 1 r1 = m.merge([a, b]) self.assertSequenceEqual(a_before_b.items(), r1.items()) self.assertIsInstance(r1, OrderedDict) r2 = m.merge([b, a]) self.assertSequenceEqual(b_before_a.items(), r2.items()) self.assertIsInstance(r2, OrderedDict)