2016-07-13 05:05:33 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
2016-07-14 05:31:44 +02:00
|
|
|
{{- else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- $dot := . }}
|
|
|
|
{{- $table := .Table }}
|
|
|
|
{{- range .Table.ToManyRelationships -}}
|
2016-09-19 09:03:29 +02:00
|
|
|
{{- $txt := txtsFromToMany $dot.Tables $table .}}
|
2016-09-20 03:59:46 +02:00
|
|
|
{{- $varNameSingular := .Table | singular | camelCase -}}
|
2016-09-22 09:03:57 +02:00
|
|
|
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
|
2016-09-19 09:03:29 +02:00
|
|
|
func test{{$txt.LocalTable.NameGo}}ToMany{{$txt.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
2016-07-14 05:31:44 +02:00
|
|
|
|
2016-09-19 09:03:29 +02:00
|
|
|
var a {{$txt.LocalTable.NameGo}}
|
|
|
|
var b, c {{$txt.ForeignTable.NameGo}}
|
|
|
|
|
|
|
|
seed := randomize.NewSeed()
|
2016-09-20 03:59:46 +02:00
|
|
|
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
|
2016-09-19 09:03:29 +02:00
|
|
|
t.Errorf("Unable to randomize {{$txt.LocalTable.NameGo}} struct: %s", err)
|
|
|
|
}
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := a.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-09-22 09:09:07 +02:00
|
|
|
randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, {{$foreignVarNameSingular}}ColumnsWithDefault...)
|
|
|
|
randomize.Struct(seed, &c, {{$foreignVarNameSingular}}DBTypes, false, {{$foreignVarNameSingular}}ColumnsWithDefault...)
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if .Nullable -}}
|
|
|
|
a.{{.Column | titleCase}}.Valid = true
|
|
|
|
{{- end}}
|
2017-01-11 03:16:02 +01:00
|
|
|
{{- if .ForeignColumnNullable}}
|
2016-09-14 10:08:30 +02:00
|
|
|
b.{{.ForeignColumn | titleCase}}.Valid = true
|
|
|
|
c.{{.ForeignColumn | titleCase}}.Valid = true
|
|
|
|
{{- end}}
|
|
|
|
{{if not .ToJoinTable -}}
|
2016-09-19 09:03:29 +02:00
|
|
|
b.{{$txt.Function.ForeignAssignment}} = a.{{$txt.Function.LocalAssignment}}
|
|
|
|
c.{{$txt.Function.ForeignAssignment}} = a.{{$txt.Function.LocalAssignment}}
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- end}}
|
|
|
|
if err = b.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = c.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-14 05:31:44 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if .ToJoinTable -}}
|
2016-09-19 09:03:29 +02:00
|
|
|
_, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values {{if $dot.Dialect.IndexPlaceholders}}($1, $2){{else}}(?, ?){{end}}", a.{{$txt.LocalTable.ColumnNameGo}}, b.{{$txt.ForeignTable.ColumnNameGo}})
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-19 09:03:29 +02:00
|
|
|
_, err = tx.Exec("insert into {{.JoinTable | $dot.SchemaTable}} ({{.JoinLocalColumn | $dot.Quotes}}, {{.JoinForeignColumn | $dot.Quotes}}) values {{if $dot.Dialect.IndexPlaceholders}}($1, $2){{else}}(?, ?){{end}}", a.{{$txt.LocalTable.ColumnNameGo}}, c.{{$txt.ForeignTable.ColumnNameGo}})
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
{{end}}
|
2016-07-15 08:28:33 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{$varname := .ForeignTable | singular | camelCase -}}
|
2016-09-19 09:03:29 +02:00
|
|
|
{{$varname}}, err := a.{{$txt.Function.Name}}(tx).All()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-14 05:31:44 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
bFound, cFound := false, false
|
|
|
|
for _, v := range {{$varname}} {
|
2016-09-19 09:03:29 +02:00
|
|
|
{{if $txt.Function.UsesBytes -}}
|
|
|
|
if 0 == bytes.Compare(v.{{$txt.Function.ForeignAssignment}}, b.{{$txt.Function.ForeignAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
bFound = true
|
|
|
|
}
|
2016-09-19 09:03:29 +02:00
|
|
|
if 0 == bytes.Compare(v.{{$txt.Function.ForeignAssignment}}, c.{{$txt.Function.ForeignAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
cFound = true
|
|
|
|
}
|
|
|
|
{{else -}}
|
2016-09-19 09:03:29 +02:00
|
|
|
if v.{{$txt.Function.ForeignAssignment}} == b.{{$txt.Function.ForeignAssignment}} {
|
2016-09-14 10:08:30 +02:00
|
|
|
bFound = true
|
|
|
|
}
|
2016-09-19 09:03:29 +02:00
|
|
|
if v.{{$txt.Function.ForeignAssignment}} == c.{{$txt.Function.ForeignAssignment}} {
|
2016-09-14 10:08:30 +02:00
|
|
|
cFound = true
|
|
|
|
}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-07-14 05:31:44 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if !bFound {
|
|
|
|
t.Error("expected to find b")
|
|
|
|
}
|
|
|
|
if !cFound {
|
|
|
|
t.Error("expected to find c")
|
|
|
|
}
|
2016-07-14 05:31:44 +02:00
|
|
|
|
2016-09-19 09:03:29 +02:00
|
|
|
slice := {{$txt.LocalTable.NameGo}}Slice{&a}
|
2017-04-05 04:44:36 +02:00
|
|
|
if err = a.L.Load{{$txt.Function.Name}}(tx, false, (*[]*{{$txt.LocalTable.NameGo}})(&slice)); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-19 09:03:29 +02:00
|
|
|
if got := len(a.R.{{$txt.Function.Name}}); got != 2 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("number of eager loaded records wrong, got:", got)
|
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-19 09:03:29 +02:00
|
|
|
a.R.{{$txt.Function.Name}} = nil
|
|
|
|
if err = a.L.Load{{$txt.Function.Name}}(tx, true, &a); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-19 09:03:29 +02:00
|
|
|
if got := len(a.R.{{$txt.Function.Name}}); got != 2 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("number of eager loaded records wrong, got:", got)
|
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("%#v", {{$varname}})
|
|
|
|
}
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 08:50:31 +02:00
|
|
|
{{end -}}{{- /* range */ -}}
|
2016-07-13 05:05:33 +02:00
|
|
|
{{- end -}}{{- /* outer if join table */ -}}
|