def testAggregate(self): """ Test aggregation of aggregates """ assertTrue = self.assertTrue assertEqual = self.assertEqual assertRaises = self.assertRaises # Create two aggregates values1 = [2, 4, 6, 12] ga1 = S3GroupAggregate("avg", "Example", values1) assertEqual(ga1.result, 6.0) values2 = [5, 3, 9, 8] ga2 = S3GroupAggregate("avg", "Example", values2) assertEqual(ga2.result, 6.25) # Combine the two aggregates ga = S3GroupAggregate.aggregate((ga1, ga2)) assertEqual(ga.result, 6.125) for value in values1 + values2: assertTrue(value in ga.values) # Combine with method mismatch ga = S3GroupAggregate("min", "Example", values1) with assertRaises(TypeError): S3GroupAggregate((ga1, ga2, ga)) # Combine with key mismatch ga = S3GroupAggregate("avg", "Other", values1) with assertRaises(TypeError): S3GroupAggregate((ga1, ga2, ga))
def testAvg(self): """ Test aggregation method 'avg' """ assertEqual = self.assertEqual # Regular input values = [2, 1.7, 3, 1] aggregate = S3GroupAggregate("avg", "Example", values) assertEqual(aggregate.result, 1.925) # None gives always None values = None aggregate = S3GroupAggregate("avg", "Example", values) assertEqual(aggregate.result, None) # Invalid input: non-numeric type values = ["1", "other"] aggregate = S3GroupAggregate("avg", "Example", values) assertEqual(aggregate.result, None)
def testMax(self): """ Test aggregation method 'max' """ assertEqual = self.assertEqual # Regular input values = [2, 1.7, 3, 1] aggregate = S3GroupAggregate("max", "Example", values) assertEqual(aggregate.result, 3) # None gives always None values = None aggregate = S3GroupAggregate("max", "Example", values) assertEqual(aggregate.result, None) # Invalid input: non-iterable values = lambda: None aggregate = S3GroupAggregate("max", "Example", values) assertEqual(aggregate.result, None)
def testCount(self): """ Test aggregation method 'count' """ assertEqual = self.assertEqual # Regular input values = ["A", "B", "B", "C"] aggregate = S3GroupAggregate("count", "Example", values) assertEqual(aggregate.result, 3) # None gives always None values = None aggregate = S3GroupAggregate("count", "Example", values) assertEqual(aggregate.result, None) # Invalid input: non-iterable type values = 17 aggregate = S3GroupAggregate("count", "Example", values) assertEqual(aggregate.result, None)
def testSum(self): """ Test aggregation method 'sum' """ assertEqual = self.assertEqual # Regular input values = [2, 1.7, 3, 1] aggregate = S3GroupAggregate("sum", "Example", values) assertEqual(aggregate.result, 7.7) # Empty input values = [] aggregate = S3GroupAggregate("sum", "Example", values) assertEqual(aggregate.result, 0.0) # Non-numeric type (e.g. returned from virtual fields) values = ["1", "other"] aggregate = S3GroupAggregate("sum", "Example", values) assertEqual(aggregate.result, 0.0) # None gives always None values = None aggregate = S3GroupAggregate("sum", "Example", values) assertEqual(aggregate.result, None)