2016-07-13 05:05:33 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
2016-07-14 05:31:44 +02:00
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . }}
|
|
|
|
{{- $table := .Table }}
|
2016-07-16 22:02:09 +02:00
|
|
|
{{- range .Table.ToManyRelationships -}}
|
2016-08-14 00:28:54 +02:00
|
|
|
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
|
2016-07-18 02:50:01 +02:00
|
|
|
{{- template "relationship_to_one_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
|
|
|
|
{{- else -}}
|
2016-07-14 05:31:44 +02:00
|
|
|
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
|
2016-08-15 11:36:38 +02:00
|
|
|
func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
|
2016-07-15 08:15:35 +02:00
|
|
|
var err error
|
|
|
|
tx := MustTx(boil.Begin())
|
2016-07-14 05:31:44 +02:00
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
var a {{$rel.LocalTable.NameGo}}
|
|
|
|
var b, c {{$rel.ForeignTable.NameGo}}
|
2016-07-13 05:05:33 +02:00
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
if err := a.Insert(tx); err != nil {
|
2016-07-13 05:05:33 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:06:28 +02:00
|
|
|
seed := randomize.NewSeed()
|
|
|
|
randomize.Struct(seed, &b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false, "{{.ForeignColumn}}")
|
|
|
|
randomize.Struct(seed, &c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, false, "{{.ForeignColumn}}")
|
2016-07-15 08:37:39 +02:00
|
|
|
{{if .Nullable -}}
|
|
|
|
a.{{.Column | titleCase}}.Valid = true
|
|
|
|
{{- end}}
|
|
|
|
{{- if .ForeignColumnNullable -}}
|
|
|
|
b.{{.ForeignColumn | titleCase}}.Valid = true
|
|
|
|
c.{{.ForeignColumn | titleCase}}.Valid = true
|
|
|
|
{{- end}}
|
2016-07-15 08:28:33 +02:00
|
|
|
{{if not .ToJoinTable -}}
|
2016-07-14 05:31:44 +02:00
|
|
|
b.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
|
|
|
|
c.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
|
2016-07-15 08:37:39 +02:00
|
|
|
{{- end}}
|
2016-08-01 07:10:10 +02:00
|
|
|
if err = b.Insert(tx); err != nil {
|
2016-07-13 05:05:33 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-01 07:10:10 +02:00
|
|
|
if err = c.Insert(tx); err != nil {
|
2016-07-13 05:05:33 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-14 05:31:44 +02:00
|
|
|
|
2016-07-15 08:28:33 +02:00
|
|
|
{{if .ToJoinTable -}}
|
2016-09-13 08:28:23 +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.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}})
|
2016-07-15 08:28:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-13 08:28:23 +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.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}})
|
2016-07-15 08:28:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
|
2016-08-14 09:43:30 +02:00
|
|
|
{{$varname := .ForeignTable | singular | camelCase -}}
|
2016-08-18 09:19:15 +02:00
|
|
|
{{$varname}}, err := a.{{$rel.Function.Name}}(tx).All()
|
2016-07-15 08:28:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-14 05:31:44 +02:00
|
|
|
|
|
|
|
bFound, cFound := false, false
|
|
|
|
for _, v := range {{$varname}} {
|
|
|
|
if v.{{$rel.Function.ForeignAssignment}} == b.{{$rel.Function.ForeignAssignment}} {
|
|
|
|
bFound = true
|
|
|
|
}
|
|
|
|
if v.{{$rel.Function.ForeignAssignment}} == c.{{$rel.Function.ForeignAssignment}} {
|
|
|
|
cFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bFound {
|
|
|
|
t.Error("expected to find b")
|
|
|
|
}
|
|
|
|
if !cFound {
|
|
|
|
t.Error("expected to find c")
|
|
|
|
}
|
|
|
|
|
2016-08-24 07:07:51 +02:00
|
|
|
slice := {{$rel.LocalTable.NameGo}}Slice{&a}
|
2016-09-02 03:01:20 +02:00
|
|
|
if err = a.L.Load{{$rel.Function.Name}}(tx, false, &slice); err != nil {
|
2016-08-17 06:08:55 +02:00
|
|
|
t.Fatal(err)
|
2016-08-16 09:30:08 +02:00
|
|
|
}
|
2016-08-27 06:40:11 +02:00
|
|
|
if got := len(a.R.{{$rel.Function.Name}}); got != 2 {
|
2016-08-17 06:08:55 +02:00
|
|
|
t.Error("number of eager loaded records wrong, got:", got)
|
2016-08-16 09:30:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-27 06:40:11 +02:00
|
|
|
a.R.{{$rel.Function.Name}} = nil
|
2016-09-02 03:01:20 +02:00
|
|
|
if err = a.L.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
|
2016-08-17 06:08:55 +02:00
|
|
|
t.Fatal(err)
|
2016-08-16 09:30:08 +02:00
|
|
|
}
|
2016-08-27 06:40:11 +02:00
|
|
|
if got := len(a.R.{{$rel.Function.Name}}); got != 2 {
|
2016-08-17 06:08:55 +02:00
|
|
|
t.Error("number of eager loaded records wrong, got:", got)
|
2016-08-16 09:30:08 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 05:31:44 +02:00
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("%#v", {{$varname}})
|
|
|
|
}
|
2016-07-13 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
2016-07-18 02:50:01 +02:00
|
|
|
{{end -}}{{- /* if unique */ -}}
|
|
|
|
{{- end -}}{{- /* range */ -}}
|
2016-07-13 05:05:33 +02:00
|
|
|
{{- end -}}{{- /* outer if join table */ -}}
|