Merge e540893344764d4861229b31f1b3bc54ce71b7a9 into 9a63fb28bad5cfdb0f89e918ad063f45285f1064

This commit is contained in:
Biju Kalissery 2015-12-29 02:25:20 +00:00
commit 6b5d2e494f
11 changed files with 84 additions and 24 deletions

View File

@ -115,3 +115,8 @@ func (commonDialect) CurrentDatabase(scope *Scope) (name string) {
scope.Err(scope.NewDB().Raw("SELECT DATABASE()").Row().Scan(&name)) scope.Err(scope.NewDB().Raw("SELECT DATABASE()").Row().Scan(&name))
return return
} }
func (commonDialect) EnableIdentityInsert(db *DB, tableName string) *DB {
return db
}

View File

@ -4,6 +4,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"time" "time"
"os"
) )
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
@ -57,6 +58,11 @@ func TestCreate(t *testing.T) {
} }
func TestCreateWithNoGORMPrimayKey(t *testing.T) { func TestCreateWithNoGORMPrimayKey(t *testing.T) {
if dialect := os.Getenv("GORM_DIALECT"); dialect == "mssql" {
t.Skip("Skipping this because MSSQL will return identity only if the table has an Id column")
}
jt := JoinTable{From: 1, To: 2} jt := JoinTable{From: 1, To: 2}
err := DB.Create(&jt).Error err := DB.Create(&jt).Error
if err != nil { if err != nil {

View File

@ -32,12 +32,23 @@ func TestCustomizeColumn(t *testing.T) {
if scope.PrimaryKey() != col { if scope.PrimaryKey() != col {
t.Errorf("CustomizeColumn should have primary key %s, but got %q", col, scope.PrimaryKey()) t.Errorf("CustomizeColumn should have primary key %s, but got %q", col, scope.PrimaryKey())
} }
tableName := "customize_columns"
idInsRes := DB.EnableIdentityInsert(&DB, tableName)
if idInsRes.Error != nil {
t.Errorf("Error while setting IDENTITY_INSERT ON for table:%v :%v", tableName, idInsRes.Error)
}
expected := "foo" expected := "foo"
cc := CustomizeColumn{ID: 666, Name: expected, Date: time.Now()} cc := CustomizeColumn{ID: 666, Name: expected, Date: time.Now()}
if count := DB.Create(&cc).RowsAffected; count != 1 { res := DB.Create(&cc)
t.Error("There should be one record be affected when create record") if res.Error != nil {
t.Errorf("Error while creating CustomizeColumn:%v", res.Error)
}
if count := res.RowsAffected; count != 1 {
t.Errorf("There should be one record be affected when create record. count:%v", count)
} }
var cc1 CustomizeColumn var cc1 CustomizeColumn

View File

@ -18,6 +18,7 @@ type Dialect interface {
HasIndex(scope *Scope, tableName string, indexName string) bool HasIndex(scope *Scope, tableName string, indexName string) bool
RemoveIndex(scope *Scope, indexName string) RemoveIndex(scope *Scope, indexName string)
CurrentDatabase(scope *Scope) string CurrentDatabase(scope *Scope) string
EnableIdentityInsert(db *DB, tableName string) *DB
} }
func NewDialect(driver string) Dialect { func NewDialect(driver string) Dialect {

View File

@ -15,6 +15,7 @@ type Person struct {
} }
type PersonAddress struct { type PersonAddress struct {
Id int
gorm.JoinTableHandler gorm.JoinTableHandler
PersonID int PersonID int
AddressID int AddressID int
@ -50,7 +51,10 @@ func TestJoinTable(t *testing.T) {
address1 := &Address{Address1: "address 1"} address1 := &Address{Address1: "address 1"}
address2 := &Address{Address1: "address 2"} address2 := &Address{Address1: "address 2"}
person := &Person{Name: "person", Addresses: []*Address{address1, address2}} person := &Person{Name: "person", Addresses: []*Address{address1, address2}}
DB.Save(person) res := DB.Save(person)
if res.Error != nil {
t.Errorf("Error while saving person object:%v", res.Error)
}
DB.Model(person).Association("Addresses").Delete(address1) DB.Model(person).Association("Addresses").Delete(address1)

View File

@ -455,6 +455,7 @@ func (s *DB) CurrentDatabase() string {
return name return name
} }
/* /*
Add foreign key to the given scope Add foreign key to the given scope
@ -554,3 +555,7 @@ func (s *DB) GetErrors() (errors []error) {
} }
return return
} }
func (s *DB) EnableIdentityInsert(db *DB, tableName string) *DB {
return s.dialect.EnableIdentityInsert(db, tableName)
}

View File

@ -88,8 +88,8 @@ type BigEmail struct {
Id int64 Id int64
UserId int64 UserId int64
Email string `sql:"index:idx_email_agent"` Email string `sql:"index:idx_email_agent"`
UserAgent string `sql:"index:idx_email_agent"` UserAgent string `sql:"index:idx_user_agent"`
RegisteredAt time.Time `sql:"unique_index"` RegisteredAt time.Time `sql:"index:idx_emails_registered_at"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
@ -100,8 +100,9 @@ func (b BigEmail) TableName() string {
func TestAutoMigration(t *testing.T) { func TestAutoMigration(t *testing.T) {
DB.AutoMigrate(&Address{}) DB.AutoMigrate(&Address{})
if err := DB.Table("emails").AutoMigrate(&BigEmail{}).Error; err != nil { if err := DB.Table("emails").AutoMigrate(&BigEmail{}).Error; err != nil {
t.Errorf("Auto Migrate should not raise any error") t.Errorf("Auto Migrate should not raise any error, but raised: %v", err)
} }
DB.Save(&BigEmail{Email: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: time.Now()}) DB.Save(&BigEmail{Email: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: time.Now()})
@ -111,7 +112,7 @@ func TestAutoMigration(t *testing.T) {
t.Errorf("Failed to create index") t.Errorf("Failed to create index")
} }
if !scope.Dialect().HasIndex(scope, scope.TableName(), "uix_emails_registered_at") { if !scope.Dialect().HasIndex(scope, scope.TableName(), "idx_emails_registered_at") {
t.Errorf("Failed to create index") t.Errorf("Failed to create index")
} }

View File

@ -78,3 +78,8 @@ func (s mssql) CurrentDatabase(scope *Scope) (name string) {
s.RawScanString(scope, &name, "SELECT DB_NAME() AS [Current Database]") s.RawScanString(scope, &name, "SELECT DB_NAME() AS [Current Database]")
return return
} }
func (s mssql) EnableIdentityInsert(db *DB, tableName string) *DB {
idSql := "SET IDENTITY_INSERT " + tableName + " ON"
return db.Exec(idSql)
}

View File

@ -34,13 +34,24 @@ func TestManyToManyWithMultiPrimaryKeys(t *testing.T) {
}, },
} }
DB.Save(&blog) res := DB.Save(&blog)
DB.Model(&blog).Association("Tags").Append([]Tag{{Locale: "ZH", Value: "tag3"}}) if nil != res.Error {
t.Errorf("Error while saving blog:%v", res.Error)
}
res2 := DB.Model(&blog).Association("Tags").Append([]Tag{{Locale: "ZH", Value: "tag3"}})
if nil != res2.Error {
t.Errorf("Error while appending tag to blog:%v", res2.Error)
}
var tags []Tag var tags []Tag
DB.Model(&blog).Related(&tags, "Tags") res = DB.Model(&blog).Related(&tags, "Tags")
if len(tags) != 3 { if nil != res.Error {
t.Errorf("should found 3 tags with blog") t.Errorf("Error while reading tags related to blog:%v", res.Error)
}
tagsCount := len(tags)
if tagsCount != 3 {
t.Errorf("should found 3 tags with blog, found:%v", tagsCount)
} }
} }
} }

View File

@ -52,12 +52,23 @@ func checkUserHasPreloadData(user User, t *testing.T) {
func TestPreload(t *testing.T) { func TestPreload(t *testing.T) {
user1 := getPreloadUser("user1") user1 := getPreloadUser("user1")
DB.Save(user1) res1 := DB.Save(user1)
if res1.Error != nil {
t.Errorf("Error in save : %v", res1.Error)
}
preloadDB := DB.Where("role = ?", "Preload").Preload("BillingAddress").Preload("ShippingAddress"). preloadDB := DB.Where("role = ?", "Preload").Preload("BillingAddress").Preload("ShippingAddress").
Preload("CreditCard").Preload("Emails").Preload("Company") Preload("CreditCard").Preload("Emails").Preload("Company")
if preloadDB.Error != nil {
t.Errorf("Error in preload : %v", preloadDB.Error)
}
var user User var user User
preloadDB.Find(&user) res := preloadDB.Find(&user)
if res.Error != nil {
t.Errorf("Error in preload : %v", res.Error)
}
checkUserHasPreloadData(user, t) checkUserHasPreloadData(user, t)
user2 := getPreloadUser("user2") user2 := getPreloadUser("user2")
@ -635,7 +646,7 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
DB.DropTableIfExists("levels") DB.DropTableIfExists("levels")
if err := DB.AutoMigrate(&Level2{}, &Level1{}).Error; err != nil { if err := DB.AutoMigrate(&Level2{}, &Level1{}).Error; err != nil {
panic(err) t.Fatalf("Error in AutoMigrate:%v", err)
} }
want := Level2{Value: "Bob", LanguageCode: "ru", Level1s: []Level1{ want := Level2{Value: "Bob", LanguageCode: "ru", Level1s: []Level1{
@ -643,7 +654,7 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
{Value: "en", LanguageCode: "en"}, {Value: "en", LanguageCode: "en"},
}} }}
if err := DB.Save(&want).Error; err != nil { if err := DB.Save(&want).Error; err != nil {
panic(err) t.Fatalf("Error in Save:%v", err)
} }
want2 := Level2{Value: "Tom", LanguageCode: "zh", Level1s: []Level1{ want2 := Level2{Value: "Tom", LanguageCode: "zh", Level1s: []Level1{
@ -651,12 +662,12 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
{Value: "de", LanguageCode: "de"}, {Value: "de", LanguageCode: "de"},
}} }}
if err := DB.Save(&want2).Error; err != nil { if err := DB.Save(&want2).Error; err != nil {
panic(err) t.Fatalf("Error in Save want2:%v", err)
} }
var got Level2 var got Level2
if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil { if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {
panic(err) t.Fatalf("Error in Preload:%v", err)
} }
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
@ -665,7 +676,7 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
var got2 Level2 var got2 Level2
if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil { if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {
panic(err) t.Fatalf("Error in Preload Level1s:%v", err)
} }
if !reflect.DeepEqual(got2, want2) { if !reflect.DeepEqual(got2, want2) {
@ -674,7 +685,7 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
var got3 []Level2 var got3 []Level2
if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil { if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
panic(err) t.Fatalf("Error in Preload got3 :%v",err)
} }
if !reflect.DeepEqual(got3, []Level2{got, got2}) { if !reflect.DeepEqual(got3, []Level2{got, got2}) {
@ -683,7 +694,7 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
var got4 []Level2 var got4 []Level2
if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil { if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
panic(err) t.Fatalf("Error in Preload got4:%v",err)
} }
var ruLevel1 Level1 var ruLevel1 Level1

View File

@ -20,13 +20,13 @@ func TestScannableSlices(t *testing.T) {
} }
if err := DB.Save(&r1).Error; err != nil { if err := DB.Save(&r1).Error; err != nil {
t.Errorf("Should save record with slice values") t.Errorf("Should save record with slice values:%v", err)
} }
var r2 RecordWithSlice var r2 RecordWithSlice
if err := DB.Find(&r2).Error; err != nil { if err := DB.Find(&r2).Error; err != nil {
t.Errorf("Should fetch record with slice values") t.Errorf("Should fetch record with slice values:%v", err)
} }
if len(r2.Strings) != 3 || r2.Strings[0] != "a" || r2.Strings[1] != "b" || r2.Strings[2] != "c" { if len(r2.Strings) != 3 || r2.Strings[0] != "a" || r2.Strings[1] != "b" || r2.Strings[2] != "c" {