| 9 | ) |
| 10 | |
| 11 | func 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 | |
| 46 | func TestCustomColumnAndIgnoredFieldClash(t *testing.T) { |
| 47 | // Make sure an ignored field does not interfere with another field's custom |