Test empty struct

This commit is contained in:
Jinzhu 2020-05-31 11:19:45 +08:00
parent 536e4d34b0
commit 1e7eb12cba
3 changed files with 31 additions and 4 deletions

View File

@ -153,6 +153,7 @@ func CreateWithReturning(db *gorm.DB) {
} }
if rows.Next() { if rows.Next() {
db.RowsAffected++
err = rows.Scan(values...) err = rows.Scan(values...)
} }
} }

View File

@ -60,6 +60,13 @@ func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder {
c.Build(builder) c.Build(builder)
} }
}, },
"VALUES": func(c clause.Clause, builder clause.Builder) {
if values, ok := c.Expression.(clause.Values); ok && len(values.Columns) == 0 {
builder.WriteString("VALUES()")
return
}
c.Build(builder)
},
} }
} }

View File

@ -9,8 +9,10 @@ import (
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
var user = *GetUser("create", Config{}) var user = *GetUser("create", Config{})
if err := DB.Create(&user).Error; err != nil { if results := DB.Create(&user); results.Error != nil {
t.Fatalf("errors happened when create: %v", err) t.Fatalf("errors happened when create: %v", results.Error)
} else if results.RowsAffected != 1 {
t.Fatalf("rows affected expects: %v, got %v", 1, results.RowsAffected)
} }
if user.ID == 0 { if user.ID == 0 {
@ -68,8 +70,10 @@ func TestBulkCreateWithAssociations(t *testing.T) {
*GetUser("bulk_8", Config{Account: false, Pets: 0, Toys: 0, Company: false, Manager: false, Team: 0, Languages: 0, Friends: 0}), *GetUser("bulk_8", Config{Account: false, Pets: 0, Toys: 0, Company: false, Manager: false, Team: 0, Languages: 0, Friends: 0}),
} }
if err := DB.Create(&users).Error; err != nil { if results := DB.Create(&users); results.Error != nil {
t.Fatalf("errors happened when create: %v", err) t.Fatalf("errors happened when create: %v", results.Error)
} else if results.RowsAffected != int64(len(users)) {
t.Fatalf("rows affected expects: %v, got %v", len(users), results.RowsAffected)
} }
var userIDs []uint var userIDs []uint
@ -182,3 +186,18 @@ func TestPolymorphicHasOne(t *testing.T) {
} }
}) })
} }
func TestCreateEmptyStrut(t *testing.T) {
type EmptyStruct struct {
ID uint
}
DB.Migrator().DropTable(&EmptyStruct{})
if err := DB.AutoMigrate(&EmptyStruct{}); err != nil {
t.Errorf("no error should happen when auto migrate, but got %v", err)
}
if err := DB.Create(&EmptyStruct{}).Error; err != nil {
t.Errorf("No error should happen when creating user, but got %v", err)
}
}