vikunja-api/pkg/models/task_readall_test.go

719 lines
16 KiB
Go
Raw Normal View History

2018-12-16 14:21:32 +01:00
/*
* Copyright (c) 2018 the Vikunja Authors. All rights reserved.
* Use of this source code is governed by a LPGLv3-style
* license that can be found in the LICENSE file.
*/
package models
import (
2019-10-16 22:52:29 +02:00
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/files"
2018-12-28 22:49:46 +01:00
"github.com/stretchr/testify/assert"
2019-04-21 20:18:17 +02:00
"gopkg.in/d4l3k/messagediff.v1"
2018-12-16 14:21:32 +01:00
"sort"
"testing"
"code.vikunja.io/web"
)
2019-08-14 22:19:04 +02:00
func sortTasksForTesting(by SortBy) (tasks []*Task) {
2019-08-14 21:59:31 +02:00
user1 := &User{
ID: 1,
Username: "user1",
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
IsActive: true,
AvatarURL: "111d68d06e2d317b5a59c2c6c5bad808", // hash for ""
2019-05-25 09:33:57 +02:00
}
2019-08-14 21:59:31 +02:00
user2 := &User{
ID: 2,
Username: "user2",
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
AvatarURL: "ab53a2911ddf9b4817ac01ddcd3d975f", // hash for ""
2019-05-25 09:33:57 +02:00
}
2019-08-14 21:59:31 +02:00
user6 := &User{
ID: 6,
Username: "user6",
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
IsActive: true,
AvatarURL: "3efbe51f864c6666bc27caf4c6ff90ed", // hash for ""
2019-05-25 09:33:57 +02:00
}
2019-08-14 22:19:04 +02:00
tasks = []*Task{
2018-12-16 14:21:32 +01:00
{
ID: 1,
Text: "task #1",
2019-04-21 20:18:17 +02:00
Description: "Lorem Ipsum",
2018-12-16 14:21:32 +01:00
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-16 14:21:32 +01:00
ListID: 1,
2019-05-25 09:33:57 +02:00
Labels: []*Label{
{
ID: 4,
Title: "Label #4 - visible via other task",
CreatedByID: 2,
2019-08-14 21:59:31 +02:00
CreatedBy: user2,
2019-05-25 09:33:57 +02:00
Updated: 0,
Created: 0,
},
},
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{
RelationKindSubtask: {
{
ID: 29,
Text: "task #29 with parent task (1)",
CreatedByID: 1,
ListID: 1,
Created: 1543626724,
Updated: 1543626724,
},
},
},
2019-10-16 22:52:29 +02:00
Attachments: []*TaskAttachment{
{
ID: 1,
TaskID: 1,
FileID: 1,
CreatedByID: 1,
CreatedBy: user1,
File: &files.File{
ID: 1,
Name: "test",
Size: 100,
CreatedUnix: 1570998791,
CreatedByID: 1,
},
},
{
ID: 2,
TaskID: 1,
FileID: 9999,
CreatedByID: 1,
CreatedBy: user1,
},
},
2019-05-25 09:33:57 +02:00
Created: 1543626724,
Updated: 1543626724,
2018-12-16 14:21:32 +01:00
},
{
ID: 2,
Text: "task #2 done",
Done: true,
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-16 14:21:32 +01:00
ListID: 1,
Labels: []*Label{
{
ID: 4,
Title: "Label #4 - visible via other task",
CreatedByID: 2,
CreatedBy: user2,
Updated: 0,
Created: 0,
},
},
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 3,
Text: "task #3 high prio",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
Priority: 100,
},
{
ID: 4,
Text: "task #4 low prio",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
Priority: 1,
},
{
ID: 5,
Text: "task #5 higher due date",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
DueDateUnix: 1543636724,
},
{
ID: 6,
Text: "task #6 lower due date",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
DueDateUnix: 1543616724,
2018-12-16 14:21:32 +01:00
},
2018-12-22 19:06:14 +01:00
{
ID: 7,
Text: "task #7 with start date",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-22 19:06:14 +01:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2018-12-22 19:06:14 +01:00
Created: 1543626724,
Updated: 1543626724,
StartDateUnix: 1544600000,
},
{
2019-09-25 20:44:41 +02:00
ID: 8,
Text: "task #8 with end date",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
EndDateUnix: 1544700000,
2018-12-22 19:06:14 +01:00
},
{
ID: 9,
Text: "task #9 with start and end date",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-22 19:06:14 +01:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2018-12-22 19:06:14 +01:00
Created: 1543626724,
Updated: 1543626724,
StartDateUnix: 1544600000,
EndDateUnix: 1544700000,
},
2018-12-28 22:49:46 +01:00
{
2019-09-25 20:44:41 +02:00
ID: 10,
Text: "task #10 basic",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 11,
Text: "task #11 basic",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 12,
Text: "task #12 basic",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 15,
Text: "task #15",
CreatedByID: 6,
CreatedBy: user6,
ListID: 6,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 16,
Text: "task #16",
CreatedByID: 6,
CreatedBy: user6,
ListID: 7,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 17,
Text: "task #17",
CreatedByID: 6,
CreatedBy: user6,
ListID: 8,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 18,
Text: "task #18",
CreatedByID: 6,
CreatedBy: user6,
ListID: 9,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 19,
Text: "task #19",
CreatedByID: 6,
CreatedBy: user6,
ListID: 10,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 20,
Text: "task #20",
CreatedByID: 6,
CreatedBy: user6,
ListID: 11,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 21,
Text: "task #21",
CreatedByID: 6,
CreatedBy: user6,
ListID: 12,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 22,
Text: "task #22",
CreatedByID: 6,
CreatedBy: user6,
ListID: 13,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 23,
Text: "task #23",
CreatedByID: 6,
CreatedBy: user6,
ListID: 14,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 24,
Text: "task #24",
CreatedByID: 6,
CreatedBy: user6,
ListID: 15,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 25,
Text: "task #25",
CreatedByID: 6,
CreatedBy: user6,
ListID: 16,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 26,
Text: "task #26",
CreatedByID: 6,
CreatedBy: user6,
ListID: 17,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
2019-04-21 20:18:17 +02:00
},
{
ID: 27,
Text: "task #27 with reminders",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2019-04-21 20:18:17 +02:00
RemindersUnix: []int64{1543626724, 1543626824},
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2019-04-21 20:18:17 +02:00
Created: 1543626724,
Updated: 1543626724,
},
{
2019-09-25 20:44:41 +02:00
ID: 28,
Text: "task #28 with repeat after",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
RepeatAfter: 3600,
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 29,
Text: "task #29 with parent task (1)",
2019-04-21 20:18:17 +02:00
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2019-04-21 20:18:17 +02:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{
RelationKindParenttask: {
{
ID: 1,
Text: "task #1",
Description: "Lorem Ipsum",
CreatedByID: 1,
ListID: 1,
Created: 1543626724,
Updated: 1543626724,
},
},
},
Created: 1543626724,
Updated: 1543626724,
2019-04-21 20:18:17 +02:00
},
{
ID: 30,
Text: "task #30 with assignees",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2019-04-21 20:18:17 +02:00
ListID: 1,
2019-05-25 09:33:57 +02:00
Assignees: []*User{
2019-08-14 21:59:31 +02:00
user1,
user2,
2019-05-25 09:33:57 +02:00
},
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 31,
Text: "task #31 with color",
HexColor: "f0f0f0",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 32,
Text: "task #32",
CreatedByID: 1,
CreatedBy: user1,
ListID: 3,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
},
{
ID: 33,
Text: "task #33 with percent done",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
PercentDone: 0.5,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
2019-04-30 20:36:39 +02:00
},
2018-12-16 14:21:32 +01:00
}
switch by {
case SortTasksByPriorityDesc:
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].Priority > tasks[j].Priority
})
case SortTasksByPriorityAsc:
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].Priority < tasks[j].Priority
})
case SortTasksByDueDateDesc:
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].DueDateUnix > tasks[j].DueDateUnix
})
case SortTasksByDueDateAsc:
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].DueDateUnix < tasks[j].DueDateUnix
})
}
return
}
2019-08-14 22:19:04 +02:00
func TestTask_ReadAll(t *testing.T) {
2019-10-16 22:52:29 +02:00
assert.NoError(t, db.LoadFixtures())
2019-05-25 09:33:57 +02:00
// Dummy users
2019-08-14 21:59:31 +02:00
user1 := &User{
ID: 1,
Username: "user1",
Password: "$2a$14$dcadBoMBL9jQoOcZK8Fju.cy0Ptx2oZECkKLnaa8ekRoTFe1w7To.",
IsActive: true,
AvatarURL: "111d68d06e2d317b5a59c2c6c5bad808", // hash for ""
2019-05-25 09:33:57 +02:00
}
2018-12-16 14:21:32 +01:00
type fields struct {
2018-12-22 19:06:14 +01:00
ListID int64
Sorting string
StartDateSortUnix int64
EndDateSortUnix int64
2019-12-01 14:38:11 +01:00
Lists []*List
2018-12-22 19:06:14 +01:00
CRUDable web.CRUDable
Rights web.Rights
2018-12-16 14:21:32 +01:00
}
type args struct {
search string
a web.Auth
page int
}
tests := []struct {
name string
fields fields
args args
want interface{}
wantErr bool
}{
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks normally",
2018-12-16 14:21:32 +01:00
fields: fields{},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
want: sortTasksForTesting(SortTasksByUnsorted),
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks sorted by priority (desc)",
2018-12-16 14:21:32 +01:00
fields: fields{
Sorting: "priority",
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
want: sortTasksForTesting(SortTasksByPriorityDesc),
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks sorted by priority asc",
2018-12-16 14:21:32 +01:00
fields: fields{
Sorting: "priorityasc",
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-05-25 09:33:57 +02:00
want: sortTasksForTesting(SortTasksByPriorityAsc),
2018-12-16 14:21:32 +01:00
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks sorted by priority desc",
2018-12-16 14:21:32 +01:00
fields: fields{
Sorting: "prioritydesc",
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
want: sortTasksForTesting(SortTasksByPriorityDesc),
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks sorted by due date default desc",
2018-12-16 14:21:32 +01:00
fields: fields{
2019-05-31 10:10:56 +02:00
Sorting: "duedate",
2018-12-16 14:21:32 +01:00
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-05-25 09:33:57 +02:00
want: sortTasksForTesting(SortTasksByDueDateDesc),
2018-12-16 14:21:32 +01:00
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks sorted by due date asc",
2018-12-16 14:21:32 +01:00
fields: fields{
Sorting: "duedateasc",
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-05-25 09:33:57 +02:00
want: sortTasksForTesting(SortTasksByDueDateAsc),
2018-12-16 14:21:32 +01:00
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks sorted by due date desc",
2018-12-16 14:21:32 +01:00
fields: fields{
2019-05-31 10:10:56 +02:00
Sorting: "duedatedesc",
2018-12-16 14:21:32 +01:00
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-04-21 20:18:17 +02:00
2018-12-16 14:21:32 +01:00
want: sortTasksForTesting(SortTasksByDueDateDesc),
wantErr: false,
},
2018-12-22 19:06:14 +01:00
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks with range",
2018-12-22 19:06:14 +01:00
fields: fields{
StartDateSortUnix: 1544500000,
EndDateSortUnix: 1544600000,
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-08-14 22:19:04 +02:00
want: []*Task{
2018-12-22 19:06:14 +01:00
{
ID: 7,
Text: "task #7 with start date",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-22 19:06:14 +01:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2018-12-22 19:06:14 +01:00
Created: 1543626724,
Updated: 1543626724,
StartDateUnix: 1544600000,
},
{
ID: 9,
Text: "task #9 with start and end date",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-22 19:06:14 +01:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2018-12-22 19:06:14 +01:00
Created: 1543626724,
Updated: 1543626724,
StartDateUnix: 1544600000,
EndDateUnix: 1544700000,
},
},
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks with range",
2018-12-22 19:06:14 +01:00
fields: fields{
StartDateSortUnix: 1544700000,
EndDateSortUnix: 1545000000,
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-08-14 22:19:04 +02:00
want: []*Task{
2018-12-22 19:06:14 +01:00
{
2019-09-25 20:44:41 +02:00
ID: 8,
Text: "task #8 with end date",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
EndDateUnix: 1544700000,
2018-12-22 19:06:14 +01:00
},
{
ID: 9,
Text: "task #9 with start and end date",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-22 19:06:14 +01:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2018-12-22 19:06:14 +01:00
Created: 1543626724,
Updated: 1543626724,
StartDateUnix: 1544600000,
EndDateUnix: 1544700000,
},
},
wantErr: false,
},
{
2019-08-14 22:19:04 +02:00
name: "ReadAll Tasks with range without end date",
2018-12-22 19:06:14 +01:00
fields: fields{
StartDateSortUnix: 1544700000,
},
args: args{
search: "",
a: &User{ID: 1},
page: 0,
},
2019-08-14 22:19:04 +02:00
want: []*Task{
2018-12-22 19:06:14 +01:00
{
2019-09-25 20:44:41 +02:00
ID: 8,
Text: "task #8 with end date",
CreatedByID: 1,
CreatedBy: user1,
ListID: 1,
RelatedTasks: map[RelationKind][]*Task{},
Created: 1543626724,
Updated: 1543626724,
EndDateUnix: 1544700000,
2018-12-22 19:06:14 +01:00
},
{
ID: 9,
Text: "task #9 with start and end date",
CreatedByID: 1,
2019-05-25 09:33:57 +02:00
CreatedBy: user1,
2018-12-22 19:06:14 +01:00
ListID: 1,
2019-09-25 20:44:41 +02:00
RelatedTasks: map[RelationKind][]*Task{},
2018-12-22 19:06:14 +01:00
Created: 1543626724,
Updated: 1543626724,
StartDateUnix: 1544600000,
EndDateUnix: 1544700000,
},
},
wantErr: false,
},
2018-12-16 14:21:32 +01:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2019-12-01 14:38:11 +01:00
lt := &TaskCollection{
2018-12-22 19:06:14 +01:00
ListID: tt.fields.ListID,
Sorting: tt.fields.Sorting,
StartDateSortUnix: tt.fields.StartDateSortUnix,
EndDateSortUnix: tt.fields.EndDateSortUnix,
CRUDable: tt.fields.CRUDable,
Rights: tt.fields.Rights,
2018-12-16 14:21:32 +01:00
}
2019-10-23 23:11:40 +02:00
got, _, _, err := lt.ReadAll(tt.args.a, tt.args.search, tt.args.page, 50)
2018-12-16 14:21:32 +01:00
if (err != nil) != tt.wantErr {
2019-08-14 22:19:04 +02:00
t.Errorf("Test %s, Task.ReadAll() error = %v, wantErr %v", tt.name, err, tt.wantErr)
2018-12-16 14:21:32 +01:00
return
}
2019-04-21 20:18:17 +02:00
if diff, equal := messagediff.PrettyDiff(got, tt.want); !equal {
2019-05-25 09:33:57 +02:00
t.Errorf("Test %s, LabelTask.ReadAll() = %v, want %v, \ndiff: %v", tt.name, got, tt.want, diff)
2018-12-16 14:21:32 +01:00
}
})
}
}