def test_Merge_NestedDicts(self): mydicts = [{"a": {"x": 1, "y": 2}, "b": {"x": 1, "z": 3}}, {"a": {"x": 1, "y": 5}, "c": {"x": 1, "z": 3}}] actual = dicttools.merge(lambda d1, d2: dicttools.merge(lambda x, y: x + y, d1, d2), *mydicts) expected = {"a": {"x": 2, "y": 7}, "b": {"x": 1, "z": 3}, "c": {"x": 1, "z": 3}} self.assertEqual(expected, actual)
def test_Merge_OrderedDicts_ReturnOrderedDict(self): dict1 = collections.OrderedDict((('a', 1), ('b', 2))) dict2 = collections.OrderedDict((('b', 1), ('c', 2))) actual = dicttools.merge(dict1, dict2) expected = collections.OrderedDict((('a', 1), ('b', 1), ('c', 2))) self.assertIsInstance(actual, collections.OrderedDict) self.assertEqual(expected, actual)
def test_Merge_NestedDicts(self): mydicts = [{ "a": { "x": 1, "y": 2 }, "b": { "x": 1, "z": 3 } }, { "a": { "x": 1, "y": 5 }, "c": { "x": 1, "z": 3 } }] actual = dicttools.merge( lambda d1, d2: dicttools.merge(lambda x, y: x + y, d1, d2), *mydicts) expected = { "a": { "x": 2, "y": 7 }, "b": { "x": 1, "z": 3 }, "c": { "x": 1, "z": 3 } } self.assertEqual(expected, actual)
def test_Merge_KeyRepeats_ReturnDictWithValueFromLast(self): result = dicttools.merge({ 'A': 1, 'X': 8 }, { 'B': 2, 'X': 14 }, { 'C': 3, 'X': 12 }) self.assertEqual(12, result['X'])
def test_Merge_KeyRepeats_MergeFuncIsDifference_ReturnDictWithValuesSubtractedValues( self): result = dicttools.merge(lambda x, y: x - y, { 'A': 1, 'X': 8 }, { 'B': 2, 'X': 14 }, { 'C': 3, 'X': 12 }) self.assertEqual(-18, result['X'])
def test_Merge_KeyRepeats_MergeFuncIsSum_ReturnDictWithAddedDuplicatedValues( self): result = dicttools.merge(lambda x, y: x + y, { 'A': 1, 'X': 8 }, { 'B': 2, 'X': 14 }, { 'C': 3, 'X': 12 }) self.assertEqual(34, result['X'])
def test_Merge_3DictsGiven_ReturnDictContainingEachElementFromGivenDicts(self): result = dicttools.merge({'A': 1}, {'B': 2}, {'C': 3}) expected = {'A': 1, 'B': 2, 'C': 3} self.assertEqual(expected, result)
def test_Merge_ByDefault_ReturnEmptyDict(self): result = dicttools.merge() self.assertEqual({}, result)
def test_Merge_OnlyNonesGiven_ReturnEmptyDict(self): result = dicttools.merge(None, None) self.assertEqual({}, result)
def test_Merge_NoneGiven_MergeFuncGivenAndFirstArgumentIsNone_IgnoreNones(self): result = dicttools.merge(lambda x, y: x + y, None, {'A': 1}, {'B': 2}, {'C': 3}) self.assertEqual({'A', 'B', 'C'}, set(result))
def test_Merge_NoneGivenAsFirstArgument_IgnoreNones(self): result = dicttools.merge(None, {'A': 1}, {'B': 2}, {'C': 3}) self.assertEqual({'A', 'B', 'C'}, set(result))
def test_Merge_KeyRepeats_MergeFuncIsDifference_ReturnDictWithValuesSubtractedValues(self): result = dicttools.merge(lambda x, y: x - y, {'A': 1, 'X': 8}, {'B': 2, 'X': 14}, {'C': 3, 'X': 12}) self.assertEqual(-18, result['X'])
def test_Merge_KeyRepeats_MergeFuncIsSum_ReturnDictWithAddedDuplicatedValues(self): result = dicttools.merge(lambda x, y: x + y, {'A': 1, 'X': 8}, {'B': 2, 'X': 14}, {'C': 3, 'X': 12}) self.assertEqual(34, result['X'])
def test_Merge_KeyRepeats_ReturnDictWithValueFromLast(self): result = dicttools.merge({'A': 1, 'X': 8}, {'B': 2, 'X': 14}, {'C': 3, 'X': 12}) self.assertEqual(12, result['X'])
def test_Merge_ByDefault_ReturnEmptyDict(self): result = dicttools.merge() self.assertEqual({}, result)
def test_Merge_3DictsGiven_ReturnDictContainingEachElementFromGivenDicts( self): result = dicttools.merge({'A': 1}, {'B': 2}, {'C': 3}) expected = {'A': 1, 'B': 2, 'C': 3} self.assertEqual(expected, result)
def test_Merge_OnlyNonesGiven_ReturnEmptyDict(self): result = dicttools.merge(None, None) self.assertEqual({}, result)
def test_Merge_NoneGiven_MergeFuncGivenAndFirstArgumentIsNone_IgnoreNones( self): result = dicttools.merge(lambda x, y: x + y, None, {'A': 1}, {'B': 2}, {'C': 3}) self.assertEqual({'A', 'B', 'C'}, set(result))
def test_Merge_NoneGivenAsFirstArgument_IgnoreNones(self): result = dicttools.merge(None, {'A': 1}, {'B': 2}, {'C': 3}) self.assertEqual({'A', 'B', 'C'}, set(result))