Repository navigation
Expand file tree
/
Copy pathbatch_update_test.go
More file actions
222 lines (204 loc) · 5.91 KB
/
Copy pathbatch_update_test.go
File metadata and controls
222 lines (204 loc) · 5.91 KB
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
package cosy
import (
"fmt"
"testing"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"github.com/stretchr/testify/assert"
"github.com/uozi-tech/cosy/logger"
"github.com/uozi-tech/cosy/map2struct"
"github.com/uozi-tech/cosy/model"
"github.com/uozi-tech/cosy/router"
"github.com/uozi-tech/cosy/sandbox"
"github.com/uozi-tech/cosy/settings"
)
func TestCtx_BatchModify(t *testing.T) {
sandbox.NewInstance("app.ini", "pgsql").
RegisterModels(User{}).Run(func(instance *sandbox.Instance) {
r := router.GetEngine()
err := r.SetTrustedProxies([]string{"127.0.0.1"})
if err != nil {
t.Error(err)
return
}
g := r.Group("/")
Api[User]("users").InitRouter(g)
g.PUT("/users", func(c *gin.Context) {
Core[User](c).
SetValidRules(gin.H{
"gender": "omitempty",
"age": "omitempty",
}).BatchModify()
})
testBatchModify(t, instance)
})
}
func testBatchModify(t *testing.T, instance *sandbox.Instance) {
body := map[string]any{
"school_id": "0281876",
"avatar": "",
"gender": 0,
"name": "张三-1",
"password": "123457887",
"age": 20,
"title": "助理教授",
"bio": "",
"college": "大数据与互联网学院",
"direction": "大数据与人工智能",
"teacher_certificate": "/xx/xx.pdf",
"contract": "/xx/xx.pdf",
"task_agreement": "/xx/xx.pdf",
"office_number": "208",
"email": "1@aa.com",
"phone": "123456789",
"user_group_id": 1,
"status": 1,
"employed_at": "2024-03-13T11:22:44.405374+08:00",
}
c := instance.GetClient()
resp, err := c.Post("/users", body)
if err != nil {
t.Error(err)
return
}
var firstUser User
err = resp.To(&firstUser)
if err != nil {
t.Error(err)
return
}
body = map[string]any{
"school_id": "0281877",
"avatar": "",
"gender": 0,
"name": "张三-2",
"password": "123457887",
"age": 20,
"title": "助理教授",
"bio": "",
"college": "大数据与互联网学院",
"direction": "大数据与人工智能",
"teacher_certificate": "/xx/xx.pdf",
"contract": "/xx/xx.pdf",
"task_agreement": "/xx/xx.pdf",
"office_number": "208",
"email": "2@aa.com",
"phone": "123456789",
"user_group_id": 1,
"status": 1,
"employed_at": "2024-03-13T11:22:44.405374+08:00",
}
resp, err = c.Post("/users", body)
if err != nil {
t.Error(err)
return
}
var secondUser User
err = resp.To(&secondUser)
if err != nil {
t.Error(err)
return
}
body = map[string]any{
"gender": 0,
"age": 30,
"bio": "SHOULD_NOT_BE_MODIFIED",
}
resp, err = c.Put("/users", gin.H{
"ids": []string{fmt.Sprint(firstUser.ID), fmt.Sprint(secondUser.ID)},
"data": body,
})
if err != nil {
t.Error(err)
return
}
logger.Debug(resp.BodyText())
var data User
err = resp.To(&data)
if err != nil {
t.Error(err)
return
}
resp, err = c.Get("/users")
if err != nil {
t.Error(err)
return
}
var dataList model.DataList
err = resp.To(&dataList)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, int64(2), dataList.Pagination.Total)
assert.Equal(t, 1, dataList.Pagination.CurrentPage)
assert.Equal(t, int64(1), dataList.Pagination.TotalPages)
assert.Equal(t, settings.AppSettings.PageSize, dataList.Pagination.PerPage)
mapData := dataList.Data.([]any)[0]
var user User
err = map2struct.WeakDecode(mapData, &user)
if err != nil {
logger.Error(err)
return
}
assert.Equal(t, "0281877", user.SchoolID)
assert.Equal(t, "", user.Avatar)
assert.Equal(t, "张三-2", user.Name)
assert.Equal(t, "0", user.Gender)
assert.Equal(t, 30, user.Age)
assert.Equal(t, "助理教授", user.Title)
assert.Equal(t, "", user.Bio)
assert.Equal(t, "大数据与互联网学院", user.College)
assert.Equal(t, "大数据与人工智能", user.Direction)
assert.Equal(t, "/xx/xx.pdf", user.TeacherCertificate)
assert.Equal(t, "/xx/xx.pdf", user.Contract)
assert.Equal(t, "/xx/xx.pdf", user.TaskAgreement)
assert.Equal(t, "208", user.OfficeNumber)
assert.Equal(t, "", user.Password)
assert.Equal(t, "2@aa.com", user.Email)
assert.Equal(t, "123456789", user.Phone)
assert.Equal(t, 1, user.Status)
assert.Equal(t, cast.ToTime("2024-03-13T11:22:44.405374+08:00"), *user.EmployedAt)
mapData = dataList.Data.([]any)[1]
user = User{}
err = map2struct.WeakDecode(mapData, &user)
if err != nil {
logger.Error(err)
return
}
assert.Equal(t, "0281876", user.SchoolID)
assert.Equal(t, "", user.Avatar)
assert.Equal(t, "张三-1", user.Name)
assert.Equal(t, "0", user.Gender)
assert.Equal(t, 30, user.Age)
assert.Equal(t, "助理教授", user.Title)
assert.Equal(t, "", user.Bio)
assert.Equal(t, "大数据与互联网学院", user.College)
assert.Equal(t, "大数据与人工智能", user.Direction)
assert.Equal(t, "/xx/xx.pdf", user.TeacherCertificate)
assert.Equal(t, "/xx/xx.pdf", user.Contract)
assert.Equal(t, "/xx/xx.pdf", user.TaskAgreement)
assert.Equal(t, "208", user.OfficeNumber)
assert.Equal(t, "", user.Password)
assert.Equal(t, "1@aa.com", user.Email)
assert.Equal(t, "123456789", user.Phone)
assert.Equal(t, 1, user.Status)
assert.Equal(t, cast.ToTime("2024-03-13T11:22:44.405374+08:00"), *user.EmployedAt)
// data without any batch-updatable field is rejected instead of running
// an unrestricted update
resp, err = c.Put("/users", gin.H{
"ids": []string{fmt.Sprint(firstUser.ID), fmt.Sprint(secondUser.ID)},
"data": gin.H{"bio": "SHOULD_NOT_BE_MODIFIED"},
})
if err != nil {
t.Error(err)
return
}
assert.Equal(t, 406, resp.StatusCode, resp.BodyText())
var verr ValidateError
if err = resp.To(&verr); err != nil {
t.Error(err)
return
}
assert.Equal(t, ErrEmptyPayload, verr.Errors["body"])
}