'Test aggregation of event data, cumulative'

def testEventDataCumulativeAggregation(self):

    s3db = current.s3db
    assertEqual = self.assertEqual
    assertTrue = self.assertTrue
    PERIODS = 'p'
    TIMES = 't'
    VALUE = 'v'
    resource = s3db.resource('tp_test_events')
    ts = S3TimeSeries(resource, event_start='event_start', event_end='event_end', start='2012-01-01', end='2013-01-01', slots='months', facts=[S3TimeSeriesFact('cumulate', None, slope='parameter1', interval='months')])
    assertEqual(ts.event_frame.slots, 'months')
    expected = [((2012, 1, 1), (2012, 2, 1), [45]), ((2012, 2, 1), (2012, 3, 1), [45]), ((2012, 3, 1), (2012, 4, 1), [45]), ((2012, 4, 1), (2012, 5, 1), [45]), ((2012, 5, 1), (2012, 6, 1), [45]), ((2012, 6, 1), (2012, 7, 1), [45]), ((2012, 7, 1), (2012, 8, 1), [48]), ((2012, 8, 1), (2012, 9, 1), [51]), ((2012, 9, 1), (2012, 10, 1), [54]), ((2012, 10, 1), (2012, 11, 1), [60]), ((2012, 11, 1), (2012, 12, 1), [66]), ((2012, 12, 1), (2013, 1, 1), [72])]
    result = ts.as_dict()
    periods = result[PERIODS]
    for (i, period) in enumerate(periods):
        (expected_start, expected_end, expected_value) = expected[i]
        expected_start = tp_datetime(*expected_start).isoformat()
        expected_end = tp_datetime(*expected_end).isoformat()
        dates = period.get(TIMES)
        assertTrue(isinstance(dates, tuple))
        (start, end) = dates
        assertEqual(start, expected_start, msg=('Period    %s    start    should    be    %s,    but    is    %s' % (i, expected_start, start)))
        assertEqual(end, expected_end, msg=('Period    %s    end    should    be    %s,    but    is    %s' % (i, expected_end, end)))
        value = period.get(VALUE)
        assertEqual(value, expected_value, msg=('Period    %s    cumulative    sum    should    be    %s,    but    is    %s' % (i, expected_value, value)))
