gorm/schema/field_test.go

252 lines
8.0 KiB
Go
Raw Normal View History

2020-02-15 11:04:21 +03:00
package schema_test
import (
2020-02-15 19:37:59 +03:00
"database/sql"
2020-02-15 11:04:21 +03:00
"reflect"
"sync"
"testing"
"time"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
"gorm.io/gorm/schema"
2020-06-02 05:34:50 +03:00
"gorm.io/gorm/utils/tests"
2020-02-15 11:04:21 +03:00
)
func TestFieldValuerAndSetter(t *testing.T) {
var (
2020-02-24 03:51:35 +03:00
userSchema, _ = schema.Parse(&tests.User{}, &sync.Map{}, schema.NamingStrategy{})
user = tests.User{
2020-02-15 11:04:21 +03:00
Model: gorm.Model{
ID: 10,
CreatedAt: time.Now(),
2020-05-30 11:47:16 +03:00
DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true},
2020-02-15 11:04:21 +03:00
},
Name: "valuer_and_setter",
Age: 18,
Birthday: tests.Now(),
2020-02-15 14:45:27 +03:00
Active: true,
2020-02-15 11:04:21 +03:00
}
2020-02-15 14:45:27 +03:00
reflectValue = reflect.ValueOf(&user)
2020-02-15 11:04:21 +03:00
)
2020-02-15 14:45:27 +03:00
// test valuer
2020-02-15 11:04:21 +03:00
values := map[string]interface{}{
"name": user.Name,
"id": user.ID,
"created_at": user.CreatedAt,
"deleted_at": user.DeletedAt,
"age": user.Age,
"birthday": user.Birthday,
2020-02-15 14:45:27 +03:00
"active": true,
2020-02-15 11:04:21 +03:00
}
2020-02-15 14:45:27 +03:00
checkField(t, userSchema, reflectValue, values)
2020-02-15 11:04:21 +03:00
2020-02-15 14:45:27 +03:00
// test setter
2020-02-15 11:04:21 +03:00
newValues := map[string]interface{}{
"name": "valuer_and_setter_2",
2020-02-15 14:45:27 +03:00
"id": 2,
2020-02-15 11:04:21 +03:00
"created_at": time.Now(),
2020-05-30 11:47:16 +03:00
"deleted_at": time.Now(),
2020-02-15 11:04:21 +03:00
"age": 20,
"birthday": time.Now(),
2020-02-15 14:45:27 +03:00
"active": false,
2020-02-15 11:04:21 +03:00
}
for k, v := range newValues {
if err := userSchema.FieldsByDBName[k].Set(reflectValue, v); err != nil {
2020-02-15 19:37:59 +03:00
t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
2020-02-15 11:04:21 +03:00
}
2020-02-15 14:45:27 +03:00
}
checkField(t, userSchema, reflectValue, newValues)
2020-02-15 19:37:59 +03:00
// test valuer and other type
age := myint(10)
newValues2 := map[string]interface{}{
"name": sql.NullString{String: "valuer_and_setter_3", Valid: true},
"id": &sql.NullInt64{Int64: 3, Valid: true},
"created_at": tests.Now(),
"deleted_at": time.Now(),
"age": &age,
"birthday": mytime(time.Now()),
"active": mybool(true),
}
for k, v := range newValues2 {
if err := userSchema.FieldsByDBName[k].Set(reflectValue, v); err != nil {
t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
}
}
checkField(t, userSchema, reflectValue, newValues2)
2020-02-15 14:45:27 +03:00
}
func TestPointerFieldValuerAndSetter(t *testing.T) {
var (
2020-02-24 03:51:35 +03:00
userSchema, _ = schema.Parse(&User{}, &sync.Map{}, schema.NamingStrategy{})
name = "pointer_field_valuer_and_setter"
age uint = 18
active = true
user = User{
2020-02-15 14:45:27 +03:00
Model: &gorm.Model{
ID: 10,
CreatedAt: time.Now(),
2020-05-30 11:47:16 +03:00
DeletedAt: gorm.DeletedAt{Time: time.Now(), Valid: true},
2020-02-15 14:45:27 +03:00
},
Name: &name,
Age: &age,
Birthday: tests.Now(),
Active: &active,
}
reflectValue = reflect.ValueOf(&user)
)
// test valuer
values := map[string]interface{}{
"name": user.Name,
"id": user.ID,
"created_at": user.CreatedAt,
"deleted_at": user.DeletedAt,
"age": user.Age,
"birthday": user.Birthday,
"active": true,
}
checkField(t, userSchema, reflectValue, values)
// test setter
newValues := map[string]interface{}{
"name": "valuer_and_setter_2",
"id": 2,
"created_at": time.Now(),
2020-05-30 11:47:16 +03:00
"deleted_at": time.Now(),
2020-02-15 14:45:27 +03:00
"age": 20,
"birthday": time.Now(),
"active": false,
}
2020-02-15 11:04:21 +03:00
2020-02-15 14:45:27 +03:00
for k, v := range newValues {
if err := userSchema.FieldsByDBName[k].Set(reflectValue, v); err != nil {
t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
2020-02-15 11:04:21 +03:00
}
}
2020-02-15 14:45:27 +03:00
checkField(t, userSchema, reflectValue, newValues)
2020-02-15 19:37:59 +03:00
// test valuer and other type
age2 := myint(10)
newValues2 := map[string]interface{}{
"name": sql.NullString{String: "valuer_and_setter_3", Valid: true},
"id": &sql.NullInt64{Int64: 3, Valid: true},
"created_at": tests.Now(),
"deleted_at": time.Now(),
"age": &age2,
"birthday": mytime(time.Now()),
"active": mybool(true),
}
for k, v := range newValues2 {
if err := userSchema.FieldsByDBName[k].Set(reflectValue, v); err != nil {
t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
}
}
checkField(t, userSchema, reflectValue, newValues2)
2020-02-15 14:45:27 +03:00
}
2020-02-15 19:37:59 +03:00
func TestAdvancedDataTypeValuerAndSetter(t *testing.T) {
var (
2020-02-24 03:51:35 +03:00
userSchema, _ = schema.Parse(&AdvancedDataTypeUser{}, &sync.Map{}, schema.NamingStrategy{})
name = "advanced_data_type_valuer_and_setter"
deletedAt = mytime(time.Now())
isAdmin = mybool(false)
user = AdvancedDataTypeUser{
2020-02-15 19:37:59 +03:00
ID: sql.NullInt64{Int64: 10, Valid: true},
Name: &sql.NullString{String: name, Valid: true},
Birthday: sql.NullTime{Time: time.Now(), Valid: true},
RegisteredAt: mytime(time.Now()),
DeletedAt: &deletedAt,
Active: mybool(true),
Admin: &isAdmin,
}
reflectValue = reflect.ValueOf(&user)
)
// test valuer
values := map[string]interface{}{
"id": user.ID,
"name": user.Name,
"birthday": user.Birthday,
"registered_at": user.RegisteredAt,
"deleted_at": user.DeletedAt,
"active": user.Active,
"admin": user.Admin,
}
checkField(t, userSchema, reflectValue, values)
// test setter
newDeletedAt := mytime(time.Now())
newIsAdmin := mybool(true)
newValues := map[string]interface{}{
"id": sql.NullInt64{Int64: 1, Valid: true},
"name": &sql.NullString{String: name + "rename", Valid: true},
"birthday": time.Now(),
"registered_at": mytime(time.Now()),
"deleted_at": &newDeletedAt,
"active": mybool(false),
"admin": &newIsAdmin,
}
for k, v := range newValues {
if err := userSchema.FieldsByDBName[k].Set(reflectValue, v); err != nil {
t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
}
}
checkField(t, userSchema, reflectValue, newValues)
newValues2 := map[string]interface{}{
"id": 5,
"name": name + "rename2",
"birthday": time.Now(),
"registered_at": time.Now(),
"deleted_at": time.Now(),
"active": true,
"admin": false,
}
for k, v := range newValues2 {
if err := userSchema.FieldsByDBName[k].Set(reflectValue, v); err != nil {
t.Errorf("no error should happen when assign value to field %v, but got %v", k, err)
}
}
checkField(t, userSchema, reflectValue, newValues2)
2020-02-15 11:04:21 +03:00
}
type UserWithPermissionControl struct {
ID uint
Name string `gorm:"-"`
Name2 string `gorm:"->"`
Name3 string `gorm:"<-"`
Name4 string `gorm:"<-:create"`
Name5 string `gorm:"<-:update"`
Name6 string `gorm:"<-:create,update"`
2020-06-05 14:18:22 +03:00
Name7 string `gorm:"->:false;<-:create,update"`
}
func TestParseFieldWithPermission(t *testing.T) {
user, err := schema.Parse(&UserWithPermissionControl{}, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
t.Fatalf("Failed to parse user with permission, got error %v", err)
}
fields := []schema.Field{
{Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, Creatable: true, Updatable: true, Readable: true},
2020-06-05 14:18:22 +03:00
{Name: "Name", DBName: "", BindNames: []string{"Name"}, DataType: "", Tag: `gorm:"-"`, Creatable: false, Updatable: false, Readable: false},
{Name: "Name2", DBName: "name2", BindNames: []string{"Name2"}, DataType: schema.String, Tag: `gorm:"->"`, Creatable: false, Updatable: false, Readable: true},
2020-06-05 14:18:22 +03:00
{Name: "Name3", DBName: "name3", BindNames: []string{"Name3"}, DataType: schema.String, Tag: `gorm:"<-"`, Creatable: true, Updatable: true, Readable: true},
{Name: "Name4", DBName: "name4", BindNames: []string{"Name4"}, DataType: schema.String, Tag: `gorm:"<-:create"`, Creatable: true, Updatable: false, Readable: true},
{Name: "Name5", DBName: "name5", BindNames: []string{"Name5"}, DataType: schema.String, Tag: `gorm:"<-:update"`, Creatable: false, Updatable: true, Readable: true},
{Name: "Name6", DBName: "name6", BindNames: []string{"Name6"}, DataType: schema.String, Tag: `gorm:"<-:create,update"`, Creatable: true, Updatable: true, Readable: true},
{Name: "Name7", DBName: "name7", BindNames: []string{"Name7"}, DataType: schema.String, Tag: `gorm:"->:false;<-:create,update"`, Creatable: true, Updatable: true, Readable: false},
}
for _, f := range fields {
checkSchemaField(t, user, &f, func(f *schema.Field) {})
}
}