2020-05-30 11:03:27 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
. "gorm.io/gorm/tests"
|
2020-05-30 11:03:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Animal struct {
|
2020-05-30 19:42:52 +03:00
|
|
|
Counter uint64 `gorm:"primary_key:yes"`
|
|
|
|
Name string `gorm:"DEFAULT:'galeone'"`
|
|
|
|
From string //test reserved sql keyword as field name
|
|
|
|
Age *time.Time
|
|
|
|
unexported string // unexported value
|
2020-05-30 11:03:27 +03:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
2020-05-30 19:42:52 +03:00
|
|
|
func TestNonStdPrimaryKeyAndDefaultValues(t *testing.T) {
|
2020-05-30 11:03:27 +03:00
|
|
|
DB.Migrator().DropTable(&Animal{})
|
2020-05-30 19:42:52 +03:00
|
|
|
if err := DB.AutoMigrate(&Animal{}); err != nil {
|
|
|
|
t.Fatalf("no error should happen when migrate but got %v", err)
|
|
|
|
}
|
2020-05-30 11:03:27 +03:00
|
|
|
|
|
|
|
animal := Animal{Name: "Ferdinand"}
|
|
|
|
DB.Save(&animal)
|
|
|
|
updatedAt1 := animal.UpdatedAt
|
|
|
|
|
|
|
|
DB.Save(&animal).Update("name", "Francis")
|
|
|
|
if updatedAt1.Format(time.RFC3339Nano) == animal.UpdatedAt.Format(time.RFC3339Nano) {
|
|
|
|
t.Errorf("UpdatedAt should be updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
var animals []Animal
|
|
|
|
DB.Find(&animals)
|
2020-05-31 15:42:07 +03:00
|
|
|
if count := DB.Model(Animal{}).Where("1=1").Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(animals)) {
|
2020-05-30 11:03:27 +03:00
|
|
|
t.Error("RowsAffected should be correct when do batch update")
|
|
|
|
}
|
|
|
|
|
|
|
|
animal = Animal{From: "somewhere"} // No name fields, should be filled with the default value (galeone)
|
|
|
|
DB.Save(&animal).Update("From", "a nice place") // The name field shoul be untouched
|
|
|
|
DB.First(&animal, animal.Counter)
|
|
|
|
if animal.Name != "galeone" {
|
|
|
|
t.Errorf("Name fields shouldn't be changed if untouched, but got %v", animal.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When changing a field with a default value, the change must occur
|
|
|
|
animal.Name = "amazing horse"
|
|
|
|
DB.Save(&animal)
|
|
|
|
DB.First(&animal, animal.Counter)
|
|
|
|
if animal.Name != "amazing horse" {
|
|
|
|
t.Errorf("Update a filed with a default value should occur. But got %v\n", animal.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When changing a field with a default value with blank value
|
|
|
|
animal.Name = ""
|
|
|
|
DB.Save(&animal)
|
|
|
|
DB.First(&animal, animal.Counter)
|
|
|
|
if animal.Name != "" {
|
|
|
|
t.Errorf("Update a filed to blank with a default value should occur. But got %v\n", animal.Name)
|
|
|
|
}
|
|
|
|
}
|