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

Function TestPointerFieldValuerAndSetter

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

Source from the content-addressed store, hash-verified

90}
91
92func 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,

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