-
Notifications
You must be signed in to change notification settings - Fork 12
/
sorting_test.go
237 lines (197 loc) · 6.12 KB
/
sorting_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package sorting_test
import (
"fmt"
"reflect"
"testing"
"github.com/jinzhu/gorm"
"github.com/qor/publish"
"github.com/qor/publish2"
"github.com/qor/qor/test/utils"
"github.com/qor/sorting"
)
type User struct {
gorm.Model
Name string
sorting.Sorting
publish2.Version
}
var db *gorm.DB
var pb *publish.Publish
func init() {
db = utils.TestDB()
sorting.RegisterCallbacks(db)
publish2.RegisterCallbacks(db)
db.AutoMigrate(&User{}, &Brand{})
}
func prepareUsers() {
utils.ResetDBTables(db, &User{})
for i := 1; i <= 5; i++ {
user := User{Name: fmt.Sprintf("user%v", i)}
if err := db.Save(&user).Error; err != nil {
panic(err)
}
}
}
func prepareVersioningUsers() {
utils.ResetDBTables(db, &User{})
for i := 1; i <= 5; i++ {
user := User{Name: fmt.Sprintf("user%v", i)}
for j := 0; j < 3; j++ {
user.SetVersionName(fmt.Sprintf("version-%v", j))
user.SetPosition(i)
if err := db.Save(&user).Error; err != nil {
panic(err)
}
db.Model(&user).UpdateColumn("Position", i)
}
}
}
func getUser(name string) *User {
var user User
db.First(&user, "name = ?", name)
return &user
}
func checkPosition(names ...string) bool {
var users []User
var positions []string
db.Find(&users)
for _, user := range users {
positions = append(positions, user.Name)
}
if reflect.DeepEqual(positions, names) {
return true
} else {
fmt.Printf("Expect %v, got %v\n", names, positions)
return false
}
}
func TestChangePositionForMultiVersionRecords(t *testing.T) {
prepareVersioningUsers()
u := getUser("user5")
sorting.MoveUp(db, u, 2)
if !checkPosition("user1", "user2", "user5", "user3", "user4") {
t.Errorf("user5 should be moved up")
}
user5Versions := []User{}
if err := db.Where("id = ?", u.ID).Find(&user5Versions).Error; err != nil {
t.Fatal(err)
}
u5 := getUser("user5")
for _, u5V := range user5Versions {
if u5V.GetPosition() != u5.GetPosition() {
t.Error("postion for same record version is not synced")
}
}
}
func TestMoveUpPosition(t *testing.T) {
prepareUsers()
sorting.MoveUp(db, getUser("user5"), 2)
if !checkPosition("user1", "user2", "user5", "user3", "user4") {
t.Errorf("user5 should be moved up")
}
sorting.MoveUp(db, getUser("user5"), 1)
if !checkPosition("user1", "user5", "user2", "user3", "user4") {
t.Errorf("user5's postion won't be changed because it is already the last")
}
sorting.MoveUp(db, getUser("user1"), 1)
if !checkPosition("user1", "user5", "user2", "user3", "user4") {
t.Errorf("user1's position won't be changed because it is already on the top")
}
sorting.MoveUp(db, getUser("user5"), 1)
if !checkPosition("user5", "user1", "user2", "user3", "user4") {
t.Errorf("user5 should be moved up")
}
}
func TestMoveDownPosition(t *testing.T) {
prepareUsers()
sorting.MoveDown(db, getUser("user1"), 1)
if !checkPosition("user2", "user1", "user3", "user4", "user5") {
t.Errorf("user1 should be moved down")
}
sorting.MoveDown(db, getUser("user1"), 2)
if !checkPosition("user2", "user3", "user4", "user1", "user5") {
t.Errorf("user1 should be moved down")
}
sorting.MoveDown(db, getUser("user5"), 2)
if !checkPosition("user2", "user3", "user4", "user1", "user5") {
t.Errorf("user5's postion won't be changed because it is already the last")
}
sorting.MoveDown(db, getUser("user1"), 1)
if !checkPosition("user2", "user3", "user4", "user5", "user1") {
t.Errorf("user1 should be moved down")
}
}
func TestMoveToPosition(t *testing.T) {
prepareUsers()
user := getUser("user5")
sorting.MoveTo(db, user, user.GetPosition()-3)
if !checkPosition("user1", "user5", "user2", "user3", "user4") {
t.Errorf("user5 should be moved to position 2")
}
user = getUser("user5")
sorting.MoveTo(db, user, user.GetPosition()-1)
if !checkPosition("user5", "user1", "user2", "user3", "user4") {
t.Errorf("user5 should be moved to position 1")
}
}
func TestDeleteToReorder(t *testing.T) {
prepareUsers()
if !(getUser("user1").GetPosition() == 1 && getUser("user2").GetPosition() == 2 && getUser("user3").GetPosition() == 3 && getUser("user4").GetPosition() == 4 && getUser("user5").GetPosition() == 5) {
t.Errorf("user's order should be correct after create")
}
user := getUser("user2")
db.Delete(user)
if !checkPosition("user1", "user3", "user4", "user5") {
t.Errorf("user2 is deleted, order should be correct")
}
if !(getUser("user1").GetPosition() == 1 && getUser("user3").GetPosition() == 2 && getUser("user4").GetPosition() == 3 && getUser("user5").GetPosition() == 4) {
t.Errorf("user's order should be correct after delete some resources")
}
}
func TestMultiMovePosition(t *testing.T) {
utils.ResetDBTables(db, &User{})
for i := 1; i <= 20; i++ {
user := User{Name: fmt.Sprintf("user%v", i)}
if err := db.Save(&user).Error; err != nil {
panic(err)
}
}
user7 := getUser("user7")
user8 := getUser("user8")
user21 := User{Name: fmt.Sprintf("user%v", 21)}
sorting.MoveTo(db, getUser("user5"), 10)
sorting.MoveTo(db, getUser("user5"), 1)
sorting.MoveTo(db, getUser("user5"), 15)
db.Delete(user7)
sorting.MoveTo(db, getUser("user5"), 3)
sorting.MoveTo(db, getUser("user5"), 7)
db.Delete(user8)
sorting.MoveTo(db, getUser("user5"), 20)
db.Save(&user21)
sorting.MoveTo(db, getUser("user5"), 21)
if p := getUser("user21").GetPosition(); p != 20 {
t.Errorf("user21 should at pos 20, but got pos: %v", p)
}
sorting.MoveTo(db, getUser("user5"), 8)
sorting.MoveTo(db, getUser("user21"), 3)
if p := getUser("user5").GetPosition(); p != 9 {
t.Errorf("user5 should at pos 9, but got pos: %v", p)
}
sorting.MoveTo(db, getUser("user5"), 1)
if p := getUser("user21").GetPosition(); p != 4 {
t.Errorf("user21 should at pos 4, but got pos: %v", p)
}
sorting.MoveTo(db, getUser("user5"), 21)
if p := getUser("user5").GetPosition(); p != 21 {
t.Errorf("user5 should at pos 9, but got pos: %v", p)
}
sorting.MoveTo(db, getUser("user5"), 10)
sorting.MoveTo(db, getUser("user5"), 16)
sorting.MoveTo(db, getUser("user5"), 19)
sorting.MoveTo(db, getUser("user5"), 4)
sorting.MoveTo(db, getUser("user5"), 7)
sorting.MoveTo(db, getUser("user5"), 14)
if p := getUser("user5").GetPosition(); p != 14 {
t.Errorf("user5 should at pos 14, but got pos: %v", p)
}
}