Make sure to not make the task done if there is no repeat after duration

This commit is contained in:
kolaente 2021-04-13 22:39:50 +02:00
parent ab98351c68
commit 4e876f2686
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 40 additions and 4 deletions

View File

@ -65,9 +65,7 @@ type Task struct {
ListID int64 `xorm:"bigint INDEX not null" json:"list_id" param:"list"` ListID int64 `xorm:"bigint INDEX not null" json:"list_id" param:"list"`
// An amount in seconds this task repeats itself. If this is set, when marking the task as done, it will mark itself as "undone" and then increase all remindes and the due date by its amount. // An amount in seconds this task repeats itself. If this is set, when marking the task as done, it will mark itself as "undone" and then increase all remindes and the due date by its amount.
RepeatAfter int64 `xorm:"bigint INDEX null" json:"repeat_after"` RepeatAfter int64 `xorm:"bigint INDEX null" json:"repeat_after"`
// If specified, a repeating task will repeat from the current date rather than the last set date. // Can have three possible values which will trigger when the task is marked as done: 0 = repeats after the amount specified in repeat_after, 1 = repeats all dates each months (ignoring repeat_after), 3 = repeats from the current date rather than the last set date.
//RepeatFromCurrentDate bool `xorm:"null" json:"repeat_from_current_date"`
RepeatMode TaskRepeatMode `xorm:"not null default 0" json:"repeat_mode"` RepeatMode TaskRepeatMode `xorm:"not null default 0" json:"repeat_mode"`
// The task priority. Can be anything you want, it is possible to sort by this later. // The task priority. Can be anything you want, it is possible to sort by this later.
Priority int64 `xorm:"bigint null" json:"priority"` Priority int64 `xorm:"bigint null" json:"priority"`
@ -1132,6 +1130,8 @@ func setTaskDatesDefault(oldTask, newTask *Task) {
newTask.EndDate = newTask.EndDate.Add(repeatDuration) newTask.EndDate = newTask.EndDate.Add(repeatDuration)
} }
} }
newTask.Done = false
} }
func setTaskDatesMonthRepeat(oldTask, newTask *Task) { func setTaskDatesMonthRepeat(oldTask, newTask *Task) {
@ -1159,6 +1159,8 @@ func setTaskDatesMonthRepeat(oldTask, newTask *Task) {
newTask.EndDate = addOneMonthToDate(oldTask.EndDate) newTask.EndDate = addOneMonthToDate(oldTask.EndDate)
} }
} }
newTask.Done = false
} }
func setTaskDatesFromCurrentDateRepeat(oldTask, newTask *Task) { func setTaskDatesFromCurrentDateRepeat(oldTask, newTask *Task) {
@ -1204,6 +1206,8 @@ func setTaskDatesFromCurrentDateRepeat(oldTask, newTask *Task) {
newTask.EndDate = now.Add(repeatDuration) newTask.EndDate = now.Add(repeatDuration)
} }
} }
newTask.Done = false
} }
// This helper function updates the reminders, doneAt, start and end dates of the *old* task // This helper function updates the reminders, doneAt, start and end dates of the *old* task
@ -1223,7 +1227,6 @@ func updateDone(oldTask *Task, newTask *Task) {
} }
newTask.DoneAt = time.Now() newTask.DoneAt = time.Now()
newTask.Done = false
} }
// When unmarking a task as done, reset the timestamp // When unmarking a task as done, reset the timestamp

View File

@ -345,6 +345,23 @@ func TestUpdateDone(t *testing.T) {
updateDone(oldTask, newTask) updateDone(oldTask, newTask)
assert.Equal(t, time.Time{}, newTask.DoneAt) assert.Equal(t, time.Time{}, newTask.DoneAt)
}) })
t.Run("no interval set, default repeat mode", func(t *testing.T) {
dueDate := time.Unix(1550000000, 0)
oldTask := &Task{
Done: false,
RepeatAfter: 0,
RepeatMode: TaskRepeatModeDefault,
DueDate: dueDate,
}
newTask := &Task{
Done: true,
DueDate: dueDate,
}
updateDone(oldTask, newTask)
assert.Equal(t, dueDate.Unix(), newTask.DueDate.Unix())
assert.True(t, newTask.Done)
})
t.Run("repeating interval", func(t *testing.T) { t.Run("repeating interval", func(t *testing.T) {
t.Run("normal", func(t *testing.T) { t.Run("normal", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -363,6 +380,7 @@ func TestUpdateDone(t *testing.T) {
} }
assert.Equal(t, expected, newTask.DueDate) assert.Equal(t, expected, newTask.DueDate)
assert.False(t, newTask.Done)
}) })
t.Run("don't update if due date is zero", func(t *testing.T) { t.Run("don't update if due date is zero", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -376,6 +394,7 @@ func TestUpdateDone(t *testing.T) {
} }
updateDone(oldTask, newTask) updateDone(oldTask, newTask)
assert.Equal(t, time.Unix(1543626724, 0), newTask.DueDate) assert.Equal(t, time.Unix(1543626724, 0), newTask.DueDate)
assert.False(t, newTask.Done)
}) })
t.Run("update reminders", func(t *testing.T) { t.Run("update reminders", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -403,6 +422,7 @@ func TestUpdateDone(t *testing.T) {
assert.Len(t, newTask.Reminders, 2) assert.Len(t, newTask.Reminders, 2)
assert.Equal(t, expected1, newTask.Reminders[0]) assert.Equal(t, expected1, newTask.Reminders[0])
assert.Equal(t, expected2, newTask.Reminders[1]) assert.Equal(t, expected2, newTask.Reminders[1])
assert.False(t, newTask.Done)
}) })
t.Run("update start date", func(t *testing.T) { t.Run("update start date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -421,6 +441,7 @@ func TestUpdateDone(t *testing.T) {
} }
assert.Equal(t, expected, newTask.StartDate) assert.Equal(t, expected, newTask.StartDate)
assert.False(t, newTask.Done)
}) })
t.Run("update end date", func(t *testing.T) { t.Run("update end date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -439,6 +460,7 @@ func TestUpdateDone(t *testing.T) {
} }
assert.Equal(t, expected, newTask.EndDate) assert.Equal(t, expected, newTask.EndDate)
assert.False(t, newTask.Done)
}) })
t.Run("ensure due date is repeated even if the original one is in the future", func(t *testing.T) { t.Run("ensure due date is repeated even if the original one is in the future", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -452,6 +474,7 @@ func TestUpdateDone(t *testing.T) {
updateDone(oldTask, newTask) updateDone(oldTask, newTask)
expected := oldTask.DueDate.Add(time.Duration(oldTask.RepeatAfter) * time.Second) expected := oldTask.DueDate.Add(time.Duration(oldTask.RepeatAfter) * time.Second)
assert.Equal(t, expected, newTask.DueDate) assert.Equal(t, expected, newTask.DueDate)
assert.False(t, newTask.Done)
}) })
t.Run("repeat from current date", func(t *testing.T) { t.Run("repeat from current date", func(t *testing.T) {
t.Run("due date", func(t *testing.T) { t.Run("due date", func(t *testing.T) {
@ -468,6 +491,7 @@ func TestUpdateDone(t *testing.T) {
// Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value // Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value
assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.DueDate.Unix()) assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.DueDate.Unix())
assert.False(t, newTask.Done)
}) })
t.Run("reminders", func(t *testing.T) { t.Run("reminders", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -490,6 +514,7 @@ func TestUpdateDone(t *testing.T) {
// Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value // Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value
assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.Reminders[0].Unix()) assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.Reminders[0].Unix())
assert.Equal(t, time.Now().Add(diff+time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.Reminders[1].Unix()) assert.Equal(t, time.Now().Add(diff+time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.Reminders[1].Unix())
assert.False(t, newTask.Done)
}) })
t.Run("start date", func(t *testing.T) { t.Run("start date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -505,6 +530,7 @@ func TestUpdateDone(t *testing.T) {
// Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value // Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value
assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.StartDate.Unix()) assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.StartDate.Unix())
assert.False(t, newTask.Done)
}) })
t.Run("end date", func(t *testing.T) { t.Run("end date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -520,6 +546,7 @@ func TestUpdateDone(t *testing.T) {
// Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value // Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value
assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.EndDate.Unix()) assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.EndDate.Unix())
assert.False(t, newTask.Done)
}) })
t.Run("start and end date", func(t *testing.T) { t.Run("start and end date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -539,6 +566,7 @@ func TestUpdateDone(t *testing.T) {
// Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value // Only comparing unix timestamps because time.Time use nanoseconds which can't ever possibly have the same value
assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.StartDate.Unix()) assert.Equal(t, time.Now().Add(time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.StartDate.Unix())
assert.Equal(t, time.Now().Add(diff+time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.EndDate.Unix()) assert.Equal(t, time.Now().Add(diff+time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.EndDate.Unix())
assert.False(t, newTask.Done)
}) })
}) })
t.Run("repeat each month", func(t *testing.T) { t.Run("repeat each month", func(t *testing.T) {
@ -557,6 +585,7 @@ func TestUpdateDone(t *testing.T) {
assert.True(t, newTask.DueDate.After(oldDueDate)) assert.True(t, newTask.DueDate.After(oldDueDate))
assert.NotEqual(t, oldDueDate.Month(), newTask.DueDate.Month()) assert.NotEqual(t, oldDueDate.Month(), newTask.DueDate.Month())
assert.False(t, newTask.Done)
}) })
t.Run("reminders", func(t *testing.T) { t.Run("reminders", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -580,6 +609,7 @@ func TestUpdateDone(t *testing.T) {
assert.True(t, r.After(oldReminders[i])) assert.True(t, r.After(oldReminders[i]))
assert.NotEqual(t, oldReminders[i].Month(), r.Month()) assert.NotEqual(t, oldReminders[i].Month(), r.Month())
} }
assert.False(t, newTask.Done)
}) })
t.Run("start date", func(t *testing.T) { t.Run("start date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -596,6 +626,7 @@ func TestUpdateDone(t *testing.T) {
assert.True(t, newTask.StartDate.After(oldStartDate)) assert.True(t, newTask.StartDate.After(oldStartDate))
assert.NotEqual(t, oldStartDate.Month(), newTask.StartDate.Month()) assert.NotEqual(t, oldStartDate.Month(), newTask.StartDate.Month())
assert.False(t, newTask.Done)
}) })
t.Run("end date", func(t *testing.T) { t.Run("end date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -612,6 +643,7 @@ func TestUpdateDone(t *testing.T) {
assert.True(t, newTask.EndDate.After(oldEndDate)) assert.True(t, newTask.EndDate.After(oldEndDate))
assert.NotEqual(t, oldEndDate.Month(), newTask.EndDate.Month()) assert.NotEqual(t, oldEndDate.Month(), newTask.EndDate.Month())
assert.False(t, newTask.Done)
}) })
t.Run("start and end date", func(t *testing.T) { t.Run("start and end date", func(t *testing.T) {
oldTask := &Task{ oldTask := &Task{
@ -634,6 +666,7 @@ func TestUpdateDone(t *testing.T) {
assert.True(t, newTask.EndDate.After(oldEndDate)) assert.True(t, newTask.EndDate.After(oldEndDate))
assert.NotEqual(t, oldEndDate.Month(), newTask.EndDate.Month()) assert.NotEqual(t, oldEndDate.Month(), newTask.EndDate.Month())
assert.Equal(t, oldDiff, newTask.EndDate.Sub(newTask.StartDate)) assert.Equal(t, oldDiff, newTask.EndDate.Sub(newTask.StartDate))
assert.False(t, newTask.Done)
}) })
}) })
}) })