Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 0 additions & 126 deletions tests/multi_primary_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,132 +172,6 @@ func TestManyToManyWithMultiPrimaryKeys(t *testing.T) {
}
}

func TestManyToManyWithCustomizedForeignKeys(t *testing.T) {
t.Skip()
if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
}

if name := DB.Dialector.Name(); name == "postgres" {
t.Skip("skip postgres due to it only allow unique constraint matching given keys")
}

DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
t.Fatalf("Failed to auto migrate, got error: %v", err)
}

blog := Blog{
Locale: "ZH",
Subject: "subject",
Body: "body",
SharedTags: []Tag{
{Locale: "ZH", Value: "tag1"},
{Locale: "ZH", Value: "tag2"},
},
}
DB.Save(&blog)

blog2 := Blog{
ID: blog.ID,
Locale: "EN",
}
DB.Create(&blog2)

if !compareTags(blog.SharedTags, []string{"tag1", "tag2"}) {
t.Fatalf("Blog should has two tags")
}

// Append
tag3 := &Tag{Locale: "ZH", Value: "tag3"}
DB.Model(&blog).Association("SharedTags").Append([]*Tag{tag3})
if !compareTags(blog.SharedTags, []string{"tag1", "tag2", "tag3"}) {
t.Fatalf("Blog should has three tags after Append")
}

if DB.Model(&blog).Association("SharedTags").Count() != 3 {
t.Fatalf("Blog should has three tags after Append")
}

if DB.Model(&blog2).Association("SharedTags").Count() != 3 {
t.Fatalf("Blog should has three tags after Append")
}

var tags []Tag
DB.Model(&blog).Association("SharedTags").Find(&tags)
if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
t.Fatalf("Should find 3 tags")
}

DB.Model(&blog2).Association("SharedTags").Find(&tags)
if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
t.Fatalf("Should find 3 tags")
}

var blog1 Blog
DB.Preload("SharedTags").Find(&blog1)
if !compareTags(blog1.SharedTags, []string{"tag1", "tag2", "tag3"}) {
t.Fatalf("Preload many2many relations")
}

tag4 := &Tag{Locale: "ZH", Value: "tag4"}
DB.Model(&blog2).Association("SharedTags").Append(tag4)

DB.Model(&blog).Association("SharedTags").Find(&tags)
if !compareTags(tags, []string{"tag1", "tag2", "tag3", "tag4"}) {
t.Fatalf("Should find 3 tags")
}

DB.Model(&blog2).Association("SharedTags").Find(&tags)
if !compareTags(tags, []string{"tag1", "tag2", "tag3", "tag4"}) {
t.Fatalf("Should find 3 tags")
}

// Replace
tag5 := &Tag{Locale: "ZH", Value: "tag5"}
tag6 := &Tag{Locale: "ZH", Value: "tag6"}
DB.Model(&blog2).Association("SharedTags").Replace(tag5, tag6)
var tags2 []Tag
DB.Model(&blog).Association("SharedTags").Find(&tags2)
if !compareTags(tags2, []string{"tag5", "tag6"}) {
t.Fatalf("Should find 2 tags after Replace")
}

DB.Model(&blog2).Association("SharedTags").Find(&tags2)
if !compareTags(tags2, []string{"tag5", "tag6"}) {
t.Fatalf("Should find 2 tags after Replace")
}

if DB.Model(&blog).Association("SharedTags").Count() != 2 {
t.Fatalf("Blog should has three tags after Replace")
}

// Delete
DB.Model(&blog).Association("SharedTags").Delete(tag5)
var tags3 []Tag
DB.Model(&blog).Association("SharedTags").Find(&tags3)
if !compareTags(tags3, []string{"tag6"}) {
t.Fatalf("Should find 1 tags after Delete")
}

if DB.Model(&blog).Association("SharedTags").Count() != 1 {
t.Fatalf("Blog should has three tags after Delete")
}

DB.Model(&blog2).Association("SharedTags").Delete(tag3)
var tags4 []Tag
DB.Model(&blog).Association("SharedTags").Find(&tags4)
if !compareTags(tags4, []string{"tag6"}) {
t.Fatalf("Tag should not be deleted when Delete with a unrelated tag")
}

// Clear
DB.Model(&blog2).Association("SharedTags").Clear()
if DB.Model(&blog).Association("SharedTags").Count() != 0 {
t.Fatalf("All tags should be cleared")
}
}

func TestManyToManyWithCustomizedForeignKeys2(t *testing.T) {
if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
Expand Down
Loading