Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Aug 28, 2014
1 parent e6715ce commit 423d949
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 353 deletions.
28 changes: 28 additions & 0 deletions anonymous_struct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gorm_test

import "testing"

type BasePost struct {
Id int64
Title string
Url string
}

type HNPost struct {
BasePost
Upvotes int32
}

type EngadgetPost struct {
BasePost
ImageUrl string
}

func TestAnonymousStruct(t *testing.T) {
hn := HNPost{}
hn.Title = "hn_news"
DB.Debug().Save(hn)

var news HNPost
DB.Debug().First(&news)
}
100 changes: 50 additions & 50 deletions association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package gorm_test
import "testing"

func TestHasOneAndHasManyAssociation(t *testing.T) {
db.DropTable(Category{})
db.DropTable(Post{})
db.DropTable(Comment{})
DB.DropTable(Category{})
DB.DropTable(Post{})
DB.DropTable(Comment{})

db.CreateTable(Category{})
db.CreateTable(Post{})
db.CreateTable(Comment{})
DB.CreateTable(Category{})
DB.CreateTable(Post{})
DB.CreateTable(Comment{})

post := Post{
Title: "post 1",
Expand All @@ -19,30 +19,30 @@ func TestHasOneAndHasManyAssociation(t *testing.T) {
MainCategory: Category{Name: "Main Category 1"},
}

if err := db.Save(&post).Error; err != nil {
if err := DB.Save(&post).Error; err != nil {
t.Errorf("Got errors when save post")
}

if db.First(&Category{}, "name = ?", "Category 1").Error != nil {
if DB.First(&Category{}, "name = ?", "Category 1").Error != nil {
t.Errorf("Category should be saved")
}

var p Post
db.First(&p, post.Id)
DB.First(&p, post.Id)

if post.CategoryId.Int64 == 0 || p.CategoryId.Int64 == 0 || post.MainCategoryId == 0 || p.MainCategoryId == 0 {
t.Errorf("Category Id should exist")
}

if db.First(&Comment{}, "content = ?", "Comment 1").Error != nil {
if DB.First(&Comment{}, "content = ?", "Comment 1").Error != nil {
t.Errorf("Comment 1 should be saved")
}
if post.Comments[0].PostId == 0 {
t.Errorf("Comment Should have post id")
}

var comment Comment
if db.First(&comment, "content = ?", "Comment 2").Error != nil {
if DB.First(&comment, "content = ?", "Comment 2").Error != nil {
t.Errorf("Comment 2 should be saved")
}

Expand All @@ -51,7 +51,7 @@ func TestHasOneAndHasManyAssociation(t *testing.T) {
}

comment3 := Comment{Content: "Comment 3", Post: Post{Title: "Title 3", Body: "Body 3"}}
db.Save(&comment3)
DB.Save(&comment3)
}

func TestRelated(t *testing.T) {
Expand All @@ -63,7 +63,7 @@ func TestRelated(t *testing.T) {
CreditCard: CreditCard{Number: "1234567890"},
}

db.Save(&user)
DB.Save(&user)

if user.CreditCard.Id == 0 {
t.Errorf("After user save, credit card should have id")
Expand All @@ -78,131 +78,131 @@ func TestRelated(t *testing.T) {
}

var emails []Email
db.Model(&user).Related(&emails)
DB.Model(&user).Related(&emails)
if len(emails) != 2 {
t.Errorf("Should have two emails")
}

var emails2 []Email
db.Model(&user).Where("email = ?", "jinzhu@example.com").Related(&emails2)
DB.Model(&user).Where("email = ?", "jinzhu@example.com").Related(&emails2)
if len(emails2) != 1 {
t.Errorf("Should have two emails")
}

var user1 User
db.Model(&user).Related(&user1.Emails)
DB.Model(&user).Related(&user1.Emails)
if len(user1.Emails) != 2 {
t.Errorf("Should have only one email match related condition")
}

var address1 Address
db.Model(&user).Related(&address1, "BillingAddressId")
DB.Model(&user).Related(&address1, "BillingAddressId")
if address1.Address1 != "Billing Address - Address 1" {
t.Errorf("Should get billing address from user correctly")
}

user1 = User{}
db.Model(&address1).Related(&user1, "BillingAddressId")
if db.NewRecord(user1) {
DB.Model(&address1).Related(&user1, "BillingAddressId")
if DB.NewRecord(user1) {
t.Errorf("Should get user from address correctly")
}

var user2 User
db.Model(&emails[0]).Related(&user2)
DB.Model(&emails[0]).Related(&user2)
if user2.Id != user.Id || user2.Name != user.Name {
t.Errorf("Should get user from email correctly")
}

var creditcard CreditCard
var user3 User
db.First(&creditcard, "number = ?", "1234567890")
db.Model(&creditcard).Related(&user3)
DB.First(&creditcard, "number = ?", "1234567890")
DB.Model(&creditcard).Related(&user3)
if user3.Id != user.Id || user3.Name != user.Name {
t.Errorf("Should get user from credit card correctly")
}

if !db.Model(&CreditCard{}).Related(&User{}).RecordNotFound() {
if !DB.Model(&CreditCard{}).Related(&User{}).RecordNotFound() {
t.Errorf("RecordNotFound for Related")
}
}

func TestManyToMany(t *testing.T) {
db.Raw("delete from languages")
DB.Raw("delete from languages")
var languages = []Language{{Name: "ZH"}, {Name: "EN"}}
user := User{Name: "Many2Many", Languages: languages}
db.Save(&user)
DB.Save(&user)

// Query
var newLanguages []Language
db.Model(&user).Related(&newLanguages, "Languages")
DB.Model(&user).Related(&newLanguages, "Languages")
if len(newLanguages) != len([]string{"ZH", "EN"}) {
t.Errorf("Query many to many relations")
}

newLanguages = []Language{}
db.Model(&user).Association("Languages").Find(&newLanguages)
DB.Model(&user).Association("Languages").Find(&newLanguages)
if len(newLanguages) != len([]string{"ZH", "EN"}) {
t.Errorf("Should be able to find many to many relations")
}

if db.Model(&user).Association("Languages").Count() != len([]string{"ZH", "EN"}) {
if DB.Model(&user).Association("Languages").Count() != len([]string{"ZH", "EN"}) {
t.Errorf("Count should return correct result")
}

// Append
db.Model(&user).Association("Languages").Append(&Language{Name: "DE"})
if db.Where("name = ?", "DE").First(&Language{}).RecordNotFound() {
DB.Model(&user).Association("Languages").Append(&Language{Name: "DE"})
if DB.Where("name = ?", "DE").First(&Language{}).RecordNotFound() {
t.Errorf("New record should be saved when append")
}

languageA := Language{Name: "AA"}
db.Save(&languageA)
db.Model(&User{Id: user.Id}).Association("Languages").Append(languageA)
DB.Save(&languageA)
DB.Model(&User{Id: user.Id}).Association("Languages").Append(languageA)
languageC := Language{Name: "CC"}
db.Save(&languageC)
db.Model(&user).Association("Languages").Append(&[]Language{{Name: "BB"}, languageC})
db.Model(&User{Id: user.Id}).Association("Languages").Append([]Language{{Name: "DD"}, {Name: "EE"}})
DB.Save(&languageC)
DB.Model(&user).Association("Languages").Append(&[]Language{{Name: "BB"}, languageC})
DB.Model(&User{Id: user.Id}).Association("Languages").Append([]Language{{Name: "DD"}, {Name: "EE"}})

totalLanguages := []string{"ZH", "EN", "DE", "AA", "BB", "CC", "DD", "EE"}

if db.Model(&user).Association("Languages").Count() != len(totalLanguages) {
if DB.Model(&user).Association("Languages").Count() != len(totalLanguages) {
t.Errorf("All appended languages should be saved")
}

// Delete
var language Language
db.Where("name = ?", "EE").First(&language)
db.Model(&user).Association("Languages").Delete(language, &language)
if db.Model(&user).Association("Languages").Count() != len(totalLanguages)-1 {
DB.Where("name = ?", "EE").First(&language)
DB.Model(&user).Association("Languages").Delete(language, &language)
if DB.Model(&user).Association("Languages").Count() != len(totalLanguages)-1 {
t.Errorf("Relations should be deleted with Delete")
}
if db.Where("name = ?", "EE").First(&Language{}).RecordNotFound() {
if DB.Where("name = ?", "EE").First(&Language{}).RecordNotFound() {
t.Errorf("Language EE should not be deleted")
}

languages = []Language{}
db.Where("name IN (?)", []string{"CC", "DD"}).Find(&languages)
db.Model(&user).Association("Languages").Delete(languages, &languages)
if db.Model(&user).Association("Languages").Count() != len(totalLanguages)-3 {
DB.Where("name IN (?)", []string{"CC", "DD"}).Find(&languages)
DB.Model(&user).Association("Languages").Delete(languages, &languages)
if DB.Model(&user).Association("Languages").Count() != len(totalLanguages)-3 {
t.Errorf("Relations should be deleted with Delete")
}

// Replace
var languageB Language
db.Where("name = ?", "BB").First(&languageB)
db.Model(&user).Association("Languages").Replace(languageB)
if db.Model(&user).Association("Languages").Count() != 1 {
DB.Where("name = ?", "BB").First(&languageB)
DB.Model(&user).Association("Languages").Replace(languageB)
if DB.Model(&user).Association("Languages").Count() != 1 {
t.Errorf("Relations should be replaced")
}

db.Model(&user).Association("Languages").Replace(&[]Language{{Name: "FF"}, {Name: "JJ"}})
if db.Model(&user).Association("Languages").Count() != len([]string{"FF", "JJ"}) {
DB.Model(&user).Association("Languages").Replace(&[]Language{{Name: "FF"}, {Name: "JJ"}})
if DB.Model(&user).Association("Languages").Count() != len([]string{"FF", "JJ"}) {
t.Errorf("Relations should be replaced")
}

// Clear
db.Model(&user).Association("Languages").Clear()
if db.Model(&user).Association("Languages").Count() != 0 {
DB.Model(&user).Association("Languages").Clear()
if DB.Model(&user).Association("Languages").Count() != 0 {
t.Errorf("Relations should be cleared")
}
}
Loading

0 comments on commit 423d949

Please sign in to comment.