NO-ISSUE:  Verify that GORM embedded structure is always part of containing structure
There are locations that our code assumes that embedded structure is
always not nil.  This change adds tests to verify it.
For more information:
@@ -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 = `
@@ -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())
