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

Function TestManyToManyWithCustomizedForeignKeys2

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

Source from the content-addressed store, hash-verified

267}
268
269func TestManyToManyWithCustomizedForeignKeys2(t *testing.T) {
270 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
271 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
272 }
273
274 if name := DB.Dialector.Name(); name == "postgres" || name == "mysql" {
275 t.Skip("skip postgres due to it only allow unique constraint matching given keys")
276 }
277
278 if name := DB.Dialector.Name(); name == "gaussdb" {
279 t.Skip("skip gaussdb due to it only allow unique constraint matching given keys")
280 }
281
282 DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
283 if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
284 t.Fatalf("Failed to auto migrate, got error: %v", err)
285 }
286
287 blog := Blog{
288 Locale: "ZH",
289 Subject: "subject",
290 Body: "body",
291 LocaleTags: []Tag{
292 {Locale: "ZH", Value: "tag1"},
293 {Locale: "ZH", Value: "tag2"},
294 },
295 }
296 DB.Save(&blog)
297
298 blog2 := Blog{
299 ID: blog.ID,
300 Locale: "EN",
301 }
302 DB.Create(&blog2)
303
304 // Append
305 tag3 := &Tag{Locale: "ZH", Value: "tag3"}
306 DB.Model(&blog).Association("LocaleTags").Append([]*Tag{tag3})
307 if !compareTags(blog.LocaleTags, []string{"tag1", "tag2", "tag3"}) {
308 t.Fatalf("Blog should has three tags after Append")
309 }
310
311 if DB.Model(&blog).Association("LocaleTags").Count() != 3 {
312 t.Fatalf("Blog should has three tags after Append")
313 }
314
315 if DB.Model(&blog2).Association("LocaleTags").Count() != 0 {
316 t.Fatalf("EN Blog should has 0 tags after ZH Blog Append")
317 }
318
319 var tags []Tag
320 DB.Model(&blog).Association("LocaleTags").Find(&tags)
321 if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
322 t.Fatalf("Should find 3 tags")
323 }
324
325 DB.Model(&blog2).Association("LocaleTags").Find(&tags)
326 if len(tags) != 0 {

Callers

nothing calls this directly

Calls 15

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
FindMethod · 0.65

Tested by

no test coverage detected