Overwrite slice results with Find

This commit is contained in:
Jinzhu 2015-06-24 13:56:30 +08:00
parent c2c1dd1fc8
commit bdb6fc55e8
3 changed files with 2 additions and 14 deletions

View File

@ -148,7 +148,6 @@ func TestManyToMany(t *testing.T) {
t.Errorf("Query many to many relations")
}
newLanguages = []Language{}
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")
@ -194,7 +193,6 @@ func TestManyToMany(t *testing.T) {
t.Errorf("Language EE should not be deleted")
}
languages = []Language{}
DB.Where("name IN (?)", []string{"CC", "DD"}).Find(&languages)
user2 := User{Name: "Many2Many_User2", Languages: languages}

View File

@ -30,6 +30,8 @@ func Query(scope *Scope) {
if kind := dest.Kind(); kind == reflect.Slice {
isSlice = true
destType = dest.Type().Elem()
dest.Set(reflect.Indirect(reflect.New(reflect.SliceOf(destType))))
if destType.Kind() == reflect.Ptr {
isPtr = true
destType = destType.Elem()

View File

@ -98,49 +98,41 @@ func TestSearchWithPlainSQL(t *testing.T) {
t.Errorf("Should found 2 users that age > 1, but got %v", len(users))
}
users = []User{}
DB.Where("name LIKE ?", "%PlainSqlUser%").Where("age >= ?", 1).Find(&users)
if len(users) != 3 {
t.Errorf("Should found 3 users that age >= 1, but got %v", len(users))
}
users = []User{}
scopedb.Where("age <> ?", 20).Find(&users)
if len(users) != 2 {
t.Errorf("Should found 2 users age != 20, but got %v", len(users))
}
users = []User{}
scopedb.Where("birthday > ?", now.MustParse("2000-1-1")).Find(&users)
if len(users) != 2 {
t.Errorf("Should found 2 users's birthday > 2000-1-1, but got %v", len(users))
}
users = []User{}
scopedb.Where("birthday > ?", "2002-10-10").Find(&users)
if len(users) != 2 {
t.Errorf("Should found 2 users's birthday >= 2002-10-10, but got %v", len(users))
}
users = []User{}
scopedb.Where("birthday >= ?", "2010-1-1").Where("birthday < ?", "2020-1-1").Find(&users)
if len(users) != 1 {
t.Errorf("Should found 1 users's birthday < 2020-1-1 and >= 2010-1-1, but got %v", len(users))
}
users = []User{}
DB.Where("name in (?)", []string{user1.Name, user2.Name}).Find(&users)
if len(users) != 2 {
t.Errorf("Should found 2 users, but got %v", len(users))
}
users = []User{}
DB.Where("id in (?)", []int64{user1.Id, user2.Id, user3.Id}).Find(&users)
if len(users) != 3 {
t.Errorf("Should found 3 users, but got %v", len(users))
}
users = []User{}
DB.Where("id in (?)", user1.Id).Find(&users)
if len(users) != 1 {
t.Errorf("Should found 1 users, but got %v", len(users))
@ -191,7 +183,6 @@ func TestSearchWithStruct(t *testing.T) {
t.Errorf("Search first record with where struct")
}
users = []User{}
DB.Find(&users, &User{Name: user2.Name})
if len(users) != 1 {
t.Errorf("Search all records with inline struct")
@ -222,7 +213,6 @@ func TestSearchWithMap(t *testing.T) {
t.Errorf("Search all records with inline map")
}
users = []User{}
DB.Find(&users, map[string]interface{}{"name": user3.Name})
if len(users) != 1 {
t.Errorf("Search all records with inline map")
@ -395,13 +385,11 @@ func TestNot(t *testing.T) {
t.Errorf("Should find all users's name not equal 3")
}
users4 = []User{}
DB.Not("name = ?", "user3").Find(&users4)
if len(users1)-len(users4) != int(name3Count) {
t.Errorf("Should find all users's name not equal 3")
}
users4 = []User{}
DB.Not("name <> ?", "user3").Find(&users4)
if len(users4) != int(name3Count) {
t.Errorf("Should find all users's name not equal 3")