(t *testing.T)
| 59 | } |
| 60 | |
| 61 | func TestCustomizeField(t *testing.T) { |
| 62 | type CustomizeFieldStruct struct { |
| 63 | gorm.Model |
| 64 | Name string |
| 65 | FieldAllowCreate string `gorm:"<-:create"` |
| 66 | FieldAllowUpdate string `gorm:"<-:update"` |
| 67 | FieldAllowSave string `gorm:"<-"` |
| 68 | FieldAllowSave2 string `gorm:"<-:create,update"` |
| 69 | FieldAllowSave3 string `gorm:"->:false;<-:create"` |
| 70 | FieldReadonly string `gorm:"->"` |
| 71 | FieldIgnore string `gorm:"-"` |
| 72 | AutoUnixCreateTime int32 `gorm:"autocreatetime"` |
| 73 | AutoUnixMilliCreateTime int `gorm:"autocreatetime:milli"` |
| 74 | AutoUnixNanoCreateTime int64 `gorm:"autocreatetime:nano"` |
| 75 | AutoUnixUpdateTime uint32 `gorm:"autoupdatetime"` |
| 76 | AutoUnixMilliUpdateTime int `gorm:"autoupdatetime:milli"` |
| 77 | AutoUnixNanoUpdateTime uint64 `gorm:"autoupdatetime:nano"` |
| 78 | } |
| 79 | |
| 80 | DB.Migrator().DropTable(&CustomizeFieldStruct{}) |
| 81 | |
| 82 | if err := DB.AutoMigrate(&CustomizeFieldStruct{}); err != nil { |
| 83 | t.Errorf("Failed to migrate, got error: %v", err) |
| 84 | } |
| 85 | |
| 86 | if DB.Migrator().HasColumn(&CustomizeFieldStruct{}, "FieldIgnore") { |
| 87 | t.Errorf("FieldIgnore should not be created") |
| 88 | } |
| 89 | |
| 90 | if DB.Migrator().HasColumn(&CustomizeFieldStruct{}, "field_ignore") { |
| 91 | t.Errorf("FieldIgnore should not be created") |
| 92 | } |
| 93 | |
| 94 | generateStruct := func(name string) *CustomizeFieldStruct { |
| 95 | return &CustomizeFieldStruct{ |
| 96 | Name: name, |
| 97 | FieldAllowCreate: name + "_allow_create", |
| 98 | FieldAllowUpdate: name + "_allow_update", |
| 99 | FieldAllowSave: name + "_allow_save", |
| 100 | FieldAllowSave2: name + "_allow_save2", |
| 101 | FieldAllowSave3: name + "_allow_save3", |
| 102 | FieldReadonly: name + "_allow_readonly", |
| 103 | FieldIgnore: name + "_allow_ignore", |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | create := generateStruct("create") |
| 108 | DB.Create(&create) |
| 109 | |
| 110 | var result CustomizeFieldStruct |
| 111 | DB.Find(&result, "name = ?", "create") |
| 112 | |
| 113 | AssertObjEqual(t, result, create, "Name", "FieldAllowCreate", "FieldAllowSave", "FieldAllowSave2") |
| 114 | |
| 115 | if result.FieldAllowUpdate != "" || result.FieldReadonly != "" || result.FieldIgnore != "" || result.FieldAllowSave3 != "" { |
| 116 | t.Fatalf("invalid result: %#v", result) |
| 117 | } |
| 118 |
nothing calls this directly
no test coverage detected