vikunja/pkg/models/task_collection_sort_test.go
kolaente 3da710637d
Some checks failed
continuous-integration/drone/pr Build is failing
Move assertions to seperate function
2019-12-04 22:54:34 +01:00

398 lines
7.8 KiB
Go

// Vikunja is a todo-list application to facilitate your life.
// Copyright 2019 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 General Public License 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/assert"
"math/rand"
"reflect"
"testing"
)
func TestSortParamValidation(t *testing.T) {
t.Run("Test valid order by", func(t *testing.T) {
t.Run(orderAscending.String(), func(t *testing.T) {
s := &sortParam{
orderBy: orderAscending,
sortBy: "id",
}
err := s.validate()
assert.NoError(t, err)
})
t.Run(orderDescending.String(), func(t *testing.T) {
s := &sortParam{
orderBy: orderDescending,
sortBy: "id",
}
err := s.validate()
assert.NoError(t, err)
})
})
t.Run("Test valid sort by", func(t *testing.T) {
for _, test := range []sortProperty{
taskPropertyID,
taskPropertyText,
taskPropertyDescription,
taskPropertyDone,
taskPropertyDoneAtUnix,
taskPropertyDueDateUnix,
taskPropertyCreatedByID,
taskPropertyListID,
taskPropertyRepeatAfter,
taskPropertyPriority,
taskPropertyStartDateUnix,
taskPropertyEndDateUnix,
taskPropertyHexColor,
taskPropertyPercentDone,
taskPropertyUID,
taskPropertyCreated,
taskPropertyUpdated,
} {
t.Run(test.String(), func(t *testing.T) {
s := &sortParam{
orderBy: orderAscending,
sortBy: test,
}
err := s.validate()
assert.NoError(t, err)
})
}
})
t.Run("Test invalid order by", func(t *testing.T) {
s := &sortParam{
orderBy: "somethingInvalid",
sortBy: "id",
}
err := s.validate()
assert.Error(t, err)
assert.True(t, IsErrInvalidSortOrder(err))
})
t.Run("Test invalid sort by", func(t *testing.T) {
s := &sortParam{
orderBy: orderAscending,
sortBy: "somethingInvalid",
}
err := s.validate()
assert.Error(t, err)
assert.True(t, IsErrInvalidSortParam(err))
})
}
var (
task1 = &Task{
ID: 1,
Text: "aaa",
Description: "Lorem Ipsum",
Done: true,
DoneAtUnix: 1543626000,
ListID: 1,
UID: "JywtBPCESImlyKugvaZWrxmXAFAWXFISMeXYImEh",
Created: 1543626724,
Updated: 1543626724,
}
task2 = &Task{
ID: 2,
Text: "bbb",
Description: "Arem Ipsum",
Done: true,
DoneAtUnix: 1543626724,
CreatedByID: 1,
ListID: 2,
PercentDone: 0.3,
StartDateUnix: 1543626724,
Created: 1553626724,
Updated: 1553626724,
}
task3 = &Task{
ID: 3,
Text: "ccc",
DueDateUnix: 1583626724,
Priority: 100,
ListID: 3,
HexColor: "000000",
PercentDone: 0.1,
Updated: 1555555555,
}
task4 = &Task{
ID: 4,
Text: "ddd",
Priority: 1,
StartDateUnix: 1643626724,
ListID: 1,
}
task5 = &Task{
ID: 5,
Text: "efg",
Priority: 50,
UID: "shggzCHQWLhGNMNsOGOCOjcVkInOYjTAnORqTkdL",
DueDateUnix: 1543636724,
Updated: 1565555555,
}
task6 = &Task{
ID: 6,
Text: "eef",
DueDateUnix: 1543616724,
RepeatAfter: 6400,
CreatedByID: 2,
HexColor: "ffffff",
}
task7 = &Task{
ID: 7,
Text: "mmmn",
Description: "Zoremis",
StartDateUnix: 1544600000,
EndDateUnix: 1584600000,
UID: "tyzCZuLMSKhwclJOsDyDcUdyVAPBDOPHNTBOLTcW",
}
task8 = &Task{
ID: 8,
Text: "b123",
EndDateUnix: 1544700000,
}
task9 = &Task{
ID: 9,
Done: true,
DoneAtUnix: 1573626724,
Text: "a123",
RepeatAfter: 86000,
StartDateUnix: 1544600000,
EndDateUnix: 1544700000,
}
task10 = &Task{
ID: 10,
Text: "zzz",
Priority: 10,
PercentDone: 1,
}
)
type taskSortTestCase struct {
name string
wantAsc []*Task
wantDesc []*Task
sortProperty sortProperty
}
var taskSortTestCases = []taskSortTestCase{
{
name: "Order by ID",
wantAsc: []*Task{
task1,
task2,
task3,
task4,
task5,
task6,
task7,
task8,
task9,
task10,
},
wantDesc: []*Task{
task10,
task9,
task8,
task7,
task6,
task5,
task4,
task3,
task2,
task1,
},
sortProperty: taskPropertyID,
},
{
name: "Order by Text Ascending",
want: []*Task{
task9,
task1,
task8,
task2,
task3,
task4,
task6,
task5,
task7,
task10,
},
sortParams: []*sortParam{
{
sortBy: taskPropertyText,
orderBy: orderAscending,
},
},
},
{
name: "Order by Text Descending",
want: []*Task{
task10,
task7,
task5,
task6,
task4,
task3,
task2,
task8,
task1,
task9,
},
sortParams: []*sortParam{
{
sortBy: taskPropertyText,
orderBy: orderDescending,
},
},
},
{
name: "Order by Done Ascending and Text Descending",
want: []*Task{
// Done
task2,
task1,
task9,
// Not done
task10,
task7,
task5,
task6,
task4,
task3,
task8,
},
sortParams: []*sortParam{
{
sortBy: taskPropertyDone,
orderBy: orderAscending,
},
{
sortBy: taskPropertyText,
orderBy: orderDescending,
},
},
},
{
name: "Order by Done Descending and Text Ascending",
want: []*Task{
// Not done
task8,
task3,
task4,
task6,
task5,
task7,
task10,
// Done
task9,
task1,
task2,
},
sortParams: []*sortParam{
{
sortBy: taskPropertyDone,
orderBy: orderDescending,
},
{
sortBy: taskPropertyText,
orderBy: orderAscending,
},
},
},
}
func TestTaskSort(t *testing.T) {
assertTestSliceMatch := func(t *testing.T, got, want []*Task) {
if !reflect.DeepEqual(got, want) {
t.Error("Slices do not match in order")
t.Error("Got:")
for _, task := range got {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
t.Error("Want:")
for _, task := range want {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
}
}
for _, testCase := range taskSortTestCases {
t.Run(testCase.name, func(t *testing.T) {
t.Run("asc default", func(t *testing.T) {
by := []*sortParam{
{
sortBy: testCase.sortProperty,
},
}
got := deepcopy.Copy(testCase.wantAsc).([]*Task)
// Destroy wanted order to obtain some slice we can sort
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i]
})
sortTasks(got, by)
assertTestSliceMatch(t, got, testCase.wantAsc)
})
t.Run("asc", func(t *testing.T) {
by := []*sortParam{
{
sortBy: testCase.sortProperty,
orderBy: orderAscending,
},
}
got := deepcopy.Copy(testCase.wantAsc).([]*Task)
// Destroy wanted order to obtain some slice we can sort
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i]
})
sortTasks(got, by)
assertTestSliceMatch(t, got, testCase.wantAsc)
})
t.Run("desc", func(t *testing.T) {
by := []*sortParam{
{
sortBy: testCase.sortProperty,
orderBy: orderDescending,
},
}
got := deepcopy.Copy(testCase.wantAsc).([]*Task)
// Destroy wanted order to obtain some slice we can sort
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i]
})
sortTasks(got, by)
assertTestSliceMatch(t, got, testCase.wantAsc)
})
})
}
}