def test_must_supply_valid_timepoint(self): today = datetime.date.today() h = Habit(description="Brush my teeth", start=today, user=self.user, resolution='day') with self.assertRaises(ValueError): h.record(today, 5)
def test_cannot_record_negative_value(self): today = datetime.date.today() h = Habit(description="Brush my teeth", start=today, user=self.user, resolution='day') t = TimePeriod('day', 0, today) with self.assertRaises(ValueError): h.record(t, -10)
def test_set_reminder_schedule_validation(self): mondays = [n == 0 for n in range(7)] h = Habit() # Can't specify a negative hour with self.assertRaises(ValueError): h.set_reminder_schedule(mondays, -2) # Can't specify an hour > 23 with self.assertRaises(ValueError): h.set_reminder_schedule(mondays, 24) # Can't give a list of length != 7 with self.assertRaises(ValueError): h.set_reminder_schedule(mondays[:-1], 12) with self.assertRaises(ValueError): h.set_reminder_schedule(mondays + [True], 12)
def test_reminder_last_sent(self): h = Habit(user=self.user, start=datetime.date(2013, 3, 4), description='Test my reminders') d = timezone.now() h.reminder_last_sent = d with self.assertRaises(ValidationError): h.save() d = d.replace(minute=0, second=0, microsecond=0) h.reminder_last_sent = d h.save() self.assertEqual(h.reminder_last_sent, d)
def test_set_reminder_schedule(self): h = Habit() h.set_reminder_schedule(SCHEDULE_MONDAYS, 12) self.assertEquals(h.reminder_days, 1) self.assertEquals(h.reminder_hour, 12) h.set_reminder_schedule(SCHEDULE_MON_WED_FRI, 3) self.assertEquals(h.reminder_days, 1 | 4 | 16) self.assertEquals(h.reminder_hour, 3) h.set_reminder_schedule(SCHEDULE_WEEKENDS, 0) self.assertEquals(h.reminder_days, 32 | 64) self.assertEquals(h.reminder_hour, 0)
def handle(self, *args, **kwargs): """ Send all pending habit reminders. This command should be called on a cronjob once an hour (ideally shortly after the top of the hour). """ now = timezone.now().replace(minute=0, second=0, microsecond=0) for habit in Habit.scheduled_for_reminder(now.weekday(), now.hour): # Only send the reminder if either # a) the habit has never had any reminders send for it # b) the habit last had a reminder sent at least an hour ago if habit.reminder_last_sent is None or habit.reminder_last_sent < now: habit.reminder_last_sent = now habit.save() send_reminder_email(habit)
def test_scheduled_for_reminder_weekends(self): h = Habit(user=self.user, start=datetime.date(2013, 3, 4), description='Do a thing. On a day.') h.set_reminder_schedule(SCHEDULE_WEEKENDS, 0) h.save() self.assertEquals(len(self._scheduled(calendar.MONDAY, 0)), 0) self.assertEquals(len(self._scheduled(calendar.SATURDAY, 6)), 0) self.assertEquals(len(self._scheduled(calendar.SATURDAY, 0)), 1) self.assertEquals(self._scheduled(calendar.SATURDAY, 0)[0], h)
def test_scheduled_for_reminder_mondays(self): h = Habit(user=self.user, start=datetime.date(2013, 3, 4), description='Do a thing. On a day.') h.set_reminder_schedule(SCHEDULE_MONDAYS, 12) h.save() self.assertEquals(len(self._scheduled(calendar.MONDAY, 12)), 1) self.assertEquals(self._scheduled(calendar.MONDAY, 12)[0], h) self.assertEquals(len(self._scheduled(calendar.MONDAY, 6)), 0) self.assertEquals(len(self._scheduled(calendar.TUESDAY, 12)), 0)
def test_scheduled_for_reminder_multiple_habits(self): h1 = Habit(user=self.user, start=datetime.date(2013, 3, 4), description='Habit 1') h1.set_reminder_schedule(SCHEDULE_MONDAYS, 16) h1.save() h2 = Habit(user=self.user, start=datetime.date(2013, 3, 4), description='Habit 2') h2.set_reminder_schedule(SCHEDULE_MON_WED_FRI, 16) h2.save() self.assertEquals(len(self._scheduled(calendar.MONDAY, 0)), 0) self.assertEquals(len(self._scheduled(calendar.MONDAY, 16)), 2) self.assertEquals(len(self._scheduled(calendar.WEDNESDAY, 0)), 0) self.assertEquals(len(self._scheduled(calendar.WEDNESDAY, 16)), 1)
def _scheduled(self, weekday, hour): return list(Habit.scheduled_for_reminder(weekday, hour))
def test_get_reminder_schedule(self): h = Habit() h.reminder_days = 1 h.reminder_hour = 12 weekdays, hour = h.get_reminder_schedule() self.assertEquals(SCHEDULE_MONDAYS, weekdays) self.assertEquals(hour, 12) h.reminder_days = 1 | 4 | 16 h.reminder_hour = 3 weekdays, hour = h.get_reminder_schedule() self.assertEquals(SCHEDULE_MON_WED_FRI, weekdays) self.assertEquals(hour, 3) h.reminder_days = 32 | 64 h.reminder_hour = 0 weekdays, hour = h.get_reminder_schedule() self.assertEquals(SCHEDULE_WEEKENDS, weekdays) self.assertEquals(hour, 0)