-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsortable_collection_test.go
104 lines (85 loc) · 2.77 KB
/
sortable_collection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package sorting_test
import (
"fmt"
"reflect"
"testing"
"github.com/jinzhu/gorm"
"github.com/qor/sorting"
)
type ColorVariation struct {
gorm.Model
Code string
}
func checkOrder(results interface{}, order []string) error {
values := reflect.Indirect(reflect.ValueOf(results))
for idx, o := range order {
value := values.Index(idx)
primaryValue := fmt.Sprint(reflect.Indirect(value).FieldByName("ID").Interface())
if primaryValue != o {
return fmt.Errorf("#%v of values's primary key is %v, but should be %v", idx+1, primaryValue, o)
}
}
return nil
}
func TestSortSlice(t *testing.T) {
colorVariations := []ColorVariation{
{Model: gorm.Model{ID: 1}, Code: "1"},
{Model: gorm.Model{ID: 2}, Code: "2"},
{Model: gorm.Model{ID: 3}, Code: "3"},
}
collectionSorting := sorting.SortableCollection{PrimaryKeys: []string{"3", "1", "2"}}
collectionSorting.Sort(colorVariations)
if err := checkOrder(colorVariations, []string{"3", "1", "2"}); err != nil {
t.Error(err)
}
}
func TestSort(t *testing.T) {
colorVariations := &[]ColorVariation{
{Model: gorm.Model{ID: 1}, Code: "1"},
{Model: gorm.Model{ID: 2}, Code: "2"},
{Model: gorm.Model{ID: 3}, Code: "3"},
}
collectionSorting := sorting.SortableCollection{PrimaryKeys: []string{"3", "1", "2"}}
collectionSorting.Sort(colorVariations)
if err := checkOrder(colorVariations, []string{"3", "1", "2"}); err != nil {
t.Error(err)
}
}
func TestSortPointer(t *testing.T) {
colorVariations := &[]*ColorVariation{
{Model: gorm.Model{ID: 1}, Code: "1"},
{Model: gorm.Model{ID: 2}, Code: "2"},
{Model: gorm.Model{ID: 3}, Code: "3"},
}
collectionSorting := sorting.SortableCollection{PrimaryKeys: []string{"3", "1", "2"}}
collectionSorting.Sort(colorVariations)
if err := checkOrder(colorVariations, []string{"3", "1", "2"}); err != nil {
t.Error(err)
}
}
func TestSortWithSomePrimaryKeys(t *testing.T) {
colorVariations := &[]ColorVariation{
{Model: gorm.Model{ID: 1}, Code: "1"},
{Model: gorm.Model{ID: 2}, Code: "2"},
{Model: gorm.Model{ID: 3}, Code: "3"},
{Model: gorm.Model{ID: 4}, Code: "4"},
}
collectionSorting := sorting.SortableCollection{PrimaryKeys: []string{"3", "1"}}
collectionSorting.Sort(colorVariations)
if err := checkOrder(colorVariations, []string{"3", "1", "2", "4"}); err != nil {
t.Error(err)
}
}
func TestSortPointerWithSomePrimaryKeys(t *testing.T) {
colorVariations := &[]*ColorVariation{
{Model: gorm.Model{ID: 1}, Code: "1"},
{Model: gorm.Model{ID: 2}, Code: "2"},
{Model: gorm.Model{ID: 3}, Code: "3"},
{Model: gorm.Model{ID: 4}, Code: "4"},
}
collectionSorting := sorting.SortableCollection{PrimaryKeys: []string{"3", "1"}}
collectionSorting.Sort(colorVariations)
if err := checkOrder(colorVariations, []string{"3", "1", "2", "4"}); err != nil {
t.Error(err)
}
}