Add tests for RowsAffected

This commit is contained in:
Jinzhu 2021-09-09 11:22:55 +08:00
parent a16db07945
commit 04f049c1da
2 changed files with 10 additions and 5 deletions

View File

@ -22,8 +22,8 @@ func TestDelete(t *testing.T) {
} }
} }
if err := DB.Delete(&users[1]).Error; err != nil { if res := DB.Delete(&users[1]); res.Error != nil || res.RowsAffected != 1 {
t.Errorf("errors happened when delete: %v", err) t.Errorf("errors happened when delete: %v, affected: %v", res.Error, res.RowsAffected)
} }
var result User var result User

View File

@ -69,8 +69,10 @@ func TestUpdate(t *testing.T) {
} }
values := map[string]interface{}{"Active": true, "age": 5} values := map[string]interface{}{"Active": true, "age": 5}
if err := DB.Model(user).Updates(values).Error; err != nil { if res := DB.Model(user).Updates(values); res.Error != nil {
t.Errorf("errors happened when update: %v", err) t.Errorf("errors happened when update: %v", res.Error)
} else if res.RowsAffected != 1 {
t.Errorf("rows affected should be 1, but got : %v", res.RowsAffected)
} else if user.Age != 5 { } else if user.Age != 5 {
t.Errorf("Age should equals to 5, but got %v", user.Age) t.Errorf("Age should equals to 5, but got %v", user.Age)
} else if user.Active != true { } else if user.Active != true {
@ -131,7 +133,10 @@ func TestUpdates(t *testing.T) {
lastUpdatedAt := users[0].UpdatedAt lastUpdatedAt := users[0].UpdatedAt
// update with map // update with map
DB.Model(users[0]).Updates(map[string]interface{}{"name": "updates_01_newname", "age": 100}) if res := DB.Model(users[0]).Updates(map[string]interface{}{"name": "updates_01_newname", "age": 100}); res.Error != nil || res.RowsAffected != 1 {
t.Errorf("Failed to update users")
}
if users[0].Name != "updates_01_newname" || users[0].Age != 100 { if users[0].Name != "updates_01_newname" || users[0].Age != 100 {
t.Errorf("Record should be updated also with map") t.Errorf("Record should be updated also with map")
} }