From adf1172c398c7f71f0c2c2f659a4c8e54ea23231 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 9 Jan 2021 13:59:54 +0000 Subject: [PATCH] Add tests for sending task reminders (#757) Co-authored-by: kolaente Reviewed-on: https://kolaente.dev/vikunja/api/pulls/757 Co-authored-by: konrad Co-committed-by: konrad --- pkg/models/task_reminder.go | 62 +++++++++++++++++++------------- pkg/models/task_reminder_test.go | 51 ++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 pkg/models/task_reminder_test.go diff --git a/pkg/models/task_reminder.go b/pkg/models/task_reminder.go index bcaaedb32b..c1a43086e0 100644 --- a/pkg/models/task_reminder.go +++ b/pkg/models/task_reminder.go @@ -92,6 +92,40 @@ func getTaskUsersForTasks(s *xorm.Session, taskIDs []int64) (taskUsers []*taskUs return } +func getTasksWithRemindersInTheNextMinute(s *xorm.Session, now time.Time) (taskIDs []int64, err error) { + + tz := config.GetTimeZone() + const dbFormat = `2006-01-02 15:04:05` + + // By default, time.Now() includes nanoseconds which we don't save. That results in getting the wrong dates, + // so we make sure the time we use to get the reminders don't contain nanoseconds. + now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location()).In(tz) + nextMinute := now.Add(1 * time.Minute) + + log.Debugf("[Task Reminder Cron] Looking for reminders between %s and %s to send...", now, nextMinute) + + reminders := []*TaskReminder{} + err = s. + Where("reminder >= ? and reminder < ?", now.Format(dbFormat), nextMinute.Format(dbFormat)). + Find(&reminders) + if err != nil { + return + } + + log.Debugf("[Task Reminder Cron] Found %d reminders", len(reminders)) + + if len(reminders) == 0 { + return + } + + // We're sending a reminder to everyone who is assigned to the task or has created it. + for _, r := range reminders { + taskIDs = append(taskIDs, r.TaskID) + } + + return +} + // RegisterReminderCron registers a cron function which runs every minute to check if any reminders are due the // next minute to send emails. func RegisterReminderCron() { @@ -105,42 +139,20 @@ func RegisterReminderCron() { } tz := config.GetTimeZone() - const dbFormat = `2006-01-02 15:04:05` log.Debugf("[Task Reminder Cron] Timezone is %s", tz) s := db.NewSession() err := cron.Schedule("* * * * *", func() { - // By default, time.Now() includes nanoseconds which we don't save. That results in getting the wrong dates, - // so we make sure the time we use to get the reminders don't contain nanoseconds. + now := time.Now() - now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, now.Location()).In(tz) - nextMinute := now.Add(1 * time.Minute) - - log.Debugf("[Task Reminder Cron] Looking for reminders between %s and %s to send...", now, nextMinute) - - reminders := []*TaskReminder{} - err := s. - Where("reminder >= ? and reminder < ?", now.Format(dbFormat), nextMinute.Format(dbFormat)). - Find(&reminders) + taskIDs, err := getTasksWithRemindersInTheNextMinute(s, now) if err != nil { - log.Errorf("[Task Reminder Cron] Could not get reminders for the next minute: %s", err) + log.Errorf("[Task Reminder Cron] Could not get tasks with reminders in the next minute: %s", err) return } - log.Debugf("[Task Reminder Cron] Found %d reminders", len(reminders)) - - if len(reminders) == 0 { - return - } - - // We're sending a reminder to everyone who is assigned to the task or has created it. - var taskIDs []int64 - for _, r := range reminders { - taskIDs = append(taskIDs, r.TaskID) - } - users, err := getTaskUsersForTasks(s, taskIDs) if err != nil { log.Errorf("[Task Reminder Cron] Could not get task users to send them reminders: %s", err) diff --git a/pkg/models/task_reminder_test.go b/pkg/models/task_reminder_test.go new file mode 100644 index 0000000000..a166165d39 --- /dev/null +++ b/pkg/models/task_reminder_test.go @@ -0,0 +1,51 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package models + +import ( + "testing" + "time" + + "code.vikunja.io/api/pkg/db" + "github.com/stretchr/testify/assert" +) + +func TestReminderGetTasksInTheNextMinute(t *testing.T) { + t.Run("Found Tasks", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + now, err := time.Parse(time.RFC3339Nano, "2018-12-01T01:13:00Z") + assert.NoError(t, err) + taskIDs, err := getTasksWithRemindersInTheNextMinute(s, now) + assert.NoError(t, err) + assert.Len(t, taskIDs, 1) + assert.Equal(t, int64(27), taskIDs[0]) + }) + t.Run("Found No Tasks", func(t *testing.T) { + db.LoadAndAssertFixtures(t) + s := db.NewSession() + defer s.Close() + + now, err := time.Parse(time.RFC3339Nano, "2018-12-02T01:13:00Z") + assert.NoError(t, err) + taskIDs, err := getTasksWithRemindersInTheNextMinute(s, now) + assert.NoError(t, err) + assert.Len(t, taskIDs, 0) + }) +}