sqlboiler/templates_test/relationship_to_one.tpl
2016-07-14 23:15:35 -07:00

39 lines
1.1 KiB
Smarty

{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $rel := textsFromForeignKey $dot.Tables $dot.Table . -}}
func Test{{$rel.LocalTable.NameGo}}ToOne{{$rel.ForeignTable.NameGo}}_{{$rel.LocalTable.ColumnNameGo}}(t *testing.T) {
tx := MustTx(boil.Begin())
defer tx.Rollback()
var foreign {{$rel.ForeignTable.NameGo}}
var local {{$rel.LocalTable.NameGo}}
{{if .Nullable -}}
local.{{.Column | titleCase}}.Valid = true
{{end}}
{{- if .ForeignColumnNullable -}}
foreign.{{.ForeignColumn | titleCase}}.Valid = true
{{end}}
if err := foreign.InsertX(tx); err != nil {
t.Fatal(err)
}
local.{{$rel.Function.LocalAssignment}} = foreign.{{$rel.Function.ForeignAssignment}}
if err := local.InsertX(tx); err != nil {
t.Fatal(err)
}
checkForeign, err := local.{{$rel.LocalTable.ColumnNameGo}}X(tx)
if err != nil {
t.Fatal(err)
}
if checkForeign.{{$rel.Function.ForeignAssignment}} != foreign.{{$rel.Function.ForeignAssignment}} {
t.Errorf("want: %v, got %v", foreign.{{$rel.Function.ForeignAssignment}}, checkForeign.{{$rel.Function.ForeignAssignment}})
}
}
{{end -}}
{{- end -}}