def testGivenKeyFunctionAsPositionalAttributeGroupsByItsResult(self): self.assertEqual( { 2: [(0, 2), (1, 1), (1, 1)], 3: [(1, 2), (2, 1), (3, 0)] }, groupBy([(1, 2), (2, 1), (0, 2), (3, 0), (1, 1), (1, 1)], lambda x: x[0] + x[1]))
def testGivenKeyFunctionAsKeywordAttributeGroupsByItsResult(self): self.assertEqual( { 1: [(1, 1), (2, 1, 8)], 2: [(1, 2), (3, 2)], 4: [(3, 4)] }, groupBy([(1, 2), (3, 4), (3, 2), (1, 1), (2, 1, 8)], getKey=operator.itemgetter(1)))
def testKeyFunctionDefaultsToIdentity(self): self.assertEqual({1: [1], 2: [2]}, groupBy([1, 2]))
def testGivenTupleOfNamesOfAttributesGroupsByTheirCombination(self): self.assertEqual({(1, 1): [(1, 1)], (1, 2): [(1, 2)], (2, 1): [(2, 1)]}, groupBy([Pair(1, 2), Pair(1, 1), Pair(2, 1)], ('a', 'b')))
def testGivenNameOfAttributeGroupsByTheAttribute(self): self.assertEqual({1: [(1, 2), (1, 1)], 2: [(2, 1)]}, groupBy([Pair(1, 2), Pair(1, 1), Pair(2, 1)], 'a'))
def testGivenNoObjectsReturnsEmptyDict(self): self.assertEqual({}, groupBy([], getKey=lambda x: x[0] + x[1]))
def testGivenTypeAsKeyFunctionGroupsByItsResult(self): self.assertEqual({'2': [2, 2], '3': [3]}, groupBy([2, 3, 2], getKey=str))
def testGivenKeyFunctionAsPositionalAttributeGroupsByItsResult(self): self.assertEqual({2: [(0, 2), (1, 1), (1, 1)], 3: [(1, 2), (2, 1), (3, 0)]}, groupBy([(1, 2), (2, 1), (0, 2), (3, 0), (1, 1), (1, 1)], lambda x: x[0] + x[1]))
def testRequiredKeysDoNotConflictWithKeysDefinedByInput(self): self.assertEqual({1: [1]}, groupBy([1], requiredKeys=[1]))
def testOutputAlwaysContainsRequiredKeys(self): self.assertEqual({1: [1], 'a': []}, groupBy([1], requiredKeys=['a']))
def testGivenTupleOfNamesOfAttributesGroupsByTheirCombination(self): self.assertEqual({ (1, 1): [(1, 1)], (1, 2): [(1, 2)], (2, 1): [(2, 1)] }, groupBy([Pair(1, 2), Pair(1, 1), Pair(2, 1)], ('a', 'b')))
def testGivenNameOfAttributeGroupsByTheAttribute(self): self.assertEqual({ 1: [(1, 2), (1, 1)], 2: [(2, 1)] }, groupBy([Pair(1, 2), Pair(1, 1), Pair(2, 1)], 'a'))
def testGivenTypeAsKeyFunctionGroupsByItsResult(self): self.assertEqual({ '2': [2, 2], '3': [3] }, groupBy([2, 3, 2], getKey=str))
def testGivenKeyFunctionAsKeywordAttributeGroupsByItsResult(self): self.assertEqual({1: [(1, 1), (2, 1, 8)], 2: [(1, 2), (3, 2)], 4: [(3, 4)]}, groupBy([(1, 2), (3, 4), (3, 2), (1, 1), (2, 1, 8)], getKey=operator.itemgetter(1)))