Change tests to write less
This commit is contained in:
parent
2fca034aad
commit
6dc275fa00
|
@ -182,15 +182,16 @@ var (
|
|||
)
|
||||
|
||||
type taskSortTestCase struct {
|
||||
name string
|
||||
want []*Task
|
||||
sortParams []*sortParam
|
||||
name string
|
||||
wantAsc []*Task
|
||||
wantDesc []*Task
|
||||
sortProperty sortProperty
|
||||
}
|
||||
|
||||
var taskSortTestCases = []taskSortTestCase{
|
||||
{
|
||||
name: "Order by ID Ascending",
|
||||
want: []*Task{
|
||||
name: "Order by ID",
|
||||
wantAsc: []*Task{
|
||||
task1,
|
||||
task2,
|
||||
task3,
|
||||
|
@ -202,16 +203,7 @@ var taskSortTestCases = []taskSortTestCase{
|
|||
task9,
|
||||
task10,
|
||||
},
|
||||
sortParams: []*sortParam{
|
||||
{
|
||||
sortBy: taskPropertyID,
|
||||
orderBy: orderAscending,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Order by ID Descending",
|
||||
want: []*Task{
|
||||
wantDesc: []*Task{
|
||||
task10,
|
||||
task9,
|
||||
task8,
|
||||
|
@ -223,12 +215,7 @@ var taskSortTestCases = []taskSortTestCase{
|
|||
task2,
|
||||
task1,
|
||||
},
|
||||
sortParams: []*sortParam{
|
||||
{
|
||||
sortBy: taskPropertyID,
|
||||
orderBy: orderDescending,
|
||||
},
|
||||
},
|
||||
sortProperty: taskPropertyID,
|
||||
},
|
||||
{
|
||||
name: "Order by Text Ascending",
|
||||
|
@ -333,27 +320,95 @@ var taskSortTestCases = []taskSortTestCase{
|
|||
func TestTaskSort(t *testing.T) {
|
||||
for _, testCase := range taskSortTestCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
got := deepcopy.Copy(testCase.want).([]*Task)
|
||||
t.Run("asc default", func(t *testing.T) {
|
||||
by := []*sortParam{
|
||||
{
|
||||
sortBy: testCase.sortProperty,
|
||||
},
|
||||
}
|
||||
|
||||
// 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]
|
||||
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)
|
||||
|
||||
if !reflect.DeepEqual(got, testCase.wantAsc) {
|
||||
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 testCase.wantAsc {
|
||||
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
sortTasks(got, testCase.sortParams)
|
||||
|
||||
if !reflect.DeepEqual(got, testCase.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.Run("asc", func(t *testing.T) {
|
||||
by := []*sortParam{
|
||||
{
|
||||
sortBy: testCase.sortProperty,
|
||||
orderBy: orderAscending,
|
||||
},
|
||||
}
|
||||
|
||||
t.Error("Want:")
|
||||
for _, task := range testCase.want {
|
||||
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
|
||||
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)
|
||||
|
||||
if !reflect.DeepEqual(got, testCase.wantAsc) {
|
||||
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 testCase.wantAsc {
|
||||
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
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)
|
||||
|
||||
if !reflect.DeepEqual(got, testCase.wantDesc) {
|
||||
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 testCase.wantDesc {
|
||||
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue