Test Select with Update Struct

This commit is contained in:
Jinzhu 2020-07-06 11:20:43 +08:00
parent 9a4941ba70
commit b5725940e9
3 changed files with 40 additions and 11 deletions

View File

@ -196,6 +196,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
if !stmt.UpdatingColumn && stmt.Schema != nil { if !stmt.UpdatingColumn && stmt.Schema != nil {
for _, field := range stmt.Schema.FieldsByDBName { for _, field := range stmt.Schema.FieldsByDBName {
if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil { if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil {
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
now := stmt.DB.NowFunc() now := stmt.DB.NowFunc()
assignValue(field, now) assignValue(field, now)
@ -209,6 +210,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) {
} }
} }
} }
}
default: default:
switch updatingValue.Kind() { switch updatingValue.Kind() {
case reflect.Struct: case reflect.Struct:

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/utils"
. "gorm.io/gorm/utils/tests" . "gorm.io/gorm/utils/tests"
) )
@ -267,6 +268,22 @@ func TestSelectWithUpdate(t *testing.T) {
}) })
AssertObjEqual(t, result2, result, "Name", "Account", "Toys", "Manager", "ManagerID", "Languages") AssertObjEqual(t, result2, result, "Name", "Account", "Toys", "Manager", "ManagerID", "Languages")
DB.Model(&result).Select("Name", "Age").Updates(User{Name: "update_with_select"})
if result.Age != 0 || result.Name != "update_with_select" {
t.Fatalf("Failed to update struct with select, got %+v", result)
}
AssertObjEqual(t, result, user, "UpdatedAt")
var result3 User
DB.First(&result3, result.ID)
AssertObjEqual(t, result, result3, "Name", "Age", "UpdatedAt")
DB.Model(&result).Select("Name", "Age", "UpdatedAt").Updates(User{Name: "update_with_select"})
if utils.AssertEqual(result.UpdatedAt, user.UpdatedAt) {
t.Fatalf("Update struct should update UpdatedAt, was %+v, got %+v", result.UpdatedAt, user.UpdatedAt)
}
} }
func TestSelectWithUpdateWithMap(t *testing.T) { func TestSelectWithUpdateWithMap(t *testing.T) {
@ -290,7 +307,7 @@ func TestSelectWithUpdateWithMap(t *testing.T) {
"Friends": user2.Friends, "Friends": user2.Friends,
} }
DB.Model(&result).Select("Name", "Account", "Toys", "Manager", "ManagerID", "Languages").Updates(updateValues) DB.Model(&result).Omit("name", "updated_at").Updates(updateValues)
var result2 User var result2 User
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").First(&result2, user.ID) DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").First(&result2, user.ID)
@ -427,11 +444,16 @@ func TestSelectWithUpdateColumn(t *testing.T) {
var result User var result User
DB.First(&result, user.ID) DB.First(&result, user.ID)
DB.Model(&result).Select("Name").UpdateColumns(updateValues)
time.Sleep(time.Second)
lastUpdatedAt := result.UpdatedAt
DB.Model(&result).Select("Name").Updates(updateValues)
var result2 User var result2 User
DB.First(&result2, user.ID) DB.First(&result2, user.ID)
AssertEqual(t, lastUpdatedAt, result2.UpdatedAt)
if result2.Name == user.Name || result2.Age != user.Age { if result2.Name == user.Name || result2.Age != user.Age {
t.Errorf("Should only update users with name column") t.Errorf("Should only update users with name column")
} }

View File

@ -84,17 +84,22 @@ func AssertEqual(t *testing.T, got, expect interface{}) {
if reflect.ValueOf(got).Kind() == reflect.Struct { if reflect.ValueOf(got).Kind() == reflect.Struct {
if reflect.ValueOf(got).NumField() == reflect.ValueOf(expect).NumField() { if reflect.ValueOf(got).NumField() == reflect.ValueOf(expect).NumField() {
exported := false
for i := 0; i < reflect.ValueOf(got).NumField(); i++ { for i := 0; i < reflect.ValueOf(got).NumField(); i++ {
if fieldStruct := reflect.ValueOf(got).Type().Field(i); ast.IsExported(fieldStruct.Name) { if fieldStruct := reflect.ValueOf(got).Type().Field(i); ast.IsExported(fieldStruct.Name) {
exported = true
field := reflect.ValueOf(got).Field(i) field := reflect.ValueOf(got).Field(i)
t.Run(fieldStruct.Name, func(t *testing.T) { t.Run(fieldStruct.Name, func(t *testing.T) {
AssertEqual(t, field.Interface(), reflect.ValueOf(expect).Field(i).Interface()) AssertEqual(t, field.Interface(), reflect.ValueOf(expect).Field(i).Interface())
}) })
} }
} }
if exported {
return return
} }
} }
}
if reflect.ValueOf(got).Type().ConvertibleTo(reflect.ValueOf(expect).Type()) { if reflect.ValueOf(got).Type().ConvertibleTo(reflect.ValueOf(expect).Type()) {
got = reflect.ValueOf(got).Convert(reflect.ValueOf(expect).Type()).Interface() got = reflect.ValueOf(got).Convert(reflect.ValueOf(expect).Type()).Interface()