Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
gorm.io/driver/postgres v1.5.3-0.20230607070428-18bc84b75196
gorm.io/driver/sqlite v1.5.4
gorm.io/driver/sqlserver v1.5.2-0.20230613072041-6e2cde390b0a
gorm.io/gorm v1.25.4
gorm.io/gorm v1.25.5
)

require (
Expand Down
49 changes: 49 additions & 0 deletions tests/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,52 @@ func TestSaveWithHooks(t *testing.T) {
t.Errorf(`token content should be "token2_encrypted", but got: "%s"`, o2.Token.Content)
}
}

// only postgres, sqlserver, sqlite support update from
func TestUpdateFrom(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test failed when running all test cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jinzhu It seems that the problem is not these tests, but the new versions of the drivers that were taged and are used to run the tests in the ci (in master the tests passed because the ci was run before the new tags were created). For the postgree driver the v1.5.3 tag was created but the tests do not pass using this version (they do not pass because of the changes in the commit go-gorm/postgres@7ba909e specifically). In the same way, in the mysql driver the v1.5.2 tag was created but the tests don't pass since the changes of the commit go-gorm/mysql@2a61ba0. In my fork (https://github.com/ditrit/gorm) you can see the master tests do not pass with the new driver tags

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrancoLiberali The test failure has nothing to do with this PR. You seem to have investigated the cause of the failure. Are you interested in creating other PRs to fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a631807682 To be honest, I only ran the tests on the different driver's commits to find out from which one the tests don't work, but I don't know what is the purpose of the changes in those commits to fix the generated problems

if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "sqlserver" {
return
}

users := []*User{
GetUser("update-from-1", Config{Account: true}),
GetUser("update-from-2", Config{Account: true}),
GetUser("update-from-3", Config{}),
}

if err := DB.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
} else if users[0].ID == 0 {
t.Fatalf("user's primary value should not zero, %v", users[0].ID)
} else if users[0].UpdatedAt.IsZero() {
t.Fatalf("user's updated at should not zero, %v", users[0].UpdatedAt)
}

if rowsAffected := DB.Model(&User{}).Clauses(clause.From{Tables: []clause.Table{{Name: "accounts"}}}).Where("accounts.user_id = users.id AND accounts.number = ? AND accounts.deleted_at IS NULL", users[0].Account.Number).Update("name", "franco").RowsAffected; rowsAffected != 1 {
t.Errorf("should only update one record, but got %v", rowsAffected)
}

var result User
if err := DB.Where("id = ?", users[0].ID).First(&result).Error; err != nil {
t.Errorf("errors happened when query before user: %v", err)
} else if result.UpdatedAt.UnixNano() == users[0].UpdatedAt.UnixNano() {
t.Errorf("user's updated at should be changed, but got %v, was %v", result.UpdatedAt, users[0].UpdatedAt)
} else if result.Name != "franco" {
t.Errorf("user's name should be updated")
}

if rowsAffected := DB.Model(&User{}).Clauses(clause.From{Tables: []clause.Table{{Name: "accounts"}}}).Where("accounts.user_id = users.id AND accounts.number IN ? AND accounts.deleted_at IS NULL", []string{users[0].Account.Number, users[1].Account.Number}).Update("name", gorm.Expr("accounts.number")).RowsAffected; rowsAffected != 2 {
t.Errorf("should update two records, but got %v", rowsAffected)
}

var results []User
if err := DB.Preload("Account").Find(&results, []uint{users[0].ID, users[1].ID}).Error; err != nil {
t.Errorf("Not error should happen when finding users, but got %v", err)
}

for _, user := range results {
if user.Name != user.Account.Number {
t.Errorf("user's name should be equal to the account's number %v, but got %v", user.Account.Number, user.Name)
}
}
}