Skip to content

Commit

Permalink
NO-ISSUE: Verify that GORM embedded structure is always part of conta…
Browse files Browse the repository at this point in the history
…ining structure (#4730)

There are locations that our code assumes that embedded structure is
always not nil.  This change adds tests to verify it.
For more information: go-gorm/gorm#5431
  • Loading branch information
ori-amizur committed Dec 6, 2022
1 parent e6b4ec0 commit 75e3dbe
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/common/common_test.go
Expand Up @@ -11,6 +11,7 @@ import (
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/openshift/assisted-service/models"
"gorm.io/gorm"
)

const redHatIntermediateChain string = `
Expand Down Expand Up @@ -316,6 +317,47 @@ var _ = Describe("Test GetInventoryInterfaces", func() {
})
})

var _ = Describe("db features", func() {
var (
db *gorm.DB
)
BeforeEach(func() {
db, _ = PrepareTestDB()
})
AfterEach(func() {
CloseDB(db)
})
Context("embedded struct", func() {
type inner struct {
String *string
Int int
}
type Outer struct {
Inner *inner `gorm:"embedded;embeddedPrefix:inner_"`
ID int
}
BeforeEach(func() {
Expect(db.Migrator().AutoMigrate(&Outer{})).ToNot(HaveOccurred())
})
It("default embedded struct is not nil", func() {
Expect(db.Create(&Outer{ID: 1}).Error).ToNot(HaveOccurred())
var outer Outer
Expect(db.Where("inner_string is null and inner_int is null").Take(&outer).Error).ToNot(HaveOccurred())
Expect(outer.Inner).ToNot(BeNil())
Expect(outer.Inner.Int).To(Equal(0))
Expect(outer.Inner.String).To(BeNil())
})
It("embedded struct with values", func() {
Expect(db.Create(&Outer{ID: 1, Inner: &inner{String: swag.String("blah")}}).Error).ToNot(HaveOccurred())
var outer Outer
Expect(db.Where("inner_string = 'blah'").Take(&outer).Error).ToNot(HaveOccurred())
Expect(outer.Inner).ToNot(BeNil())
Expect(outer.Inner.Int).To(Equal(0))
Expect(outer.Inner.String).To(Equal(swag.String("blah")))
})
})
})

func createHost(hostRole models.HostRole, state string) *models.Host {
hostId := strfmt.UUID(uuid.New().String())
clusterId := strfmt.UUID(uuid.New().String())
Expand Down

0 comments on commit 75e3dbe

Please sign in to comment.