Skip to content

Commit

Permalink
Test block global update/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed May 31, 2020
1 parent 5b1d3e4 commit e26abb8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
5 changes: 5 additions & 0 deletions callbacks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func Update(db *gorm.DB) {
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...)

if err == nil {
Expand Down
10 changes: 6 additions & 4 deletions tests/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]} {
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)
Expand All @@ -64,3 +60,9 @@ func TestInlineCondDelete(t *testing.T) {
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")
}
}
18 changes: 14 additions & 4 deletions tests/joins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,26 @@ func TestJoinConds(t *testing.T) {

func TestJoinsWithSelect(t *testing.T) {
type result struct {
ID uint
Name string
ID uint
PetID uint
Name string
}

user := *GetUser("joins_with_select", Config{Pets: 2})
DB.Save(&user)

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 {
t.Errorf("Should find all two pets with Join select")
t.Errorf("Should find all two pets with Join select, got %+v", results)
}
}
14 changes: 14 additions & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ func TestExceptionsWithInvalidSql(t *testing.T) {
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")
}
}
2 changes: 1 addition & 1 deletion tests/non_std_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNonStdPrimaryKeyAndDefaultValues(t *testing.T) {

var animals []Animal
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")
}

Expand Down
7 changes: 7 additions & 0 deletions tests/update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests_test

import (
"errors"
"testing"
"time"

Expand Down Expand Up @@ -211,3 +212,9 @@ func TestUpdateColumn(t *testing.T) {
CheckUser(t, user5, *users[0])
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)
}
}

0 comments on commit e26abb8

Please sign in to comment.