def test_incoming_chart(self): url = reverse('statistics.incoming_chart') self.assertLoginRedirect(self.url_get('unicef', url), 'unicef', url) self.new_messages(date(2016, 1, 1), 1) # Jan 1st msgs = self.new_messages(date(2016, 1, 15), 1) # Jan 15th self.new_messages(date(2016, 1, 16), 2) # Jan 16th # add label to message on Jan 15th msgs[0].label(self.tea) self.login(self.user3) # simulate making requests on April 4th with patch.object(timezone, 'now', return_value=datetime(2016, 4, 10, 9, 0, tzinfo=pytz.UTC)): response = self.url_get('unicef', url) series = response.json['data'] self.assertEqual(len(series), 92) self.assertEqual(series[0], [date_to_milliseconds(date(2016, 1, 10)), 0]) # from Jan 10th self.assertEqual(series[5], [date_to_milliseconds(date(2016, 1, 15)), 1]) self.assertEqual(series[6], [date_to_milliseconds(date(2016, 1, 16)), 2]) self.assertEqual(series[-1], [date_to_milliseconds(date(2016, 4, 10)), 0]) # to April 4th response = self.url_get('unicef', url + '?label=%d' % self.tea.pk) series = response.json['data'] self.assertEqual(len(series), 92) self.assertEqual(series[5], [date_to_milliseconds(date(2016, 1, 15)), 1]) self.assertEqual(series[6], [date_to_milliseconds(date(2016, 1, 16)), 0])
def test_incoming_chart(self): url = reverse("statistics.incoming_chart") self.assertLoginRedirect(self.url_get("unicef", url), url) self.new_messages(date(2016, 1, 1), 1) # Jan 1st msgs = self.new_messages(date(2016, 1, 15), 1) # Jan 15th self.new_messages(date(2016, 1, 16), 2) # Jan 16th # add label to message on Jan 15th msgs[0].label(self.tea) self.login(self.user3) # simulate making requests on March 10th with patch.object(timezone, "now", return_value=datetime(2016, 3, 10, 9, 0, tzinfo=pytz.UTC)): response = self.url_get("unicef", url) series = response.json["series"] self.assertEqual(len(series), 60) self.assertEqual(series[0], [date_to_milliseconds(date(2016, 1, 11)), 0]) # from Jan 11th self.assertEqual(series[4], [date_to_milliseconds(date(2016, 1, 15)), 1]) self.assertEqual(series[5], [date_to_milliseconds(date(2016, 1, 16)), 2]) self.assertEqual(series[-1], [date_to_milliseconds(date(2016, 3, 10)), 0]) # to March 10th response = self.url_get("unicef", url + "?label=%d" % self.tea.pk) series = response.json["series"] self.assertEqual(len(series), 60) self.assertEqual(series[4], [date_to_milliseconds(date(2016, 1, 15)), 1]) self.assertEqual(series[5], [date_to_milliseconds(date(2016, 1, 16)), 0])
def get_data(self, request): today = datetime_to_date(timezone.now(), self.request.org) since = today - relativedelta(days=self.num_days - 1) totals = self.get_day_totals(request, since) totals_by_day = {t[0]: t[1] for t in totals} series = [] day = since while day <= today: total = totals_by_day.get(day, 0) series.append((date_to_milliseconds(day), total)) day += relativedelta(days=1) return {'series': series}