sqlboiler/templates_test/relationship_to_many.tpl

85 lines
2.6 KiB
Smarty
Raw Normal View History

2016-07-13 05:05:33 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . }}
{{- $table := .Table }}
{{- range .Table.ToManyRelationships -}}
{{- if .ForeignColumnUnique -}}
{{- template "relationship_to_one_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
t.Skip("this test is broke")
2016-07-15 08:15:35 +02:00
var err error
tx := MustTx(boil.Begin())
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)
}
boil.RandomizeStruct(&b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, true, "{{.ForeignColumn}}")
boil.RandomizeStruct(&c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, true, "{{.ForeignColumn}}")
{{if .Nullable -}}
a.{{.Column | titleCase}}.Valid = true
{{- end}}
{{- if .ForeignColumnNullable -}}
b.{{.ForeignColumn | titleCase}}.Valid = true
c.{{.ForeignColumn | titleCase}}.Valid = true
{{- end}}
{{if not .ToJoinTable -}}
b.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
c.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
{{- 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)
}
{{if .ToJoinTable -}}
_, err = tx.Exec(`insert into {{.JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{.Column | titleCase}}, b.{{.ForeignColumn | titleCase}})
if err != nil {
t.Fatal(err)
}
_, err = tx.Exec(`insert into {{.JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{.Column | titleCase}}, c.{{.ForeignColumn | titleCase}})
if err != nil {
t.Fatal(err)
}
{{end}}
{{$varname := $rel.ForeignTable.NamePluralGo | toLower -}}
2016-08-01 07:10:10 +02:00
{{$varname}}, err := a.{{$rel.Function.Name}}(tx)
if err != nil {
t.Fatal(err)
}
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")
}
if t.Failed() {
t.Logf("%#v", {{$varname}})
}
2016-07-13 05:05:33 +02:00
}
{{end -}}{{- /* if unique */ -}}
{{- end -}}{{- /* range */ -}}
2016-07-13 05:05:33 +02:00
{{- end -}}{{- /* outer if join table */ -}}