Debian Codegraph @

schema/field_test.go

raw
1 package schema_test
2
3 import (
4 "context"
5 "database/sql"
6 "reflect"
7 "sync"
8 "testing"
9 "time"
10
11 "gorm.io/gorm"
12 "gorm.io/gorm/schema"
13 "gorm.io/gorm/utils/tests"
14 )
15
16 func TestFieldValuerAndSetter(t *testing.T) {
17 var (
18 userSchema, _ = schema.Parse(&tests.User{}, &sync.Map{}, schema.NamingStrategy{})
19 user = tests.User{
20 Model: gorm.Model{
21 ID: 10,
22 CreatedAt: time.Now(),
23 UpdatedAt: time.Now(),
24 DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true},
25 },
26 Name: "valuer_and_setter",
27 Age: 18,
28 Birthday: tests.Now(),
29 Active: true,
30 }
31 reflectValue = reflect.ValueOf(&user)
32 )
33
34 // test valuer
35 values := map[string]interface{}{
36 "name": user.Name,
37 "id": user.ID,
38 "created_at": user.CreatedAt,
39 "updated_at": user.UpdatedAt,
40 "deleted_at": user.DeletedAt,
41 "age": user.Age,
42 "birthday": user.Birthday,
43 "active": true,
44 }
45 checkField(t, userSchema, reflectValue, values)
46
47 var f *bool
48 // test setter
49 newValues := map[string]interface{}{
50 "name": "valuer_and_setter_2",
51 "id": 2,
52 "created_at": time.Now(),
53 "updated_at": nil,
54 "deleted_at": time.Now(),
55 "age": 20,
56 "birthday": time.Now(),
57 "active": f,
58 }
59
60 for k, v := range newValues {
61 if err := userSchema.FieldsByDBName[k].Set(context.Background(), reflectValue, v); err != nil {
62 t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
63 }
64 }
65 newValues["updated_at"] = time.Time{}
66 newValues["active"] = false
67 checkField(t, userSchema, reflectValue, newValues)
68
69 // test valuer and other type
70 age := myint(10)
71 var nilTime *time.Time
72 newValues2 := map[string]interface{}{
73 "name": sql.NullString{String: "valuer_and_setter_3", Valid: true},
74 "id": &sql.NullInt64{Int64: 3, Valid: true},
75 "created_at": tests.Now(),
76 "updated_at": nilTime,
77 "deleted_at": time.Now(),
78 "age": &age,
79 "birthday": mytime(time.Now()),
80 "active": mybool(true),
81 }
82
83 for k, v := range newValues2 {
84 if err := userSchema.FieldsByDBName[k].Set(context.Background(), reflectValue, v); err != nil {
85 t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
86 }
87 }
88 newValues2["updated_at"] = time.Time{}
89 checkField(t, userSchema, reflectValue, newValues2)
90 }
91
92 func TestPointerFieldValuerAndSetter(t *testing.T) {
93 var (
94 userSchema, _ = schema.Parse(&User{}, &sync.Map{}, schema.NamingStrategy{})
95 name = "pointer_field_valuer_and_setter"
96 age uint = 18
97 active = true
98 user = User{
99 Model: &gorm.Model{
100 ID: 10,
101 CreatedAt: time.Now(),
102 DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true},
103 },
104 Name: &name,
105 Age: &age,
106 Birthday: tests.Now(),
107 Active: &active,
108 }
109 reflectValue = reflect.ValueOf(&user)
110 )
111
112 // test valuer
113 values := map[string]interface{}{
114 "name": user.Name,
115 "id": user.ID,
116 "created_at": user.CreatedAt,
117 "deleted_at": user.DeletedAt,
118 "age": user.Age,
119 "birthday": user.Birthday,
120 "active": true,
121 }
122 checkField(t, userSchema, reflectValue, values)
123
124 // test setter
125 newValues := map[string]interface{}{
126 "name": "valuer_and_setter_2",
127 "id": 2,
128 "created_at": time.Now(),
129 "deleted_at": time.Now(),
130 "age": 20,
131 "birthday": time.Now(),
132 "active": false,
133 }
134
135 for k, v := range newValues {
136 if err := userSchema.FieldsByDBName[k].Set(context.Background(), reflectValue, v); err != nil {
137 t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
138 }
139 }
140 checkField(t, userSchema, reflectValue, newValues)
141
142 // test valuer and other type
143 age2 := myint(10)
144 newValues2 := map[string]interface{}{
145 "name": sql.NullString{String: "valuer_and_setter_3", Valid: true},
146 "id": &sql.NullInt64{Int64: 3, Valid: true},
147 "created_at": tests.Now(),
148 "deleted_at": time.Now(),
149 "age": &age2,
150 "birthday": mytime(time.Now()),
151 "active": mybool(true),
152 }
153
154 for k, v := range newValues2 {
155 if err := userSchema.FieldsByDBName[k].Set(context.Background(), reflectValue, v); err != nil {
156 t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
157 }
158 }
159 checkField(t, userSchema, reflectValue, newValues2)
160 }
161
162 func TestAdvancedDataTypeValuerAndSetter(t *testing.T) {
163 var (
164 userSchema, _ = schema.Parse(&AdvancedDataTypeUser{}, &sync.Map{}, schema.NamingStrategy{})
165 name = "advanced_data_type_valuer_and_setter"
166 deletedAt = mytime(time.Now())
167 isAdmin = mybool(false)
168 user = AdvancedDataTypeUser{
169 ID: sql.NullInt64{Int64: 10, Valid: true},
170 Name: &sql.NullString{String: name, Valid: true},
171 Birthday: sql.NullTime{Time: time.Now(), Valid: true},
172 RegisteredAt: mytime(time.Now()),
173 DeletedAt: &deletedAt,
174 Active: mybool(true),
175 Admin: &isAdmin,
176 }
177 reflectValue = reflect.ValueOf(&user)
178 )
179
180 // test valuer
181 values := map[string]interface{}{
182 "id": user.ID,
183 "name": user.Name,
184 "birthday": user.Birthday,
185 "registered_at": user.RegisteredAt,
186 "deleted_at": user.DeletedAt,
187 "active": user.Active,
188 "admin": user.Admin,
189 }
190 checkField(t, userSchema, reflectValue, values)
191
192 // test setter
193 newDeletedAt := mytime(time.Now())
194 newIsAdmin := mybool(true)
195 newValues := map[string]interface{}{
196 "id": sql.NullInt64{Int64: 1, Valid: true},
197 "name": &sql.NullString{String: name + "rename", Valid: true},
198 "birthday": time.Now(),
199 "registered_at": mytime(time.Now()),
200 "deleted_at": &newDeletedAt,
201 "active": mybool(false),
202 "admin": &newIsAdmin,
203 }
204
205 for k, v := range newValues {
206 if err := userSchema.FieldsByDBName[k].Set(context.Background(), reflectValue, v); err != nil {
207 t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
208 }
209 }
210 checkField(t, userSchema, reflectValue, newValues)
211
212 newValues2 := map[string]interface{}{
213 "id": 5,
214 "name": name + "rename2",
215 "birthday": time.Now(),
216 "registered_at": time.Now(),
217 "deleted_at": time.Now(),
218 "active": true,
219 "admin": false,
220 }
221
222 for k, v := range newValues2 {
223 if err := userSchema.FieldsByDBName[k].Set(context.Background(), reflectValue, v); err != nil {
224 t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
225 }
226 }
227 checkField(t, userSchema, reflectValue, newValues2)
228 }
229
230 type UserWithPermissionControl struct {
231 ID uint
232 Name string `gorm:"-"`
233 Name2 string `gorm:"->"`
234 Name3 string `gorm:"<-"`
235 Name4 string `gorm:"<-:create"`
236 Name5 string `gorm:"<-:update"`
237 Name6 string `gorm:"<-:create,update"`
238 Name7 string `gorm:"->:false;<-:create,update"`
239 Name8 string `gorm:"->;-:migration"`
240 }
241
242 func TestParseFieldWithPermission(t *testing.T) {
243 user, err := schema.Parse(&UserWithPermissionControl{}, &sync.Map{}, schema.NamingStrategy{})
244 if err != nil {
245 t.Fatalf("Failed to parse user with permission, got error %v", err)
246 }
247
248 fields := []*schema.Field{
249 {Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, Creatable: true, Updatable: true, Readable: true, HasDefaultValue: true, AutoIncrement: true},
250 {Name: "Name", DBName: "", BindNames: []string{"Name"}, DataType: "", Tag: `gorm:"-"`, Creatable: false, Updatable: false, Readable: false},
251 {Name: "Name2", DBName: "name2", BindNames: []string{"Name2"}, DataType: schema.String, Tag: `gorm:"->"`, Creatable: false, Updatable: false, Readable: true},
252 {Name: "Name3", DBName: "name3", BindNames: []string{"Name3"}, DataType: schema.String, Tag: `gorm:"<-"`, Creatable: true, Updatable: true, Readable: true},
253 {Name: "Name4", DBName: "name4", BindNames: []string{"Name4"}, DataType: schema.String, Tag: `gorm:"<-:create"`, Creatable: true, Updatable: false, Readable: true},
254 {Name: "Name5", DBName: "name5", BindNames: []string{"Name5"}, DataType: schema.String, Tag: `gorm:"<-:update"`, Creatable: false, Updatable: true, Readable: true},
255 {Name: "Name6", DBName: "name6", BindNames: []string{"Name6"}, DataType: schema.String, Tag: `gorm:"<-:create,update"`, Creatable: true, Updatable: true, Readable: true},
256 {Name: "Name7", DBName: "name7", BindNames: []string{"Name7"}, DataType: schema.String, Tag: `gorm:"->:false;<-:create,update"`, Creatable: true, Updatable: true, Readable: false},
257 {Name: "Name8", DBName: "name8", BindNames: []string{"Name8"}, DataType: schema.String, Tag: `gorm:"->;-:migration"`, Creatable: false, Updatable: false, Readable: true, IgnoreMigration: true},
258 }
259
260 for _, f := range fields {
261 checkSchemaField(t, user, f, func(f *schema.Field) {})
262 }
263 }
264
265 type (
266 ID int64
267 INT int
268 INT8 int8
269 INT16 int16
270 INT32 int32
271 INT64 int64
272 UINT uint
273 UINT8 uint8
274 UINT16 uint16
275 UINT32 uint32
276 UINT64 uint64
277 FLOAT32 float32
278 FLOAT64 float64
279 BOOL bool
280 STRING string
281 TIME time.Time
282 BYTES []byte
283
284 TypeAlias struct {
285 ID
286 INT `gorm:"column:fint"`
287 INT8 `gorm:"column:fint8"`
288 INT16 `gorm:"column:fint16"`
289 INT32 `gorm:"column:fint32"`
290 INT64 `gorm:"column:fint64"`
291 UINT `gorm:"column:fuint"`
292 UINT8 `gorm:"column:fuint8"`
293 UINT16 `gorm:"column:fuint16"`
294 UINT32 `gorm:"column:fuint32"`
295 UINT64 `gorm:"column:fuint64"`
296 FLOAT32 `gorm:"column:ffloat32"`
297 FLOAT64 `gorm:"column:ffloat64"`
298 BOOL `gorm:"column:fbool"`
299 STRING `gorm:"column:fstring"`
300 TIME `gorm:"column:ftime"`
301 BYTES `gorm:"column:fbytes"`
302 }
303 )
304
305 func TestTypeAliasField(t *testing.T) {
306 alias, err := schema.Parse(&TypeAlias{}, &sync.Map{}, schema.NamingStrategy{})
307 if err != nil {
308 t.Fatalf("Failed to parse TypeAlias with permission, got error %v", err)
309 }
310
311 fields := []*schema.Field{
312 {Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Int, Creatable: true, Updatable: true, Readable: true, Size: 64, PrimaryKey: true, HasDefaultValue: true, AutoIncrement: true},
313 {Name: "INT", DBName: "fint", BindNames: []string{"INT"}, DataType: schema.Int, Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fint"`},
314 {Name: "INT8", DBName: "fint8", BindNames: []string{"INT8"}, DataType: schema.Int, Creatable: true, Updatable: true, Readable: true, Size: 8, Tag: `gorm:"column:fint8"`},
315 {Name: "INT16", DBName: "fint16", BindNames: []string{"INT16"}, DataType: schema.Int, Creatable: true, Updatable: true, Readable: true, Size: 16, Tag: `gorm:"column:fint16"`},
316 {Name: "INT32", DBName: "fint32", BindNames: []string{"INT32"}, DataType: schema.Int, Creatable: true, Updatable: true, Readable: true, Size: 32, Tag: `gorm:"column:fint32"`},
317 {Name: "INT64", DBName: "fint64", BindNames: []string{"INT64"}, DataType: schema.Int, Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fint64"`},
318 {Name: "UINT", DBName: "fuint", BindNames: []string{"UINT"}, DataType: schema.Uint, Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fuint"`},
319 {Name: "UINT8", DBName: "fuint8", BindNames: []string{"UINT8"}, DataType: schema.Uint, Creatable: true, Updatable: true, Readable: true, Size: 8, Tag: `gorm:"column:fuint8"`},
320 {Name: "UINT16", DBName: "fuint16", BindNames: []string{"UINT16"}, DataType: schema.Uint, Creatable: true, Updatable: true, Readable: true, Size: 16, Tag: `gorm:"column:fuint16"`},
321 {Name: "UINT32", DBName: "fuint32", BindNames: []string{"UINT32"}, DataType: schema.Uint, Creatable: true, Updatable: true, Readable: true, Size: 32, Tag: `gorm:"column:fuint32"`},
322 {Name: "UINT64", DBName: "fuint64", BindNames: []string{"UINT64"}, DataType: schema.Uint, Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:fuint64"`},
323 {Name: "FLOAT32", DBName: "ffloat32", BindNames: []string{"FLOAT32"}, DataType: schema.Float, Creatable: true, Updatable: true, Readable: true, Size: 32, Tag: `gorm:"column:ffloat32"`},
324 {Name: "FLOAT64", DBName: "ffloat64", BindNames: []string{"FLOAT64"}, DataType: schema.Float, Creatable: true, Updatable: true, Readable: true, Size: 64, Tag: `gorm:"column:ffloat64"`},
325 {Name: "BOOL", DBName: "fbool", BindNames: []string{"BOOL"}, DataType: schema.Bool, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbool"`},
326 {Name: "STRING", DBName: "fstring", BindNames: []string{"STRING"}, DataType: schema.String, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fstring"`},
327 {Name: "TIME", DBName: "ftime", BindNames: []string{"TIME"}, DataType: schema.Time, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:ftime"`},
328 {Name: "BYTES", DBName: "fbytes", BindNames: []string{"BYTES"}, DataType: schema.Bytes, Creatable: true, Updatable: true, Readable: true, Tag: `gorm:"column:fbytes"`},
329 }
330
331 for _, f := range fields {
332 checkSchemaField(t, alias, f, func(f *schema.Field) {})
333 }
334 }