def test_group_by_group_by_key(): common_key_values = [{1: 1, 'key': 'value'}, {2: 2, 'key': 'value'}, {3: 3, 'key': 'value'}, {4: 4, 'key': 'value2'}, {5: 5, 'key': 'value2'}, {6: 6, 'key': 'value3'}] d = {'value': [{1: 1, 'key': 'value'}, {2: 2, 'key': 'value'}, {3: 3, 'key': 'value'}], 'value2': [{4: 4, 'key': 'value2'}, {5: 5, 'key': 'value2'}], 'value3': [{6: 6, 'key': 'value3'}]} assert _group_by(common_key_values, 'key') == d
def test_group_by_all_different(): all_diferent = [{1: 1}, {2: 2}, {3: 3}, {4: 4}, {5: 5}, {6: 6}] d = { 1: [{1: 1}], 2: [{2: 2}], 3: [{3: 3}], 4: [{4: 4}], 5: [{5: 5}], 6: [{6: 6}] } assert _group_by(all_diferent, lambda s: list(s.keys())[0]) == d
def test_group_by_using_fn(): requires_fn = [{'k': 'v '}, {'k': ' v '}, {'k': 'v '}, {'k': 'v2 '}, {'k': 'v2'}, {'k': ' v2 '}] d = {'v': [{'k': 'v '}, {'k': ' v '}, {'k': 'v '}], 'v2': [{'k': 'v2 '}, {'k': 'v2'}, {'k': ' v2 '}]} assert _group_by(requires_fn, lambda d: d['k'].strip()) == d