2016-09-15 05:45:09 +02:00
|
|
|
package queries
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-15 05:45:09 +02:00
|
|
|
import (
|
2016-09-29 04:57:38 +02:00
|
|
|
"fmt"
|
2016-09-15 05:45:09 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/vattle/sqlboiler/boil"
|
|
|
|
)
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
var testEagerCounters struct {
|
|
|
|
ChildOne int
|
|
|
|
ChildMany int
|
|
|
|
NestedOne int
|
|
|
|
NestedMany int
|
|
|
|
}
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEager struct {
|
|
|
|
ID int
|
|
|
|
R *testEagerR
|
|
|
|
L testEagerL
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
|
|
|
|
type testEagerR struct {
|
|
|
|
ChildOne *testEagerChild
|
|
|
|
ChildMany []*testEagerChild
|
2016-11-16 05:34:36 +01:00
|
|
|
ZeroOne *testEagerZero
|
|
|
|
ZeroMany []*testEagerZero
|
2016-09-29 04:57:38 +02:00
|
|
|
}
|
|
|
|
type testEagerL struct {
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEagerChild struct {
|
2016-09-03 08:53:37 +02:00
|
|
|
ID int
|
2016-09-29 04:57:38 +02:00
|
|
|
R *testEagerChildR
|
|
|
|
L testEagerChildL
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEagerChildR struct {
|
|
|
|
NestedOne *testEagerNested
|
|
|
|
NestedMany []*testEagerNested
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEagerChildL struct {
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEagerNested struct {
|
2016-09-03 08:53:37 +02:00
|
|
|
ID int
|
2016-09-29 04:57:38 +02:00
|
|
|
R *testEagerNestedR
|
|
|
|
L testEagerNestedL
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEagerNestedR struct {
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
type testEagerNestedL struct {
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-16 05:34:36 +01:00
|
|
|
type testEagerZero struct {
|
|
|
|
ID int
|
|
|
|
R *testEagerZeroR
|
|
|
|
L testEagerZeroL
|
|
|
|
}
|
|
|
|
type testEagerZeroR struct {
|
|
|
|
NestedOne *testEagerNested
|
|
|
|
NestedMany []*testEagerNested
|
|
|
|
}
|
|
|
|
type testEagerZeroL struct {
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +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
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
for _, o := range toSetOn {
|
|
|
|
if o.R == nil {
|
|
|
|
o.R = &testEagerR{}
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
o.R.ChildOne = &testEagerChild{ID: 11}
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
|
|
|
|
testEagerCounters.ChildOne++
|
|
|
|
|
2016-09-03 08:53:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
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
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
for _, o := range toSetOn {
|
|
|
|
if o.R == nil {
|
|
|
|
o.R = &testEagerR{}
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
o.R.ChildMany = []*testEagerChild{
|
2016-09-29 17:17:13 +02:00
|
|
|
&testEagerChild{ID: 12},
|
|
|
|
&testEagerChild{ID: 13},
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
|
|
|
|
testEagerCounters.ChildMany++
|
|
|
|
|
2016-09-03 08:53:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
for _, o := range toSetOn {
|
|
|
|
if o.R == nil {
|
|
|
|
o.R = &testEagerChildR{}
|
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
o.R.NestedOne = &testEagerNested{ID: 21}
|
2016-09-29 04:57:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
testEagerCounters.NestedOne++
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-29 04:57:38 +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
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
for _, o := range toSetOn {
|
|
|
|
if o.R == nil {
|
|
|
|
o.R = &testEagerChildR{}
|
|
|
|
}
|
|
|
|
o.R.NestedMany = []*testEagerNested{
|
2016-09-29 17:17:13 +02:00
|
|
|
&testEagerNested{ID: 22},
|
|
|
|
&testEagerNested{ID: 23},
|
2016-09-29 04:57:38 +02:00
|
|
|
}
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
testEagerCounters.NestedMany++
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-11-16 05:34:36 +01:00
|
|
|
func (testEagerL) LoadZeroOne(_ boil.Executor, singular bool, obj interface{}) error {
|
|
|
|
var toSetOn []*testEager
|
|
|
|
if singular {
|
|
|
|
toSetOn = []*testEager{obj.(*testEager)}
|
|
|
|
} else {
|
|
|
|
toSetOn = *obj.(*[]*testEager)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range toSetOn {
|
|
|
|
if o.R == nil {
|
|
|
|
o.R = &testEagerR{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testEagerL) LoadZeroMany(_ boil.Executor, singular bool, obj interface{}) error {
|
|
|
|
var toSetOn []*testEager
|
|
|
|
if singular {
|
|
|
|
toSetOn = []*testEager{obj.(*testEager)}
|
|
|
|
} else {
|
|
|
|
toSetOn = *obj.(*[]*testEager)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range toSetOn {
|
|
|
|
if o.R == nil {
|
|
|
|
o.R = &testEagerR{}
|
|
|
|
}
|
|
|
|
o.R.ZeroMany = []*testEagerZero{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testEagerZeroL) LoadNestedOne(_ boil.Executor, singular bool, obj interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testEagerZeroL) LoadNestedMany(_ boil.Executor, singular bool, obj interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +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
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
obj := &testEager{}
|
2016-09-03 08:53:37 +02:00
|
|
|
|
2016-09-29 17:17:13 +02:00
|
|
|
toLoad := []string{"ChildOne.NestedMany", "ChildOne.NestedOne", "ChildMany.NestedMany", "ChildMany.NestedOne"}
|
2016-09-29 04:57:38 +02:00
|
|
|
err := eagerLoad(nil, toLoad, obj, kindStruct)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
if testEagerCounters.ChildMany != 1 {
|
|
|
|
t.Error(testEagerCounters.ChildMany)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
if testEagerCounters.ChildOne != 1 {
|
|
|
|
t.Error(testEagerCounters.ChildOne)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
if testEagerCounters.NestedMany != 2 {
|
2016-09-29 04:57:38 +02:00
|
|
|
t.Error(testEagerCounters.NestedMany)
|
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
if testEagerCounters.NestedOne != 2 {
|
2016-09-29 04:57:38 +02:00
|
|
|
t.Error(testEagerCounters.NestedOne)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
|
|
|
|
checkChildOne(obj.R.ChildOne)
|
|
|
|
checkChildMany(obj.R.ChildMany)
|
|
|
|
|
|
|
|
checkNestedOne(obj.R.ChildOne.R.NestedOne)
|
|
|
|
checkNestedOne(obj.R.ChildMany[0].R.NestedOne)
|
|
|
|
checkNestedOne(obj.R.ChildMany[1].R.NestedOne)
|
|
|
|
|
|
|
|
checkNestedMany(obj.R.ChildOne.R.NestedMany)
|
|
|
|
checkNestedMany(obj.R.ChildMany[0].R.NestedMany)
|
|
|
|
checkNestedMany(obj.R.ChildMany[1].R.NestedMany)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +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
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
slice := []*testEager{
|
|
|
|
{ID: -1},
|
|
|
|
{ID: -2},
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 17:17:13 +02:00
|
|
|
toLoad := []string{"ChildOne.NestedMany", "ChildOne.NestedOne", "ChildMany.NestedMany", "ChildMany.NestedOne"}
|
2016-09-29 04:57:38 +02:00
|
|
|
err := eagerLoad(nil, toLoad, &slice, kindPtrSliceStruct)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
if testEagerCounters.ChildMany != 1 {
|
|
|
|
t.Error(testEagerCounters.ChildMany)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
if testEagerCounters.ChildOne != 1 {
|
|
|
|
t.Error(testEagerCounters.ChildOne)
|
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
if testEagerCounters.NestedMany != 2 {
|
2016-09-29 04:57:38 +02:00
|
|
|
t.Error(testEagerCounters.NestedMany)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
if testEagerCounters.NestedOne != 2 {
|
2016-09-29 04:57:38 +02:00
|
|
|
t.Error(testEagerCounters.NestedOne)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 17:17:13 +02:00
|
|
|
|
|
|
|
checkChildOne(slice[0].R.ChildOne)
|
|
|
|
checkChildOne(slice[1].R.ChildOne)
|
|
|
|
checkChildMany(slice[0].R.ChildMany)
|
|
|
|
checkChildMany(slice[1].R.ChildMany)
|
|
|
|
|
|
|
|
checkNestedOne(slice[0].R.ChildOne.R.NestedOne)
|
|
|
|
checkNestedOne(slice[0].R.ChildMany[0].R.NestedOne)
|
|
|
|
checkNestedOne(slice[0].R.ChildMany[1].R.NestedOne)
|
|
|
|
checkNestedOne(slice[1].R.ChildOne.R.NestedOne)
|
|
|
|
checkNestedOne(slice[1].R.ChildMany[0].R.NestedOne)
|
|
|
|
checkNestedOne(slice[1].R.ChildMany[1].R.NestedOne)
|
|
|
|
|
|
|
|
checkNestedMany(slice[0].R.ChildOne.R.NestedMany)
|
|
|
|
checkNestedMany(slice[0].R.ChildMany[0].R.NestedMany)
|
|
|
|
checkNestedMany(slice[0].R.ChildMany[1].R.NestedMany)
|
|
|
|
checkNestedMany(slice[1].R.ChildOne.R.NestedMany)
|
|
|
|
checkNestedMany(slice[1].R.ChildMany[0].R.NestedMany)
|
|
|
|
checkNestedMany(slice[1].R.ChildMany[1].R.NestedMany)
|
|
|
|
}
|
|
|
|
|
2016-11-16 05:34:36 +01:00
|
|
|
func TestEagerLoadZeroParents(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
obj := &testEager{}
|
|
|
|
|
|
|
|
toLoad := []string{"ZeroMany.NestedMany", "ZeroOne.NestedOne", "ZeroMany.NestedMany", "ZeroOne.NestedOne"}
|
|
|
|
err := eagerLoad(nil, toLoad, obj, kindStruct)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(obj.R.ZeroMany) != 0 {
|
|
|
|
t.Error("should have loaded nothing")
|
|
|
|
}
|
|
|
|
if obj.R.ZeroOne != nil {
|
|
|
|
t.Error("should have loaded nothing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 06:28:13 +01:00
|
|
|
func TestEagerLoadZeroParentsMany(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
obj := []*testEager{
|
|
|
|
&testEager{},
|
|
|
|
&testEager{},
|
|
|
|
}
|
|
|
|
|
|
|
|
toLoad := []string{"ZeroMany.NestedMany", "ZeroOne.NestedOne", "ZeroMany.NestedMany", "ZeroOne.NestedOne"}
|
|
|
|
err := eagerLoad(nil, toLoad, &obj, kindPtrSliceStruct)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(obj[0].R.ZeroMany) != 0 {
|
|
|
|
t.Error("should have loaded nothing")
|
|
|
|
}
|
|
|
|
if obj[0].R.ZeroOne != nil {
|
|
|
|
t.Error("should have loaded nothing")
|
|
|
|
}
|
|
|
|
if len(obj[1].R.ZeroMany) != 0 {
|
|
|
|
t.Error("should have loaded nothing")
|
|
|
|
}
|
|
|
|
if obj[1].R.ZeroOne != nil {
|
|
|
|
t.Error("should have loaded nothing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 17:17:13 +02:00
|
|
|
func checkChildOne(c *testEagerChild) {
|
|
|
|
if c == nil {
|
|
|
|
panic("c was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ID != 11 {
|
|
|
|
panic(fmt.Sprintf("ChildOne id was not loaded correctly: %d", c.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkChildMany(cs []*testEagerChild) {
|
|
|
|
if len(cs) != 2 {
|
|
|
|
panic("cs len was not 2")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cs[0].ID != 12 {
|
|
|
|
panic(fmt.Sprintf("cs[0] had wrong id: %d", cs[0].ID))
|
|
|
|
}
|
|
|
|
if cs[1].ID != 13 {
|
|
|
|
panic(fmt.Sprintf("cs[1] had wrong id: %d", cs[1].ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNestedOne(n *testEagerNested) {
|
|
|
|
if n == nil {
|
|
|
|
panic("n was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.ID != 21 {
|
|
|
|
panic(fmt.Sprintf("NestedOne id was not loaded correctly: %d", n.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNestedMany(ns []*testEagerNested) {
|
|
|
|
if len(ns) != 2 {
|
|
|
|
panic("ns len was not 2")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns[0].ID != 22 {
|
|
|
|
panic(fmt.Sprintf("ns[0] had wrong id: %d", ns[0].ID))
|
|
|
|
}
|
|
|
|
if ns[1].ID != 23 {
|
|
|
|
panic(fmt.Sprintf("ns[1] had wrong id: %d", ns[1].ID))
|
|
|
|
}
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|