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

Function TestCustomizeColumn

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

Source from the content-addressed store, hash-verified

9)
10
11func TestCustomizeColumn(t *testing.T) {
12 type CustomizeColumn struct {
13 ID int64 `gorm:"column:mapped_id; primary_key:yes"`
14 Name string `gorm:"column:mapped_name"`
15 Date *time.Time `gorm:"column:mapped_time"`
16 }
17
18 DB.Migrator().DropTable(&CustomizeColumn{})
19 DB.AutoMigrate(&CustomizeColumn{})
20
21 expected := "foo"
22 now := time.Now()
23 cc := CustomizeColumn{ID: 666, Name: expected, Date: &now}
24
25 if count := DB.Create(&cc).RowsAffected; count != 1 {
26 t.Error("There should be one record be affected when create record")
27 }
28
29 var cc1 CustomizeColumn
30 DB.First(&cc1, "mapped_name = ?", "foo")
31
32 if cc1.Name != expected {
33 t.Errorf("Failed to query CustomizeColumn")
34 }
35
36 cc.Name = "bar"
37 DB.Save(&cc)
38
39 var cc2 CustomizeColumn
40 DB.First(&cc2, "mapped_id = ?", 666)
41 if cc2.Name != "bar" {
42 t.Errorf("Failed to query CustomizeColumn")
43 }
44}
45
46func TestCustomColumnAndIgnoredFieldClash(t *testing.T) {
47 // Make sure an ignored field does not interfere with another field's custom

Callers

nothing calls this directly

Calls 7

SaveMethod · 0.80
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
CreateMethod · 0.65
ErrorMethod · 0.65
FirstMethod · 0.65

Tested by

no test coverage detected