Merge branch 'master' into v1.0_dev

This commit is contained in:
Jinzhu 2016-02-14 14:18:35 +08:00
commit 86b4b20278
3 changed files with 101 additions and 28 deletions

13
main.go
View File

@ -408,8 +408,17 @@ func (s *DB) DropTableIfExists(values ...interface{}) *DB {
} }
func (s *DB) HasTable(value interface{}) bool { func (s *DB) HasTable(value interface{}) bool {
scope := s.clone().NewScope(value) var (
tableName := scope.TableName() scope = s.clone().NewScope(value)
tableName string
)
if name, ok := value.(string); ok {
tableName = name
} else {
tableName = scope.TableName()
}
has := scope.Dialect().HasTable(scope, tableName) has := scope.Dialect().HasTable(scope, tableName)
s.AddError(scope.db.Error) s.AddError(scope.db.Error)
return has return has

View File

@ -165,12 +165,24 @@ func TestHasTable(t *testing.T) {
Stuff string Stuff string
} }
DB.DropTable(&Foo{}) DB.DropTable(&Foo{})
// Table should not exist at this point, HasTable should return false
if ok := DB.HasTable("foos"); ok {
t.Errorf("Table should not exist, but does")
}
if ok := DB.HasTable(&Foo{}); ok { if ok := DB.HasTable(&Foo{}); ok {
t.Errorf("Table should not exist, but does") t.Errorf("Table should not exist, but does")
} }
// We create the table
if err := DB.CreateTable(&Foo{}).Error; err != nil { if err := DB.CreateTable(&Foo{}).Error; err != nil {
t.Errorf("Table should be created") t.Errorf("Table should be created")
} }
// And now it should exits, and HasTable should return true
if ok := DB.HasTable("foos"); !ok {
t.Errorf("Table should exist, but HasTable informs it does not")
}
if ok := DB.HasTable(&Foo{}); !ok { if ok := DB.HasTable(&Foo{}); !ok {
t.Errorf("Table should exist, but HasTable informs it does not") t.Errorf("Table should exist, but HasTable informs it does not")
} }

View File

@ -611,62 +611,114 @@ func TestNestedPreload9(t *testing.T) {
} }
} }
type Level1A struct { type LevelA1 struct {
ID uint ID uint
Value string Value string
} }
type Level1B struct { type LevelA2 struct {
ID uint ID uint
Value string Value string
Level2s []*Level2 LevelA3s []*LevelA3
} }
type Level2 struct { type LevelA3 struct {
ID uint ID uint
Value string Value string
Level1AID sql.NullInt64 LevelA1ID sql.NullInt64
Level1A *Level1A LevelA1 *LevelA1
Level1BID sql.NullInt64 LevelA2ID sql.NullInt64
Level1B *Level1B LevelA2 *LevelA2
} }
func TestNestedPreload10(t *testing.T) { func TestNestedPreload10(t *testing.T) {
DB.DropTableIfExists(&Level2{}) DB.DropTableIfExists(&LevelA3{})
DB.DropTableIfExists(&Level1B{}) DB.DropTableIfExists(&LevelA2{})
DB.DropTableIfExists(&Level1A{}) DB.DropTableIfExists(&LevelA1{})
if err := DB.AutoMigrate(&Level1A{}, &Level1B{}, &Level2{}).Error; err != nil { if err := DB.AutoMigrate(&LevelA1{}, &LevelA2{}, &LevelA3{}).Error; err != nil {
t.Error(err) t.Error(err)
} }
level1A := &Level1A{Value: "foo"} levelA1 := &LevelA1{Value: "foo"}
if err := DB.Save(&level1A).Error; err != nil { if err := DB.Save(levelA1).Error; err != nil {
t.Error(err) t.Error(err)
} }
want := []*Level1B{ want := []*LevelA2{
&Level1B{ &LevelA2{
Value: "bar", Value: "bar",
Level2s: []*Level2{ LevelA3s: []*LevelA3{
&Level2{ &LevelA3{
Value: "qux", Value: "qux",
Level1A: level1A, LevelA1: levelA1,
}, },
}, },
}, },
&Level1B{ &LevelA2{
Value: "bar 2", Value: "bar 2",
}, },
} }
for _, level1B := range want { for _, levelA2 := range want {
if err := DB.Save(level1B).Error; err != nil { if err := DB.Save(levelA2).Error; err != nil {
t.Error(err) t.Error(err)
} }
} }
var got []*Level1B var got []*LevelA2
if err := DB.Preload("Level2s.Level1A").Find(&got).Error; err != nil { if err := DB.Preload("LevelA3s.LevelA1").Find(&got).Error; err != nil {
t.Error(err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
}
}
type LevelB1 struct {
ID uint
Value string
LevelB3s []*LevelB3
}
type LevelB2 struct {
ID uint
Value string
}
type LevelB3 struct {
ID uint
Value string
LevelB1ID sql.NullInt64
LevelB1 *LevelB1
LevelB2s []*LevelB2 `gorm:"many2many:levelb1_levelb3_levelb2s"`
}
func TestNestedPreload11(t *testing.T) {
DB.DropTableIfExists(&LevelB2{})
DB.DropTableIfExists(&LevelB3{})
DB.DropTableIfExists(&LevelB1{})
if err := DB.AutoMigrate(&LevelB1{}, &LevelB2{}, &LevelB3{}).Error; err != nil {
t.Error(err)
}
levelB1 := &LevelB1{Value: "foo"}
if err := DB.Create(levelB1).Error; err != nil {
t.Error(err)
}
levelB3 := &LevelB3{
Value: "bar",
LevelB1ID: sql.NullInt64{Valid: true, Int64: int64(levelB1.ID)},
}
if err := DB.Create(levelB3).Error; err != nil {
t.Error(err)
}
levelB1.LevelB3s = []*LevelB3{levelB3}
want := []*LevelB1{levelB1}
var got []*LevelB1
if err := DB.Preload("LevelB3s.LevelB2s").Find(&got).Error; err != nil {
t.Error(err) t.Error(err)
} }