Merge 0d6f01d340ff3792ee61aec1fd5c7b45e95018ee into a4e0ef6509b42be2360e339da12c8aaaf94aac12

This commit is contained in:
Joel Feinstein 2014-11-25 07:33:02 +00:00
commit d461455cc4
2 changed files with 23 additions and 0 deletions

View File

@ -77,6 +77,15 @@ func Query(scope *Scope) {
} }
if !anyRecordFound { if !anyRecordFound {
if isSlice && dest.IsNil() {
var emptySliceType reflect.Type
if isPtr {
emptySliceType = reflect.SliceOf(reflect.PtrTo(destType))
} else {
emptySliceType = reflect.SliceOf(destType)
}
dest.Set(reflect.MakeSlice(emptySliceType, 0, 0))
}
scope.Err(RecordNotFound) scope.Err(RecordNotFound)
} }
} }

View File

@ -77,6 +77,20 @@ func TestFindAsSliceOfPointers(t *testing.T) {
} }
} }
func TestFindWontReturnNilSlices(t *testing.T) {
randomNum := rand.Intn(1000000000)
var users []User
DB.Where("name = ?", randomNum).Find(&users)
var userPointers []*User
DB.Where("name = ?", randomNum).Find(&userPointers)
if reflect.ValueOf(users).IsNil() || reflect.ValueOf(userPointers).IsNil() {
t.Errorf("Should have returned empty slices")
}
}
func TestSearchWithPlainSQL(t *testing.T) { func TestSearchWithPlainSQL(t *testing.T) {
user1 := User{Name: "PlainSqlUser1", Age: 1, Birthday: now.MustParse("2000-1-1")} user1 := User{Name: "PlainSqlUser1", Age: 1, Birthday: now.MustParse("2000-1-1")}
user2 := User{Name: "PlainSqlUser2", Age: 10, Birthday: now.MustParse("2010-1-1")} user2 := User{Name: "PlainSqlUser2", Age: 10, Birthday: now.MustParse("2010-1-1")}