sqlboiler/queries/eager_load_test.go

200 lines
3.9 KiB
Go
Raw Normal View History

package queries
2016-09-03 08:53:37 +02:00
import (
"fmt"
"testing"
"github.com/vattle/sqlboiler/boil"
)
2016-09-03 08:53:37 +02:00
var testEagerCounters struct {
ChildOne int
ChildMany int
NestedOne int
NestedMany int
}
2016-09-03 08:53:37 +02:00
type testEager struct {
ID int
R *testEagerR
L testEagerL
2016-09-03 08:53:37 +02:00
}
type testEagerR struct {
ChildOne *testEagerChild
ChildMany []*testEagerChild
}
type testEagerL struct {
2016-09-03 08:53:37 +02:00
}
type testEagerChild struct {
2016-09-03 08:53:37 +02:00
ID int
R *testEagerChildR
L testEagerChildL
2016-09-03 08:53:37 +02:00
}
type testEagerChildR struct {
NestedOne *testEagerNested
NestedMany []*testEagerNested
2016-09-03 08:53:37 +02:00
}
type testEagerChildL struct {
2016-09-03 08:53:37 +02:00
}
type testEagerNested struct {
2016-09-03 08:53:37 +02:00
ID int
R *testEagerNestedR
L testEagerNestedL
2016-09-03 08:53:37 +02:00
}
type testEagerNestedR struct {
2016-09-03 08:53:37 +02:00
}
type testEagerNestedL struct {
2016-09-03 08:53:37 +02:00
}
func (testEagerL) LoadChildOne(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEager
if singular {
toSetOn = []*testEager{obj.(*testEager)}
} else {
toSetOn = *obj.(*[]*testEager)
}
2016-09-03 08:53:37 +02:00
for _, o := range toSetOn {
if o.R == nil {
o.R = &testEagerR{}
2016-09-03 08:53:37 +02:00
}
o.R.ChildOne = &testEagerChild{ID: 1}
2016-09-03 08:53:37 +02:00
}
testEagerCounters.ChildOne++
fmt.Println("l! ChildOne")
2016-09-03 08:53:37 +02:00
return nil
}
func (testEagerL) LoadChildMany(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEager
if singular {
toSetOn = []*testEager{obj.(*testEager)}
} else {
toSetOn = *obj.(*[]*testEager)
}
2016-09-03 08:53:37 +02:00
for _, o := range toSetOn {
if o.R == nil {
o.R = &testEagerR{}
2016-09-03 08:53:37 +02:00
}
o.R.ChildMany = []*testEagerChild{
&testEagerChild{ID: 2},
&testEagerChild{ID: 3},
2016-09-03 08:53:37 +02:00
}
}
testEagerCounters.ChildMany++
fmt.Println("l! ChildMany")
2016-09-03 08:53:37 +02:00
return nil
}
func (testEagerChildL) LoadNestedOne(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEagerChild
if singular {
toSetOn = []*testEagerChild{obj.(*testEagerChild)}
} else {
toSetOn = *obj.(*[]*testEagerChild)
2016-09-03 08:53:37 +02:00
}
for _, o := range toSetOn {
if o.R == nil {
o.R = &testEagerChildR{}
}
o.R.NestedOne = &testEagerNested{ID: 6}
}
testEagerCounters.NestedOne++
fmt.Println("l! NestedOne")
2016-09-03 08:53:37 +02:00
return nil
}
2016-09-03 08:53:37 +02:00
func (testEagerChildL) LoadNestedMany(_ boil.Executor, singular bool, obj interface{}) error {
var toSetOn []*testEagerChild
if singular {
toSetOn = []*testEagerChild{obj.(*testEagerChild)}
} else {
toSetOn = *obj.(*[]*testEagerChild)
2016-09-03 08:53:37 +02:00
}
for _, o := range toSetOn {
if o.R == nil {
o.R = &testEagerChildR{}
}
o.R.NestedMany = []*testEagerNested{
&testEagerNested{ID: 6},
&testEagerNested{ID: 7},
}
2016-09-03 08:53:37 +02:00
}
testEagerCounters.NestedMany++
fmt.Println("l! NestedMany")
2016-09-03 08:53:37 +02:00
return nil
}
2016-09-03 08:53:37 +02:00
func TestEagerLoadFromOne(t *testing.T) {
testEagerCounters.ChildOne = 0
testEagerCounters.ChildMany = 0
testEagerCounters.NestedOne = 0
testEagerCounters.NestedMany = 0
2016-09-03 08:53:37 +02:00
obj := &testEager{}
2016-09-03 08:53:37 +02:00
toLoad := []string{"ChildOne", "ChildMany.NestedMany", "ChildMany.NestedOne"}
err := eagerLoad(nil, toLoad, obj, kindStruct)
if err != nil {
t.Fatal(err)
2016-09-03 08:53:37 +02:00
}
if testEagerCounters.ChildMany != 1 {
t.Error(testEagerCounters.ChildMany)
2016-09-03 08:53:37 +02:00
}
if testEagerCounters.ChildOne != 1 {
t.Error(testEagerCounters.ChildOne)
2016-09-03 08:53:37 +02:00
}
if testEagerCounters.NestedMany != 1 {
t.Error(testEagerCounters.NestedMany)
}
if testEagerCounters.NestedOne != 1 {
t.Error(testEagerCounters.NestedOne)
2016-09-03 08:53:37 +02:00
}
}
func TestEagerLoadFromMany(t *testing.T) {
testEagerCounters.ChildOne = 0
testEagerCounters.ChildMany = 0
testEagerCounters.NestedOne = 0
testEagerCounters.NestedMany = 0
2016-09-03 08:53:37 +02:00
slice := []*testEager{
{ID: -1},
{ID: -2},
2016-09-03 08:53:37 +02:00
}
toLoad := []string{"ChildOne", "ChildMany.NestedMany", "ChildMany.NestedOne"}
err := eagerLoad(nil, toLoad, &slice, kindPtrSliceStruct)
if err != nil {
t.Fatal(err)
2016-09-03 08:53:37 +02:00
}
if testEagerCounters.ChildMany != 1 {
t.Error(testEagerCounters.ChildMany)
2016-09-03 08:53:37 +02:00
}
if testEagerCounters.ChildOne != 1 {
t.Error(testEagerCounters.ChildOne)
}
if testEagerCounters.NestedMany != 1 {
t.Error(testEagerCounters.NestedMany)
2016-09-03 08:53:37 +02:00
}
if testEagerCounters.NestedOne != 1 {
t.Error(testEagerCounters.NestedOne)
2016-09-03 08:53:37 +02:00
}
}