Skip to content

Commit

Permalink
test: FullSaveAssociations test
Browse files Browse the repository at this point in the history
  • Loading branch information
icpd committed Dec 24, 2023
1 parent 6aee054 commit ca3e76f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

var (
Expand Down Expand Up @@ -263,3 +264,50 @@ func TestPostgres(t *testing.T) {
require.Equal(t, int64(2), user.Version.Int64)
require.Equal(t, uint(18), user.Age)
}

func TestFullSaveAssociation(t *testing.T) {
DB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.Nil(t, err)

type Order struct {
ID uint64
Amount int64
Version Version
}
type User struct {
ID uint64
FirstName string
LastName string
Orders []*Order `gorm:"many2many:user_orders"`
Version Version
}

_ = DB.AutoMigrate(&User{}, &Order{})

u := User{
ID: 1000,
FirstName: "aaa",
LastName: "bbb",
Orders: []*Order{
{
ID: 2000,
Amount: 5000,
},
},
}

require.Nil(t, DB.Save(&u).Error)

u.Orders[0].Amount = 6000
u.Orders[0].Version.Int64 = 1000
DB = DB.Session(&gorm.Session{
Logger: DB.Logger.LogMode(logger.Info),
FullSaveAssociations: true,
})
DB.Table("users").Updates(&u)

var o Order
require.Nil(t, DB.First(&o).Error)
require.Equal(t, int64(6000), o.Amount)
require.Equal(t, int64(1000), o.Version.Int64)
}

0 comments on commit ca3e76f

Please sign in to comment.