MCPcopy
hub / github.com/go-gorm/gorm / TestFieldValuerAndSetter

Function TestFieldValuerAndSetter

schema/field_test.go:16–90  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func 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},

Callers

nothing calls this directly

Calls 6

ParseFunction · 0.92
NowFunction · 0.92
checkFieldFunction · 0.85
myintTypeAlias · 0.85
mytimeTypeAlias · 0.85
SetMethod · 0.65

Tested by

no test coverage detected