Fix sorting tasks by bool values

There was a bug where it would return all tasks with a true value before the ones with a false value. This is the exact opposite of what the db does, leading to wrongly sorted values
This commit is contained in:
kolaente 2019-12-07 16:56:18 +01:00
parent d8399e374c
commit 1555f04bd2
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 53 additions and 17 deletions

View file

@ -146,9 +146,9 @@ func mustMakeComparator(fieldName string) taskComparator {
return func(lhs, rhs *Task) int64 { return func(lhs, rhs *Task) int64 {
boolLHS, boolRHS := extractProp(lhs).(bool), extractProp(rhs).(bool) boolLHS, boolRHS := extractProp(lhs).(bool), extractProp(rhs).(bool)
if !boolLHS && boolRHS { if !boolLHS && boolRHS {
return 1
} else if boolLHS && !boolRHS {
return -1 return -1
} else if boolLHS && !boolRHS {
return 1
} }
return 0 return 0
} }

View file

@ -277,10 +277,6 @@ var taskSortTestCases = []taskSortTestCase{
name: "done", name: "done",
sortProperty: taskPropertyDone, sortProperty: taskPropertyDone,
wantAsc: []*Task{ wantAsc: []*Task{
// These are done
task1,
task2,
task9,
// These are not // These are not
task3, task3,
task4, task4,
@ -289,8 +285,16 @@ var taskSortTestCases = []taskSortTestCase{
task7, task7,
task8, task8,
task10, task10,
// These are done
task1,
task2,
task9,
}, },
wantDesc: []*Task{ wantDesc: []*Task{
// These are done
task1,
task2,
task9,
// These are not // These are not
task3, task3,
task4, task4,
@ -299,10 +303,6 @@ var taskSortTestCases = []taskSortTestCase{
task7, task7,
task8, task8,
task10, task10,
// These are done
task1,
task2,
task9,
}, },
}, },
{ {
@ -749,13 +749,46 @@ func TestTaskSort(t *testing.T) {
} }
// Other cases // Other cases
t.Run("Order by Done Ascending and Text Descending", func(t *testing.T) { t.Run("Order by Done Ascending and ID Descending", func(t *testing.T) {
want := []*Task{ want := []*Task{
// Not done
task10,
task8,
task7,
task6,
task5,
task4,
task3,
// Done // Done
task9,
task2, task2,
task1, task1,
task9, }
sortParams := []*sortParam{
{
sortBy: taskPropertyDone,
orderBy: orderAscending,
},
{
sortBy: taskPropertyID,
orderBy: orderDescending,
},
}
got := deepcopy.Copy(want).([]*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, sortParams)
assertTestSliceMatch(t, got, want)
})
t.Run("Order by Done Ascending and Text Descending", func(t *testing.T) {
want := []*Task{
// Not done // Not done
task10, task10,
task7, task7,
@ -764,6 +797,10 @@ func TestTaskSort(t *testing.T) {
task4, task4,
task3, task3,
task8, task8,
// Done
task2,
task1,
task9,
} }
sortParams := []*sortParam{ sortParams := []*sortParam{
{ {
@ -789,6 +826,10 @@ func TestTaskSort(t *testing.T) {
}) })
t.Run("Order by Done Descending and Text Ascending", func(t *testing.T) { t.Run("Order by Done Descending and Text Ascending", func(t *testing.T) {
want := []*Task{ want := []*Task{
// Done
task9,
task1,
task2,
// Not done // Not done
task8, task8,
task3, task3,
@ -797,11 +838,6 @@ func TestTaskSort(t *testing.T) {
task6, task6,
task7, task7,
task10, task10,
// Done
task9,
task1,
task2,
} }
sortParams := []*sortParam{ sortParams := []*sortParam{
{ {