2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-07-27 14:47:52 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2020-10-11 22:10:03 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-02 23:48:37 +01:00
|
|
|
"code.vikunja.io/api/pkg/events"
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
2018-07-27 14:47:52 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-08-14 22:19:04 +02:00
|
|
|
func TestTask_Create(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
usr := &user.User{
|
2019-12-08 16:10:34 +01:00
|
|
|
ID: 1,
|
|
|
|
Username: "user1",
|
|
|
|
Email: "user1@example.com",
|
2018-07-27 14:47:52 +02:00
|
|
|
}
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
// We only test creating a task here, the rights are all well tested in the integration tests.
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Lorem",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := task.Create(s, usr)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// Assert getting a uid
|
|
|
|
assert.NotEmpty(t, task.UID)
|
|
|
|
// Assert getting a new index
|
|
|
|
assert.NotEmpty(t, task.Index)
|
|
|
|
assert.Equal(t, int64(18), task.Index)
|
2020-04-25 22:32:02 +02:00
|
|
|
// Assert moving it into the default bucket
|
|
|
|
assert.Equal(t, int64(1), task.BucketID)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "tasks", map[string]interface{}{
|
|
|
|
"id": task.ID,
|
|
|
|
"title": "Lorem",
|
|
|
|
"description": "Lorem Ipsum Dolor",
|
|
|
|
"list_id": 1,
|
|
|
|
"created_by_id": 1,
|
|
|
|
"bucket_id": 1,
|
|
|
|
}, false)
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2021-02-02 23:48:37 +01:00
|
|
|
events.AssertDispatched(t, &TaskCreatedEvent{})
|
2019-12-08 16:10:34 +01:00
|
|
|
})
|
2020-05-16 12:17:44 +02:00
|
|
|
t.Run("empty title", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := task.Create(s, usr)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskCannotBeEmpty(err))
|
|
|
|
})
|
|
|
|
t.Run("nonexistant list", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Test",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 9999999,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := task.Create(s, usr)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrListDoesNotExist(err))
|
|
|
|
})
|
|
|
|
t.Run("noneixtant user", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-01-26 18:08:06 +01:00
|
|
|
nUser := &user.User{ID: 99999999}
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "Test",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := task.Create(s, nUser)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.Error(t, err)
|
2020-01-26 18:08:06 +01:00
|
|
|
assert.True(t, user.IsErrUserDoesNotExist(err))
|
2019-12-08 16:10:34 +01:00
|
|
|
})
|
2020-09-04 16:37:56 +02:00
|
|
|
t.Run("full bucket", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-09-04 16:37:56 +02:00
|
|
|
task := &Task{
|
|
|
|
Title: "Lorem",
|
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
BucketID: 2, // Bucket 2 already has 3 tasks and a limit of 3
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err := task.Create(s, usr)
|
2020-09-04 16:37:56 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrBucketLimitExceeded(err))
|
|
|
|
})
|
2019-12-08 16:10:34 +01:00
|
|
|
}
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
func TestTask_Update(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "test10000",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.Update(s, u)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertExists(t, "tasks", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"title": "test10000",
|
|
|
|
"description": "Lorem Ipsum Dolor",
|
|
|
|
"list_id": 1,
|
|
|
|
}, false)
|
2019-12-08 16:10:34 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexistant task", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
|
|
|
ID: 9999999,
|
2020-05-16 12:17:44 +02:00
|
|
|
Title: "test10000",
|
2019-12-08 16:10:34 +01:00
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.Update(s, u)
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
})
|
2020-09-04 16:37:56 +02:00
|
|
|
t.Run("full bucket", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-09-04 16:37:56 +02:00
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
Title: "test10000",
|
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
BucketID: 2, // Bucket 2 already has 3 tasks and a limit of 3
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.Update(s, u)
|
2020-09-04 16:37:56 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrBucketLimitExceeded(err))
|
|
|
|
})
|
2020-09-04 22:01:33 +02:00
|
|
|
t.Run("full bucket but not changing the bucket", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2020-09-04 22:01:33 +02:00
|
|
|
task := &Task{
|
|
|
|
ID: 4,
|
|
|
|
Title: "test10000",
|
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
Position: 10,
|
|
|
|
ListID: 1,
|
|
|
|
BucketID: 2, // Bucket 2 already has 3 tasks and a limit of 3
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.Update(s, u)
|
2020-09-04 22:01:33 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2021-03-24 21:16:35 +01:00
|
|
|
t.Run("bucket on other list", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
Title: "test10000",
|
|
|
|
Description: "Lorem Ipsum Dolor",
|
|
|
|
ListID: 1,
|
|
|
|
BucketID: 4, // Bucket 4 belongs to list 2
|
|
|
|
}
|
|
|
|
err := task.Update(s, u)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrBucketDoesNotBelongToList(err))
|
|
|
|
})
|
|
|
|
t.Run("moving a task to the done bucket", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
Title: "test",
|
|
|
|
ListID: 1,
|
|
|
|
BucketID: 3, // Bucket 3 is the done bucket
|
|
|
|
}
|
|
|
|
err := task.Update(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, task.Done)
|
|
|
|
|
|
|
|
db.AssertExists(t, "tasks", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"done": true,
|
|
|
|
"title": "test",
|
|
|
|
"list_id": 1,
|
|
|
|
"bucket_id": 3,
|
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
t.Run("default bucket when moving a task between lists", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
ListID: 2,
|
|
|
|
}
|
|
|
|
err := task.Update(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(4), task.BucketID) // bucket 4 is the default bucket on list 2
|
|
|
|
assert.True(t, task.Done) // bucket 4 is the done bucket, so the task should be marked as done as well
|
|
|
|
})
|
|
|
|
t.Run("marking a task as done should move it to the done bucket", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
err := task.Update(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, task.Done)
|
|
|
|
assert.Equal(t, int64(3), task.BucketID)
|
|
|
|
|
|
|
|
db.AssertExists(t, "tasks", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"done": true,
|
|
|
|
"bucket_id": 3,
|
|
|
|
}, false)
|
|
|
|
})
|
2021-03-24 22:47:03 +01:00
|
|
|
t.Run("move task to another list", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
ListID: 2,
|
|
|
|
}
|
|
|
|
err := task.Update(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
db.AssertExists(t, "tasks", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
"list_id": 2,
|
|
|
|
"bucket_id": 4,
|
|
|
|
}, false)
|
|
|
|
})
|
2019-12-08 16:10:34 +01:00
|
|
|
}
|
2018-07-27 14:47:52 +02:00
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
func TestTask_Delete(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-12-08 16:10:34 +01:00
|
|
|
task := &Task{
|
|
|
|
ID: 1,
|
|
|
|
}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.Delete(s, &user.User{ID: 1})
|
2020-12-23 16:32:28 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = s.Commit()
|
2019-12-08 16:10:34 +01:00
|
|
|
assert.NoError(t, err)
|
2020-12-23 16:32:28 +01:00
|
|
|
|
2020-09-27 17:45:17 +02:00
|
|
|
db.AssertMissing(t, "tasks", map[string]interface{}{
|
|
|
|
"id": 1,
|
|
|
|
})
|
2019-12-08 16:10:34 +01:00
|
|
|
})
|
2018-07-27 14:47:52 +02:00
|
|
|
}
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
func TestUpdateDone(t *testing.T) {
|
|
|
|
t.Run("marking a task as done", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-08-14 22:19:04 +02:00
|
|
|
oldTask := &Task{Done: false}
|
|
|
|
newTask := &Task{Done: true}
|
2019-05-22 19:48:48 +02:00
|
|
|
updateDone(oldTask, newTask)
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.NotEqual(t, time.Time{}, newTask.DoneAt)
|
2019-05-22 19:48:48 +02:00
|
|
|
})
|
|
|
|
t.Run("unmarking a task as done", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-08-14 22:19:04 +02:00
|
|
|
oldTask := &Task{Done: true}
|
|
|
|
newTask := &Task{Done: false}
|
2019-05-22 19:48:48 +02:00
|
|
|
updateDone(oldTask, newTask)
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.Equal(t, time.Time{}, newTask.DoneAt)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("repeating interval", func(t *testing.T) {
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
2020-06-27 19:04:01 +02:00
|
|
|
DueDate: time.Unix(1550000000, 0),
|
2020-02-26 22:09:45 +01:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
var expected = time.Unix(1550008600, 0)
|
|
|
|
for time.Since(expected) > 0 {
|
|
|
|
expected = expected.Add(time.Second * time.Duration(oldTask.RepeatAfter))
|
2020-06-13 23:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.Equal(t, expected, newTask.DueDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("don't update if due date is zero", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
2020-06-27 19:04:01 +02:00
|
|
|
DueDate: time.Time{},
|
2020-02-26 22:09:45 +01:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
2020-06-27 19:04:01 +02:00
|
|
|
DueDate: time.Unix(1543626724, 0),
|
2020-02-26 22:09:45 +01:00
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.Equal(t, time.Unix(1543626724, 0), newTask.DueDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("update reminders", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
2020-06-27 19:04:01 +02:00
|
|
|
Reminders: []time.Time{
|
|
|
|
time.Unix(1550000000, 0),
|
|
|
|
time.Unix(1555000000, 0),
|
2020-02-26 22:09:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
var expected1 = time.Unix(1550008600, 0)
|
|
|
|
var expected2 = time.Unix(1555008600, 0)
|
|
|
|
for time.Since(expected1) > 0 {
|
|
|
|
expected1 = expected1.Add(time.Duration(oldTask.RepeatAfter) * time.Second)
|
2020-06-13 23:32:31 +02:00
|
|
|
}
|
2020-06-27 19:04:01 +02:00
|
|
|
for time.Since(expected2) > 0 {
|
|
|
|
expected2 = expected2.Add(time.Duration(oldTask.RepeatAfter) * time.Second)
|
2020-06-13 23:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:09:45 +01:00
|
|
|
assert.Len(t, newTask.Reminders, 2)
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.Equal(t, expected1, newTask.Reminders[0])
|
|
|
|
assert.Equal(t, expected2, newTask.Reminders[1])
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("update start date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
2020-06-27 19:04:01 +02:00
|
|
|
StartDate: time.Unix(1550000000, 0),
|
2020-02-26 22:09:45 +01:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
var expected = time.Unix(1550008600, 0)
|
|
|
|
for time.Since(expected) > 0 {
|
|
|
|
expected = expected.Add(time.Second * time.Duration(oldTask.RepeatAfter))
|
2020-06-13 23:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.Equal(t, expected, newTask.StartDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
|
|
|
t.Run("update end date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
2020-06-27 19:04:01 +02:00
|
|
|
EndDate: time.Unix(1550000000, 0),
|
2020-02-26 22:09:45 +01:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-13 23:32:31 +02:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
var expected = time.Unix(1550008600, 0)
|
|
|
|
for time.Since(expected) > 0 {
|
|
|
|
expected = expected.Add(time.Second * time.Duration(oldTask.RepeatAfter))
|
2020-06-13 23:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
assert.Equal(t, expected, newTask.EndDate)
|
2020-06-13 23:32:31 +02:00
|
|
|
})
|
|
|
|
t.Run("ensure due date is repeated even if the original one is in the future", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
2020-06-27 19:04:01 +02:00
|
|
|
DueDate: time.Now().Add(time.Hour),
|
2020-06-13 23:32:31 +02:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
2020-06-27 19:04:01 +02:00
|
|
|
expected := oldTask.DueDate.Add(time.Duration(oldTask.RepeatAfter) * time.Second)
|
|
|
|
assert.Equal(t, expected, newTask.DueDate)
|
2020-02-26 22:09:45 +01:00
|
|
|
})
|
2020-06-14 13:04:38 +02:00
|
|
|
t.Run("repeat from current date", func(t *testing.T) {
|
|
|
|
t.Run("due date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
RepeatFromCurrentDate: true,
|
2020-06-27 19:04:01 +02:00
|
|
|
DueDate: time.Unix(1550000000, 0),
|
2020-06-14 13:04:38 +02:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
// 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())
|
2020-06-14 13:04:38 +02:00
|
|
|
})
|
|
|
|
t.Run("reminders", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
RepeatFromCurrentDate: true,
|
2020-06-27 19:04:01 +02:00
|
|
|
Reminders: []time.Time{
|
|
|
|
time.Unix(1550000000, 0),
|
|
|
|
time.Unix(1555000000, 0),
|
2020-06-14 13:04:38 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
diff := oldTask.Reminders[1].Sub(oldTask.Reminders[0])
|
2020-06-14 13:04:38 +02:00
|
|
|
|
|
|
|
assert.Len(t, newTask.Reminders, 2)
|
2020-06-27 19:04:01 +02:00
|
|
|
// 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(diff+time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.Reminders[1].Unix())
|
2020-06-14 13:04:38 +02:00
|
|
|
})
|
|
|
|
t.Run("start date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
RepeatFromCurrentDate: true,
|
2020-06-27 19:04:01 +02:00
|
|
|
StartDate: time.Unix(1550000000, 0),
|
2020-06-14 13:04:38 +02:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
// 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())
|
2020-06-14 13:04:38 +02:00
|
|
|
})
|
|
|
|
t.Run("end date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
RepeatFromCurrentDate: true,
|
2020-06-27 19:04:01 +02:00
|
|
|
EndDate: time.Unix(1560000000, 0),
|
2020-06-14 13:04:38 +02:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
// 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())
|
2020-06-14 13:04:38 +02:00
|
|
|
})
|
|
|
|
t.Run("start and end date", func(t *testing.T) {
|
|
|
|
oldTask := &Task{
|
|
|
|
Done: false,
|
|
|
|
RepeatAfter: 8600,
|
|
|
|
RepeatFromCurrentDate: true,
|
2020-06-27 19:04:01 +02:00
|
|
|
StartDate: time.Unix(1550000000, 0),
|
|
|
|
EndDate: time.Unix(1560000000, 0),
|
2020-06-14 13:04:38 +02:00
|
|
|
}
|
|
|
|
newTask := &Task{
|
|
|
|
Done: true,
|
|
|
|
}
|
|
|
|
updateDone(oldTask, newTask)
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
diff := oldTask.EndDate.Sub(oldTask.StartDate)
|
2020-06-14 13:04:38 +02:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
// 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(diff+time.Duration(oldTask.RepeatAfter)*time.Second).Unix(), newTask.EndDate.Unix())
|
2020-06-14 13:04:38 +02:00
|
|
|
})
|
|
|
|
})
|
2019-05-22 19:48:48 +02:00
|
|
|
})
|
|
|
|
}
|
2019-11-02 21:33:18 +01:00
|
|
|
|
|
|
|
func TestTask_ReadOne(t *testing.T) {
|
2021-02-02 23:48:37 +01:00
|
|
|
u := &user.User{ID: 1}
|
|
|
|
|
2019-11-02 21:33:18 +01:00
|
|
|
t.Run("default", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-11-02 21:33:18 +01:00
|
|
|
task := &Task{ID: 1}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.ReadOne(s, u)
|
2019-11-02 21:33:18 +01:00
|
|
|
assert.NoError(t, err)
|
2020-05-16 12:17:44 +02:00
|
|
|
assert.Equal(t, "task #1", task.Title)
|
2019-11-02 21:33:18 +01:00
|
|
|
})
|
|
|
|
t.Run("nonexisting", func(t *testing.T) {
|
2020-01-26 18:08:06 +01:00
|
|
|
db.LoadAndAssertFixtures(t)
|
2020-12-23 16:32:28 +01:00
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-11-02 21:33:18 +01:00
|
|
|
task := &Task{ID: 99999}
|
2021-02-02 23:48:37 +01:00
|
|
|
err := task.ReadOne(s, u)
|
2019-11-02 21:33:18 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrTaskDoesNotExist(err))
|
|
|
|
})
|
2021-02-14 20:18:14 +01:00
|
|
|
t.Run("with subscription", func(t *testing.T) {
|
|
|
|
u = &user.User{ID: 6}
|
|
|
|
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{ID: 22}
|
|
|
|
err := task.ReadOne(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, task.Subscription)
|
|
|
|
})
|
2021-04-07 14:44:39 +02:00
|
|
|
t.Run("created by link share", func(t *testing.T) {
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
s := db.NewSession()
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
task := &Task{ID: 37}
|
|
|
|
err := task.ReadOne(s, u)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "task #37", task.Title)
|
|
|
|
assert.Equal(t, int64(-2), task.CreatedByID)
|
|
|
|
assert.NotNil(t, task.CreatedBy)
|
|
|
|
assert.Equal(t, int64(-2), task.CreatedBy.ID)
|
|
|
|
})
|
2019-11-02 21:33:18 +01:00
|
|
|
}
|