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"
|
2020-09-10 16:46:18 +03:00
|
|
|
"gorm.io/gorm/clause"
|
2020-06-02 05:34:50 +03:00
|
|
|
. "gorm.io/gorm/utils/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-08-06 12:48:46 +03:00
|
|
|
|
2020-12-15 05:39:20 +03:00
|
|
|
if err := DB.Delete(&users[0]).Error; err != nil {
|
2020-08-06 12:48:46 +03:00
|
|
|
t.Errorf("errors happened when delete: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-05 06:20:08 +03:00
|
|
|
if err := DB.Delete(&User{}).Error; err != gorm.ErrMissingWhereClause {
|
2020-08-25 12:27:28 +03:00
|
|
|
t.Errorf("errors happened when delete: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:48:46 +03:00
|
|
|
if err := DB.Where("id = ?", users[0].ID).First(&result).Error; err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
t.Errorf("should returns record not found error, but got %v", err)
|
|
|
|
}
|
2020-05-23 18:50:48 +03:00
|
|
|
}
|
2020-05-30 17:27:20 +03:00
|
|
|
|
2020-07-10 08:08:15 +03:00
|
|
|
func TestDeleteWithTable(t *testing.T) {
|
|
|
|
type UserWithDelete struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.Table("deleted_users").Migrator().DropTable(UserWithDelete{})
|
|
|
|
DB.Table("deleted_users").AutoMigrate(UserWithDelete{})
|
|
|
|
|
|
|
|
user := UserWithDelete{Name: "delete1"}
|
|
|
|
DB.Table("deleted_users").Create(&user)
|
|
|
|
|
|
|
|
var result UserWithDelete
|
|
|
|
if err := DB.Table("deleted_users").First(&result).Error; err != nil {
|
|
|
|
t.Errorf("failed to find deleted user, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
AssertEqual(t, result, user)
|
|
|
|
|
|
|
|
if err := DB.Table("deleted_users").Delete(&result).Error; err != nil {
|
|
|
|
t.Errorf("failed to delete user, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result2 UserWithDelete
|
|
|
|
if err := DB.Table("deleted_users").First(&result2, user.ID).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
t.Errorf("should raise record not found error, but got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result3 UserWithDelete
|
|
|
|
if err := DB.Table("deleted_users").Unscoped().First(&result3, user.ID).Error; err != nil {
|
|
|
|
t.Fatalf("failed to find record, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Table("deleted_users").Unscoped().Delete(&result).Error; err != nil {
|
|
|
|
t.Errorf("failed to delete user with unscoped, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result4 UserWithDelete
|
|
|
|
if err := DB.Table("deleted_users").Unscoped().First(&result4, user.ID).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
t.Errorf("should raise record not found error, but got error %v", 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")
|
2020-06-07 17:03:45 +03:00
|
|
|
} else if err := DB.Where("name = ?", user1.Name).First(&User{}).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
|
2020-05-30 17:27:20 +03:00
|
|
|
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)
|
2020-06-07 17:03:45 +03:00
|
|
|
} else if err := DB.Where("name = ?", user2.Name).First(&User{}).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
|
2020-05-30 17:27:20 +03:00
|
|
|
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")
|
|
|
|
}
|
2020-08-23 15:08:23 +03:00
|
|
|
|
|
|
|
if err := DB.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&User{}).Error; err != nil {
|
|
|
|
t.Errorf("should returns no error while enable global update, but got err %v", err)
|
|
|
|
}
|
2020-05-31 15:42:07 +03:00
|
|
|
}
|
2020-09-10 16:46:18 +03:00
|
|
|
|
|
|
|
func TestDeleteWithAssociations(t *testing.T) {
|
|
|
|
user := GetUser("delete_with_associations", Config{Account: true, Pets: 2, Toys: 4, Company: true, Manager: true, Team: 1, Languages: 1, Friends: 1})
|
|
|
|
|
|
|
|
if err := DB.Create(user).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create user, got error %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 09:28:26 +03:00
|
|
|
if err := DB.Select(clause.Associations, "Pets.Toy").Delete(&user).Error; err != nil {
|
2020-09-10 16:46:18 +03:00
|
|
|
t.Fatalf("failed to delete user, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range map[string]int64{"Account": 1, "Pets": 2, "Toys": 4, "Company": 1, "Manager": 1, "Team": 1, "Languages": 0, "Friends": 0} {
|
|
|
|
if count := DB.Unscoped().Model(&user).Association(key).Count(); count != value {
|
|
|
|
t.Errorf("user's %v expects: %v, got %v", key, value, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 1, "Manager": 1, "Team": 0, "Languages": 0, "Friends": 0} {
|
|
|
|
if count := DB.Model(&user).Association(key).Count(); count != value {
|
|
|
|
t.Errorf("user's %v expects: %v, got %v", key, value, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteSliceWithAssociations(t *testing.T) {
|
|
|
|
users := []User{
|
|
|
|
*GetUser("delete_slice_with_associations1", Config{Account: true, Pets: 4, Toys: 1, Company: true, Manager: true, Team: 1, Languages: 1, Friends: 4}),
|
|
|
|
*GetUser("delete_slice_with_associations2", Config{Account: true, Pets: 3, Toys: 2, Company: true, Manager: true, Team: 2, Languages: 2, Friends: 3}),
|
|
|
|
*GetUser("delete_slice_with_associations3", Config{Account: true, Pets: 2, Toys: 3, Company: true, Manager: true, Team: 3, Languages: 3, Friends: 2}),
|
|
|
|
*GetUser("delete_slice_with_associations4", Config{Account: true, Pets: 1, Toys: 4, Company: true, Manager: true, Team: 4, Languages: 4, Friends: 1}),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Create(users).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create user, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Select(clause.Associations).Delete(&users).Error; err != nil {
|
|
|
|
t.Fatalf("failed to delete user, got error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range map[string]int64{"Account": 4, "Pets": 10, "Toys": 10, "Company": 4, "Manager": 4, "Team": 10, "Languages": 0, "Friends": 0} {
|
|
|
|
if count := DB.Unscoped().Model(&users).Association(key).Count(); count != value {
|
|
|
|
t.Errorf("user's %v expects: %v, got %v", key, value, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 4, "Manager": 4, "Team": 0, "Languages": 0, "Friends": 0} {
|
|
|
|
if count := DB.Model(&users).Association(key).Count(); count != value {
|
|
|
|
t.Errorf("user's %v expects: %v, got %v", key, value, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|