def test_notification(self): self._save_schedule(self.user_1, 17, 18) for i in range(NOTIFICATION_PARTNER_THRESHOLD * 2): offset = (i % 2) * 2 user = test_utils.create_user( 'user_{0}'.format(i), ['en'], ['es']) self._save_schedule(user, 17 - offset, 18 - offset) tasks.notify_users() self.assertEqual( 0 if i < NOTIFICATION_PARTNER_THRESHOLD * 2 - 2 else 1, len(self._user_notification_records)) user_notification = self._user_notification_records[0] self.assertEqual( 1, len(user_notification.usernotificationrange_set.all()))
def test_notification_second_range_added(self): # first we save schedules for 8/11. Notifications are sent. self._save_schedule(self.user_1, 17, 18) for i in range(NOTIFICATION_PARTNER_THRESHOLD): user = test_utils.create_user( 'a_user_{0}'.format(i), ['en'], ['es']) self._save_schedule(user, 16, 19) tasks.notify_users() self.assertEqual(1, len(self._user_notification_records)) # now save schedules for days in future, after notification period. period_days = int(math.ceil(MIN_NOTIFICATION_PERIOD / 24.0)) self._save_schedule(self.user_1, 17, 18, 11 + period_days) for i in range(NOTIFICATION_PARTNER_THRESHOLD): user = test_utils.create_user( 'b_user_{0}'.format(i), ['en'], ['es']) self._save_schedule(user, 16, 19, 11 + period_days) # try to notify next day (should be before min notification period). # no new notifications should be sent tasks.utcnow = lambda: datetime(2011, 8, 12) tasks.notify_users() self.assertEqual(1, len(self._user_notification_records)) # now notify after min period. should get a new notification. tasks.utcnow = lambda: datetime(2011, 8, 11 + period_days, 2) tasks.notify_users() self.assertEqual(2, len(self._user_notification_records))
def test_notification_multiple_ranges(self): request = RequestMockup(self.user_1) rpc.save_schedule( request, _schedule_arg_for_times([[11, 17, 11, 18], [12, 17, 12, 18], [13, 17, 13, 18]]), 2011, 8, 11, TIME_RANGE) for i in range(NOTIFICATION_PARTNER_THRESHOLD): user = test_utils.create_user( 'a_user_{0}'.format(i), ['en'], ['es']) request = RequestMockup(user) times = [[11, 17, 11, 17], [12, 17, 12, 18]] if i < NOTIFICATION_PARTNER_THRESHOLD / 2: times.append([13, 17, 13, 18]) rpc.save_schedule(request, _schedule_arg_for_times(times), 2011, 8, 11, TIME_RANGE) tasks.notify_users() self.assertEquals(1, len(self._user_notification_records)) notification_record = self._user_notification_records[0] # user_1 should be notified for 2 ranges, not 1 or 3. self.assertEqual( 2, len(notification_record.usernotificationrange_set.all()))