fix test...

This commit is contained in:
black 2023-03-10 20:25:54 +08:00
parent 8bc0a78dd7
commit f158060716
2 changed files with 20 additions and 22 deletions

View File

@ -82,9 +82,7 @@ func embeddedValues(embeddedRelations *schema.Relationships) []string {
return names return names
} }
func preloadEmbedded( func preloadEmbedded(tx *gorm.DB, relationships *schema.Relationships, s *schema.Schema, preloads map[string][]interface{}, as []interface{}) error {
tx *gorm.DB, relationships *schema.Relationships, s *schema.Schema, preloads map[string][]interface{}, as []interface{},
) error {
if relationships == nil { if relationships == nil {
return nil return nil
} }

View File

@ -312,33 +312,33 @@ func TestEmbedPreload(t *testing.T) {
ID int `gorm:"primaryKey"` ID int `gorm:"primaryKey"`
Name string Name string
} }
type Address struct { type EmbeddedAddress struct {
ID int ID int
Name string Name string
CountryID *int CountryID *int
Country *Country Country *Country
} }
type NestedAddress struct { type NestedAddress struct {
Address EmbeddedAddress
} }
type Org struct { type Org struct {
ID int ID int
PostalAddress Address `gorm:"embedded;embeddedPrefix:postal_address_"` PostalAddress EmbeddedAddress `gorm:"embedded;embeddedPrefix:postal_address_"`
VisitingAddress Address `gorm:"embedded;embeddedPrefix:visiting_address_"` VisitingAddress EmbeddedAddress `gorm:"embedded;embeddedPrefix:visiting_address_"`
AddressID *int AddressID *int
Address *Address Address *EmbeddedAddress
NestedAddress NestedAddress `gorm:"embedded;embeddedPrefix:nested_address_"` NestedAddress NestedAddress `gorm:"embedded;embeddedPrefix:nested_address_"`
} }
DB.Migrator().DropTable(&Org{}, &Address{}, &Country{}) DB.Migrator().DropTable(&Org{}, &EmbeddedAddress{}, &Country{})
DB.AutoMigrate(&Org{}, &Address{}, &Country{}) DB.AutoMigrate(&Org{}, &EmbeddedAddress{}, &Country{})
org := Org{ org := Org{
PostalAddress: Address{Name: "a1", Country: &Country{Name: "c1"}}, PostalAddress: EmbeddedAddress{Name: "a1", Country: &Country{Name: "c1"}},
VisitingAddress: Address{Name: "a2", Country: &Country{Name: "c2"}}, VisitingAddress: EmbeddedAddress{Name: "a2", Country: &Country{Name: "c2"}},
Address: &Address{Name: "a3", Country: &Country{Name: "c3"}}, Address: &EmbeddedAddress{Name: "a3", Country: &Country{Name: "c3"}},
NestedAddress: NestedAddress{ NestedAddress: NestedAddress{
Address: Address{Name: "a4", Country: &Country{Name: "c4"}}, EmbeddedAddress: EmbeddedAddress{Name: "a4", Country: &Country{Name: "c4"}},
}, },
} }
if err := DB.Create(&org).Error; err != nil { if err := DB.Create(&org).Error; err != nil {
@ -355,13 +355,13 @@ func TestEmbedPreload(t *testing.T) {
preloads: map[string][]interface{}{"Address.Country": {}}, preloads: map[string][]interface{}{"Address.Country": {}},
expect: Org{ expect: Org{
ID: org.ID, ID: org.ID,
PostalAddress: Address{ PostalAddress: EmbeddedAddress{
ID: org.PostalAddress.ID, ID: org.PostalAddress.ID,
Name: org.PostalAddress.Name, Name: org.PostalAddress.Name,
CountryID: org.PostalAddress.CountryID, CountryID: org.PostalAddress.CountryID,
Country: nil, Country: nil,
}, },
VisitingAddress: Address{ VisitingAddress: EmbeddedAddress{
ID: org.VisitingAddress.ID, ID: org.VisitingAddress.ID,
Name: org.VisitingAddress.Name, Name: org.VisitingAddress.Name,
CountryID: org.VisitingAddress.CountryID, CountryID: org.VisitingAddress.CountryID,
@ -369,7 +369,7 @@ func TestEmbedPreload(t *testing.T) {
}, },
AddressID: org.AddressID, AddressID: org.AddressID,
Address: org.Address, Address: org.Address,
NestedAddress: NestedAddress{Address{ NestedAddress: NestedAddress{EmbeddedAddress{
ID: org.NestedAddress.ID, ID: org.NestedAddress.ID,
Name: org.NestedAddress.Name, Name: org.NestedAddress.Name,
CountryID: org.NestedAddress.CountryID, CountryID: org.NestedAddress.CountryID,
@ -382,7 +382,7 @@ func TestEmbedPreload(t *testing.T) {
expect: Org{ expect: Org{
ID: org.ID, ID: org.ID,
PostalAddress: org.PostalAddress, PostalAddress: org.PostalAddress,
VisitingAddress: Address{ VisitingAddress: EmbeddedAddress{
ID: org.VisitingAddress.ID, ID: org.VisitingAddress.ID,
Name: org.VisitingAddress.Name, Name: org.VisitingAddress.Name,
CountryID: org.VisitingAddress.CountryID, CountryID: org.VisitingAddress.CountryID,
@ -390,7 +390,7 @@ func TestEmbedPreload(t *testing.T) {
}, },
AddressID: org.AddressID, AddressID: org.AddressID,
Address: nil, Address: nil,
NestedAddress: NestedAddress{Address{ NestedAddress: NestedAddress{EmbeddedAddress{
ID: org.NestedAddress.ID, ID: org.NestedAddress.ID,
Name: org.NestedAddress.Name, Name: org.NestedAddress.Name,
CountryID: org.NestedAddress.CountryID, CountryID: org.NestedAddress.CountryID,
@ -399,16 +399,16 @@ func TestEmbedPreload(t *testing.T) {
}, },
}, { }, {
name: "nested address country", name: "nested address country",
preloads: map[string][]interface{}{"NestedAddress.Address.Country": {}}, preloads: map[string][]interface{}{"NestedAddress.EmbeddedAddress.Country": {}},
expect: Org{ expect: Org{
ID: org.ID, ID: org.ID,
PostalAddress: Address{ PostalAddress: EmbeddedAddress{
ID: org.PostalAddress.ID, ID: org.PostalAddress.ID,
Name: org.PostalAddress.Name, Name: org.PostalAddress.Name,
CountryID: org.PostalAddress.CountryID, CountryID: org.PostalAddress.CountryID,
Country: nil, Country: nil,
}, },
VisitingAddress: Address{ VisitingAddress: EmbeddedAddress{
ID: org.VisitingAddress.ID, ID: org.VisitingAddress.ID,
Name: org.VisitingAddress.Name, Name: org.VisitingAddress.Name,
CountryID: org.VisitingAddress.CountryID, CountryID: org.VisitingAddress.CountryID,