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

Function TestManyToManyWithCustomizedForeignKeys

tests/multi_primary_keys_test.go:137–267  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

135}
136
137func TestManyToManyWithCustomizedForeignKeys(t *testing.T) {
138 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
139 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
140 }
141
142 if mysqlVersionAtLeast(8, 4) {
143 t.Skip("skip mysql 8.4+ due to stricter foreign key requirements for non-unique referenced keys")
144 }
145
146 if name := DB.Dialector.Name(); name == "postgres" {
147 t.Skip("skip postgres due to it only allow unique constraint matching given keys")
148 }
149 if name := DB.Dialector.Name(); name == "gaussdb" {
150 t.Skip("skip gaussdb due to it only allow unique constraint matching given keys")
151 }
152
153 DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
154 if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
155 t.Fatalf("Failed to auto migrate, got error: %v", err)
156 }
157
158 blog := Blog{
159 Locale: "ZH",
160 Subject: "subject",
161 Body: "body",
162 SharedTags: []Tag{
163 {Locale: "ZH", Value: "tag1"},
164 {Locale: "ZH", Value: "tag2"},
165 },
166 }
167 DB.Save(&blog)
168
169 blog2 := Blog{
170 ID: blog.ID,
171 Locale: "EN",
172 }
173 DB.Create(&blog2)
174
175 if !compareTags(blog.SharedTags, []string{"tag1", "tag2"}) {
176 t.Fatalf("Blog should has two tags")
177 }
178
179 // Append
180 tag3 := &Tag{Locale: "ZH", Value: "tag3"}
181 DB.Model(&blog).Association("SharedTags").Append([]*Tag{tag3})
182 if !compareTags(blog.SharedTags, []string{"tag1", "tag2", "tag3"}) {
183 t.Fatalf("Blog should has three tags after Append")
184 }
185
186 if DB.Model(&blog).Association("SharedTags").Count() != 3 {
187 t.Fatalf("Blog should has three tags after Append")
188 }
189
190 if DB.Model(&blog2).Association("SharedTags").Count() != 3 {
191 t.Fatalf("Blog should has three tags after Append")
192 }
193
194 var tags []Tag

Callers

nothing calls this directly

Calls 15

mysqlVersionAtLeastFunction · 0.85
compareTagsFunction · 0.85
SaveMethod · 0.80
AppendMethod · 0.80
ModelMethod · 0.80
ClearMethod · 0.80
NameMethod · 0.65
DropTableMethod · 0.65
MigratorMethod · 0.65
AutoMigrateMethod · 0.65
CreateMethod · 0.65
CountMethod · 0.65

Tested by

no test coverage detected