MCPcopy
hub / github.com/go-gorm/gorm / TestMany2ManyOmitAssociations

Function TestMany2ManyOmitAssociations

tests/associations_many2many_test.go:100–127  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

98}
99
100func TestMany2ManyOmitAssociations(t *testing.T) {
101 tidbSkip(t, "not support the foreign key feature")
102
103 user := *GetUser("many2many_omit_associations", Config{Languages: 2})
104
105 if err := DB.Omit("Languages.*").Create(&user).Error; err == nil {
106 t.Fatalf("should raise error when create users without languages reference")
107 }
108
109 if err := DB.Create(&user.Languages).Error; err != nil {
110 t.Fatalf("no error should happen when create languages, but got %v", err)
111 }
112
113 if err := DB.Omit("Languages.*").Create(&user).Error; err != nil {
114 t.Fatalf("no error should happen when create user when languages exists, but got %v", err)
115 }
116
117 // Find
118 var languages []Language
119 if DB.Model(&user).Association("Languages").Find(&languages); len(languages) != 2 {
120 t.Errorf("languages count should be %v, but got %v", 2, len(languages))
121 }
122
123 newLang := Language{Code: "omitmany2many", Name: "omitmany2many"}
124 if err := DB.Model(&user).Omit("Languages.*").Association("Languages").Replace(&newLang); err == nil {
125 t.Errorf("should failed to insert languages due to constraint failed, error: %v", err)
126 }
127}
128
129func TestMany2ManyAssociationForSlice(t *testing.T) {
130 users := []User{

Callers

nothing calls this directly

Calls 8

tidbSkipFunction · 0.85
GetUserFunction · 0.85
ModelMethod · 0.80
CreateMethod · 0.65
OmitMethod · 0.65
FindMethod · 0.65
ReplaceMethod · 0.65
AssociationMethod · 0.45

Tested by

no test coverage detected