From bd0aed633e55d65cb00f02c75a1891574fd887ac Mon Sep 17 00:00:00 2001 From: Marsel Arduanov Date: Mon, 9 Jan 2017 00:24:47 +0500 Subject: [PATCH] BeforeSave test failed BeforeSave event called but struct not changed Sql update is broken, because doesn't have id column ``` INSERT INTO "uuid_structs" ("name") VALUES ('') ``` --- main_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/main_test.go b/main_test.go index 9869a7ad..4aae7fbd 100644 --- a/main_test.go +++ b/main_test.go @@ -95,6 +95,27 @@ func TestStringPrimaryKey(t *testing.T) { } } +type UUIDStruct struct { + ID string `gorm:"primary_key"` + Name string +} + +func (u *UUIDStruct) BeforeSave() { + u.ID = "new_uuid" + return +} + +func TestBeforeSaveGenerateId(t *testing.T) { + + DB.DropTable(&UUIDStruct{}) + DB.AutoMigrate(&UUIDStruct{}) + + data := UUIDStruct{} + if err := DB.Save(&data).Error; err != nil || data.ID != "new_uuid" { + t.Errorf("string primary key should be changed") + } +} + func TestExceptionsWithInvalidSql(t *testing.T) { var columns []string if DB.Where("sdsd.zaaa = ?", "sd;;;aa").Pluck("aaa", &columns).Error == nil {