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

Function TestCustomizeField

tests/customize_field_test.go:61–192  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

59}
60
61func 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

Callers

nothing calls this directly

Calls 13

AssertObjEqualFunction · 0.85
SaveMethod · 0.80
UpdateColumnMethod · 0.80
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
HasColumnMethod · 0.65
CreateMethod · 0.65
FindMethod · 0.65
FirstMethod · 0.65
WhereMethod · 0.65
TableMethod · 0.65

Tested by

no test coverage detected