forked from lilith44/gox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paging_test.go
179 lines (153 loc) · 4.69 KB
/
paging_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package gox
import (
"fmt"
"testing"
)
func TestManualPaging_Sort_PtrSlice(t *testing.T) {
type test struct {
Id int64
CourseSum int
TeacherCount int
Proportion float32
}
originData := []*test{
{Id: 3, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 6, CourseSum: 86, TeacherCount: 2, Proportion: 0.3},
{Id: 2, CourseSum: 70, TeacherCount: 2, Proportion: 0.6},
{Id: 4, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 5, CourseSum: 70, TeacherCount: 1, Proportion: 0.8},
{Id: 1, CourseSum: 86, TeacherCount: 2, Proportion: 0.5},
}
expected := []*test{
{Id: 1, CourseSum: 86, TeacherCount: 2, Proportion: 0.5},
{Id: 6, CourseSum: 86, TeacherCount: 2, Proportion: 0.3},
{Id: 3, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 4, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 2, CourseSum: 70, TeacherCount: 2, Proportion: 0.6},
{Id: 5, CourseSum: 70, TeacherCount: 1, Proportion: 0.8},
}
sortedData := NewManualPaging(originData, Paging{
Page: 2,
PerPage: 3,
}).Sort("course_sum DESC,teacher_count DESC,proportion DESC,id ASC")
results := sortedData.GetInterface().([]*test)
fmt.Println("Slice By Sort:")
for _, result := range results {
fmt.Println(result)
}
for index := range expected {
if *results[index] != *expected[index] {
t.Fatalf("sort(),第%v元素,期望:%v,实际:%v", index, *expected[index], *results[index])
}
}
pagingResults := sortedData.Paging().([]*test)
expectedPaging := []*test{
{Id: 4, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 2, CourseSum: 70, TeacherCount: 2, Proportion: 0.6},
{Id: 5, CourseSum: 70, TeacherCount: 1, Proportion: 0.8},
}
fmt.Println("Slice By Sort And Paging:")
for _, result := range pagingResults {
fmt.Println(result)
}
for index := range expectedPaging {
if *pagingResults[index] != *expectedPaging[index] {
t.Fatalf("sort(),第%v元素,期望:%v,实际:%v", index, *expectedPaging[index], *pagingResults[index])
}
}
}
func TestManualPaging_Sort_StructSlice(t *testing.T) {
type test struct {
Id int64
CourseSum int
TeacherCount int
Proportion float32
}
originData := []test{
{Id: 3, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 6, CourseSum: 86, TeacherCount: 2, Proportion: 0.3},
{Id: 2, CourseSum: 70, TeacherCount: 2, Proportion: 0.6},
{Id: 4, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 5, CourseSum: 70, TeacherCount: 1, Proportion: 0.8},
{Id: 1, CourseSum: 86, TeacherCount: 2, Proportion: 0.5},
}
expected := []test{
{Id: 1, CourseSum: 86, TeacherCount: 2, Proportion: 0.5},
{Id: 6, CourseSum: 86, TeacherCount: 2, Proportion: 0.3},
{Id: 3, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 4, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 2, CourseSum: 70, TeacherCount: 2, Proportion: 0.6},
{Id: 5, CourseSum: 70, TeacherCount: 1, Proportion: 0.8},
}
sortedData := NewManualPaging(originData, Paging{
Page: 2,
PerPage: 3,
}).Sort("course_sum DESC,teacher_count DESC,proportion DESC,id ASC")
results := sortedData.GetInterface().([]test)
fmt.Println("Slice By Sort:")
for _, result := range results {
fmt.Println(result)
}
for index := range expected {
if results[index] != expected[index] {
t.Fatalf("sort(),第%v元素,期望:%v,实际:%v", index, expected[index], results[index])
}
}
pagingResults := sortedData.Paging().([]test)
expectedPaging := []test{
{Id: 4, CourseSum: 76, TeacherCount: 3, Proportion: 0.4},
{Id: 2, CourseSum: 70, TeacherCount: 2, Proportion: 0.6},
{Id: 5, CourseSum: 70, TeacherCount: 1, Proportion: 0.8},
}
fmt.Println("Slice By Sort And Paging:")
for _, result := range pagingResults {
fmt.Println(result)
}
for index := range expectedPaging {
if pagingResults[index] != expectedPaging[index] {
t.Fatalf("sort(),第%v元素,期望:%v,实际:%v", index, expectedPaging[index], pagingResults[index])
}
}
}
func TestPaging_EscapedLikeKeyword(t *testing.T) {
type suit struct {
input *Paging
want string
}
suits := []*suit{
{
input: &Paging{Keyword: ""},
want: "",
},
{
input: &Paging{Keyword: "%_"},
want: "\\%\\_",
},
{
input: &Paging{Keyword: "12345"},
want: "12345",
},
{
input: &Paging{Keyword: "我是关键字"},
want: "我是关键字",
},
{
input: &Paging{Keyword: "我是关%键字"},
want: "我是关\\%键字",
},
{
input: &Paging{Keyword: "我是关_键字"},
want: "我是关\\_键字",
},
{
input: &Paging{Keyword: "我是关_键%字"},
want: "我是关\\_键\\%字",
},
}
for _, suit := range suits {
output := suit.input.EscapedLikeKeyword()
if output != suit.want {
t.Fail()
}
}
}