Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Add test for multiple months grouped by key
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrco committed Oct 28, 2014
1 parent f186513 commit 0ac6a95
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 48 deletions.
13 changes: 5 additions & 8 deletions backdrop/core/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _time_to_index(dt):

def timeseries(start, end, period, data, default):
"""
Return a list of results from start to end, with 'missing'
Return a list of results from start to end, with missing
data filled in with default.
"""
data_by_start_at = _group_by_start_at(data)
Expand All @@ -169,7 +169,6 @@ def timeseries(start, end, period, data, default):
return results


#@statsd.time_this_mother because it could get slow with loads of group_bys
def timeseries_the_return_of_the_keys(start, end, period, data, default, grouped_by=['paymentStatus', 'paymentThing', 'no']):

# group data by permutation of group_by keys
Expand All @@ -183,11 +182,9 @@ def mad_thing(key):
results = []
# for each time period (e.g. 1 week) in the period requested (e.g. 3 weeks)
for period_start, period_end in period.range(start, end):
print "Period: %s to %s" % (period_start, period_end)
time_index = _time_to_index(period_start)

print "Permutations: %d" % len(permutations)
for idx, group in enumerate(permutations):
for group in permutations:
group['_start_at'] = period_start
group['_end_at'] = period_end
match = False
# iterate through all data, matching keys and a time index.
for value in data:
Expand All @@ -198,7 +195,7 @@ def mad_thing(key):

# No match found in all values
if not match:
results.append(idx)
results.append(_merge(default, group))

return results

Expand Down
64 changes: 24 additions & 40 deletions tests/core/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,59 +54,43 @@ def test_include_all_key_permutations(self):
assert_that(series, has_length(18))


@nottest
def test_fill_missing_periods_includes_all_key_permutations(self):

def test_multiple_group_by_months(self):
"""
Test that given multiple channels, flat data returned for a encompassing time period
is
Test that the response correctly returns multiple time periods with group by keys.
"""

first_month_start = datetime.datetime(2013, 1, 1, 0, 0, tzinfo=pytz.UTC)
second_month_start = datetime.datetime(2013, 2, 1, 0, 0, tzinfo=pytz.UTC)

data = [
{
'_end_at': datetime.datetime(2013, 2, 1, 0, 0, tzinfo=pytz.UTC),
'_count': 17.0,
'_count': 17.0,
'paymentStatus': 'Success',
'paymentThing': 'blue',
'no': 'yes',
'_start_at': datetime.datetime(2013, 1, 1, 0, 0, tzinfo=pytz.UTC)
'_start_at': first_month_start
},
{
'_end_at': datetime.datetime(2013, 2, 1, 0, 0, tzinfo=pytz.UTC),
'_count': 18.0,
'_end_at': datetime.datetime(2013, 3, 1, 0, 0, tzinfo=pytz.UTC),
'_count': 18.0,
'paymentStatus': 'Unknown',
'paymentThing': 'elephant',
'no': 'maybe',
'_start_at': datetime.datetime(2013, 1, 1, 0, 0, tzinfo=pytz.UTC)
'_start_at': second_month_start
},
{
'_end_at': datetime.datetime(2013, 2, 1, 0, 0, tzinfo=pytz.UTC),
'_count': 30.0,
'paymentStatus': '',
'paymentThing': 'kant',
'no': 'maybe',
'_start_at': datetime.datetime(2013, 1, 1, 0, 0, tzinfo=pytz.UTC)
}
]

# make data a thing pulled from the cursor not from inside of fill_missing_periods
# as this has already run through _add() and that does some munge
period_thingy = PeriodFlatData([], MONTH)
period_thingy._data = data
filled_in_data = period_thingy.fill_missing_periods(
datetime.datetime(2012, 12, 1, 0, 0, tzinfo=pytz.UTC),
datetime.datetime(2013, 2, 1, 0, 0, tzinfo=pytz.UTC)
)
print filled_in_data

assert_that(filled_in_data, has_length(36))

keyed_things = filled_in_data

assert_that(keyed_things, has_key('Unknown'))
assert_that(keyed_things['Unknown'], has_length(2))
assert_that(keyed_things, has_key('Success'))
assert_that(keyed_things['Success'], has_length(2))
assert_that(keyed_things, has_key(''))
assert_that(keyed_things[''], has_length(2))


series = timeseries_the_return_of_the_keys(start=datetime.datetime(2013, 1, 1, 0, 0, tzinfo=pytz.UTC),
end=datetime.datetime(2013, 3, 1, 0, 0, tzinfo=pytz.UTC),
period=MONTH,
data=data,
default={"_count": 0})

assert_that(series, has_length(16))

# Assert the correct date spans have been returned
first_month = [point for point in series if point['_start_at'] == first_month_start]
assert_that(first_month, has_length(8))
second_month = [point for point in series if point['_start_at'] == second_month_start]
assert_that(second_month, has_length(8))

0 comments on commit 0ac6a95

Please sign in to comment.