From 04f049c1dac757c6fb93df863a9585b98fc8661b Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 9 Sep 2021 11:22:55 +0800 Subject: [PATCH] Add tests for RowsAffected --- tests/delete_test.go | 4 ++-- tests/update_test.go | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/delete_test.go b/tests/delete_test.go index abe85b0e..f62cc606 100644 --- a/tests/delete_test.go +++ b/tests/delete_test.go @@ -22,8 +22,8 @@ func TestDelete(t *testing.T) { } } - if err := DB.Delete(&users[1]).Error; err != nil { - t.Errorf("errors happened when delete: %v", err) + if res := DB.Delete(&users[1]); res.Error != nil || res.RowsAffected != 1 { + t.Errorf("errors happened when delete: %v, affected: %v", res.Error, res.RowsAffected) } var result User diff --git a/tests/update_test.go b/tests/update_test.go index 9e5b630e..631d0d6d 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -69,8 +69,10 @@ func TestUpdate(t *testing.T) { } values := map[string]interface{}{"Active": true, "age": 5} - if err := DB.Model(user).Updates(values).Error; err != nil { - t.Errorf("errors happened when update: %v", err) + if res := DB.Model(user).Updates(values); res.Error != nil { + 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 { t.Errorf("Age should equals to 5, but got %v", user.Age) } else if user.Active != true { @@ -131,7 +133,10 @@ func TestUpdates(t *testing.T) { lastUpdatedAt := users[0].UpdatedAt // 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 { t.Errorf("Record should be updated also with map") }