gorm/tests/delete_test.go

71 lines
2.1 KiB
Go
Raw Normal View History

2020-05-23 18:50:48 +03:00
package tests_test
import (
"errors"
"testing"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
. "gorm.io/gorm/tests"
2020-05-23 18:50:48 +03:00
)
func TestDelete(t *testing.T) {
var users = []User{*GetUser("delete", Config{}), *GetUser("delete", Config{}), *GetUser("delete", Config{})}
if err := DB.Create(&users).Error; err != nil {
t.Errorf("errors happened when create: %v", err)
}
for _, user := range users {
if user.ID == 0 {
t.Fatalf("user's primary key should has value after create, got : %v", user.ID)
}
}
if err := DB.Delete(&users[1]).Error; err != nil {
t.Errorf("errors happened when delete: %v", err)
}
var result User
if err := DB.Where("id = ?", users[1].ID).First(&result).Error; err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("should returns record not found error, but got %v", err)
}
for _, user := range []User{users[0], users[2]} {
2020-06-01 14:41:33 +03:00
result = User{}
2020-05-23 18:50:48 +03:00
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)
}
}
for _, user := range []User{users[0], users[2]} {
2020-06-01 14:41:33 +03:00
result = User{}
2020-05-23 18:50:48 +03:00
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)
}
}
}
2020-05-30 17:27:20 +03:00
func TestInlineCondDelete(t *testing.T) {
user1 := *GetUser("inline_delete_1", Config{})
user2 := *GetUser("inline_delete_2", Config{})
DB.Save(&user1).Save(&user2)
if DB.Delete(&User{}, user1.ID).Error != nil {
t.Errorf("No error should happen when delete a record")
} else if !DB.Where("name = ?", user1.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}
if err := DB.Delete(&User{}, "name = ?", user2.Name).Error; err != nil {
t.Errorf("No error should happen when delete a record, err=%s", err)
} else if !DB.Where("name = ?", user2.Name).First(&User{}).RecordNotFound() {
t.Errorf("User can't be found after delete")
}
}
2020-05-31 15:42:07 +03:00
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")
}
}