'Test aggregation: avg'

def testAggregateAvg(self):

    period = self.period
    assertEqual = self.assertEqual
    assertAlmostEqual = self.assertAlmostEqual
    totals = period.aggregate(S3TimeSeriesFact('avg', 'base'))
    expected_rows = {'A': [45], 'B': [92.5], 'C': [202.5]}
    rows = period.rows
    assertEqual(set(rows.keys()), set(expected_rows.keys()))
    for (k, v) in rows.items():
        expected = expected_rows.get(k)
        for (i, expected_value) in enumerate(expected):
            assertAlmostEqual(v[i], expected_value, msg=('Row    %s:    %s    !=    %s' % (k, v, expected_rows.get(k))))
    expected_cols = {1: [107.5], 2: [58], 3: [213.3333333]}
    cols = period.cols
    assertEqual(set(cols.keys()), set(expected_cols.keys()))
    for (k, v) in cols.items():
        expected = expected_cols.get(k)
        for (i, expected_value) in enumerate(expected):
            assertAlmostEqual(v[i], expected_value, msg=('Column    %s:    %s    !=    %s' % (k, v, expected_cols.get(k))))
    expected_matrix = {('A', 1): [40], ('A', 2): [50], ('B', 2): [65], ('B', 3): [120], ('C', 1): [175], ('C', 2): [60], ('C', 3): [400]}
    matrix = period.matrix
    assertEqual(set(matrix.keys()), set(expected_matrix.keys()))
    for (k, v) in matrix.items():
        expected = expected_matrix.get(k)
        for (i, expected_value) in enumerate(expected):
            assertAlmostEqual(v[i], expected_value, msg=('Cell    %s:    %s    !=    %s' % (k, v, expected_matrix.get(k))))
    expected_totals = [113.3333333]
    assertEqual(len(period.totals), 1)
    assertAlmostEqual(period.totals[0], expected_totals[0])
    assertEqual(len(totals), 1)
    assertAlmostEqual(totals[0], expected_totals[0])
