Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO-ISSUE: Verify that GORM embedded structure is always part of containing structure #4730

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions internal/common/common_test.go
Original file line number Diff line number Diff line change
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