Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preloading has many with no result should result in slice #69

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module github.com/Fs02/grimoire

go 1.14

require (
github.com/Fs02/go-paranoid v0.0.0-20180516074805-ab50069def7d
github.com/azer/snakecase v0.0.0-20161028114325-c818dddafb5c
github.com/davecgh/go-spew v1.1.0
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/go-sql-driver/mysql v1.3.0
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2
github.com/mattn/go-sqlite3 v1.6.0
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/objx v0.1.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
github.com/stretchr/testify v1.2.1
github.com/tidwall/gjson v1.1.3
github.com/tidwall/match v1.0.0
github.com/tidwall/match v1.0.0 // indirect
)
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func collectPreloadTarget(preload []preloadTarget, refIndex []int) (map[interfac

// reset to zero if slice.
if fv.Kind() == reflect.Slice || fv.Kind() == reflect.Array {
fv.Set(reflect.Zero(fv.Type()))
fv.Set(reflect.MakeSlice(fv.Type(), 0, 0))
}

addrs[id] = append(addrs[id], fv)
Expand Down
34 changes: 34 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,23 @@ func TestQuery_Preload_hasMany(t *testing.T) {
mock.AssertExpectations(t)
}

func TestQuery_Preload_hasManyNoResult(t *testing.T) {
mock := new(TestAdapter)
repo := Repo{adapter: mock}

user := User{ID: 10}
result := []Transaction{}

query := repo.From("transactions")

mock.Result(result).On("All", query.Where(In("user_id", 10)), &[]Transaction{}).Return(2, nil)

assert.Nil(t, query.Preload(&user, "Transactions"))
assert.Equal(t, result, user.Transactions)
assert.NotPanics(t, func() { query.MustPreload(&user, "Transactions") })
mock.AssertExpectations(t)
}

func TestQuery_Preload_sliceHasMany(t *testing.T) {
mock := new(TestAdapter)
repo := Repo{adapter: mock}
Expand Down Expand Up @@ -1251,6 +1268,23 @@ func TestQuery_Preload_nestedHasMany(t *testing.T) {
mock.AssertExpectations(t)
}

func TestQuery_Preload_nestedHasManyNoResult(t *testing.T) {
mock := new(TestAdapter)
repo := Repo{adapter: mock}

address := Address{User: &User{ID: 10}}
result := []Transaction{}

query := repo.From("transactions")

mock.Result(result).On("All", query.Where(In("user_id", 10)), &[]Transaction{}).Return(2, nil)

assert.Nil(t, query.Preload(&address, "User.Transactions"))
assert.Equal(t, result, address.User.Transactions)
assert.NotPanics(t, func() { query.MustPreload(&address, "User.Transactions") })
mock.AssertExpectations(t)
}

func TestQuery_Preload_nestedNullHasMany(t *testing.T) {
mock := new(TestAdapter)
repo := Repo{adapter: mock}
Expand Down