Test block global update/delete

This commit is contained in:
Jinzhu 2020-05-31 20:42:07 +08:00
parent 5b1d3e4a77
commit e26abb84b3
6 changed files with 47 additions and 9 deletions

View File

@ -59,6 +59,11 @@ func Update(db *gorm.DB) {
db.Statement.Build("UPDATE", "SET", "WHERE") db.Statement.Build("UPDATE", "SET", "WHERE")
} }
if _, ok := db.Statement.Clauses["WHERE"]; !ok {
db.AddError(gorm.ErrMissingWhereClause)
return
}
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...) result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
if err == nil { if err == nil {

View File

@ -36,10 +36,6 @@ func TestDelete(t *testing.T) {
} }
} }
if err := DB.Delete(&User{}).Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) {
t.Errorf("should returns missing WHERE clause while deleting error")
}
for _, user := range []User{users[0], users[2]} { for _, user := range []User{users[0], users[2]} {
if err := DB.Where("id = ?", user.ID).First(&result).Error; err != nil { if err := DB.Where("id = ?", user.ID).First(&result).Error; err != nil {
t.Errorf("no error should returns when query %v, but got %v", user.ID, err) t.Errorf("no error should returns when query %v, but got %v", user.ID, err)
@ -64,3 +60,9 @@ func TestInlineCondDelete(t *testing.T) {
t.Errorf("User can't be found after delete") t.Errorf("User can't be found after delete")
} }
} }
func TestBlockGlobalDelete(t *testing.T) {
if err := DB.Delete(&User{}).Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) {
t.Errorf("should returns missing WHERE clause while deleting error")
}
}

View File

@ -92,16 +92,26 @@ func TestJoinConds(t *testing.T) {
func TestJoinsWithSelect(t *testing.T) { func TestJoinsWithSelect(t *testing.T) {
type result struct { type result struct {
ID uint ID uint
Name string PetID uint
Name string
} }
user := *GetUser("joins_with_select", Config{Pets: 2}) user := *GetUser("joins_with_select", Config{Pets: 2})
DB.Save(&user) DB.Save(&user)
var results []result var results []result
DB.Table("users").Select("users.id, pets.name").Joins("left join pets on pets.user_id = users.id").Where("users.name = ?", "joins_with_select").Scan(&results) DB.Table("users").Select("users.id, pets.id as pet_id, pets.name").Joins("left join pets on pets.user_id = users.id").Where("users.name = ?", "joins_with_select").Scan(&results)
sort.Slice(results, func(i, j int) bool {
return results[i].PetID > results[j].PetID
})
sort.Slice(results, func(i, j int) bool {
return user.Pets[i].ID > user.Pets[j].ID
})
if len(results) != 2 || results[0].Name != user.Pets[0].Name || results[1].Name != user.Pets[1].Name { if len(results) != 2 || results[0].Name != user.Pets[0].Name || results[1].Name != user.Pets[1].Name {
t.Errorf("Should find all two pets with Join select") t.Errorf("Should find all two pets with Join select, got %+v", results)
} }
} }

View File

@ -35,3 +35,17 @@ func TestExceptionsWithInvalidSql(t *testing.T) {
t.Errorf("No user should not be deleted by invalid SQL") t.Errorf("No user should not be deleted by invalid SQL")
} }
} }
func TestSetAndGet(t *testing.T) {
if value, ok := DB.Set("hello", "world").Get("hello"); !ok {
t.Errorf("Should be able to get setting after set")
} else {
if value.(string) != "world" {
t.Errorf("Setted value should not be changed")
}
}
if _, ok := DB.Get("non_existing"); ok {
t.Errorf("Get non existing key should return error")
}
}

View File

@ -34,7 +34,7 @@ func TestNonStdPrimaryKeyAndDefaultValues(t *testing.T) {
var animals []Animal var animals []Animal
DB.Find(&animals) DB.Find(&animals)
if count := DB.Model(Animal{}).Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(animals)) { if count := DB.Model(Animal{}).Where("1=1").Update("CreatedAt", time.Now().Add(2*time.Hour)).RowsAffected; count != int64(len(animals)) {
t.Error("RowsAffected should be correct when do batch update") t.Error("RowsAffected should be correct when do batch update")
} }

View File

@ -1,6 +1,7 @@
package tests_test package tests_test
import ( import (
"errors"
"testing" "testing"
"time" "time"
@ -211,3 +212,9 @@ func TestUpdateColumn(t *testing.T) {
CheckUser(t, user5, *users[0]) CheckUser(t, user5, *users[0])
CheckUser(t, user6, *users[1]) CheckUser(t, user6, *users[1])
} }
func TestBlockGlobalUpdate(t *testing.T) {
if err := DB.Model(&User{}).Update("name", "jinzhu").Error; err == nil || !errors.Is(err, gorm.ErrMissingWhereClause) {
t.Errorf("should returns missing WHERE clause while updating error, got err %v", err)
}
}