2016-07-17 08:57:08 +02:00
|
|
|
{{- define "relationship_to_one_test_helper"}}
|
|
|
|
func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
2016-07-15 08:15:35 +02:00
|
|
|
tx := MustTx(boil.Begin())
|
2016-07-15 06:49:22 +02:00
|
|
|
defer tx.Rollback()
|
|
|
|
|
2016-07-17 08:57:08 +02:00
|
|
|
var foreign {{.ForeignTable.NameGo}}
|
|
|
|
var local {{.LocalTable.NameGo}}
|
|
|
|
{{if .ForeignKey.Nullable -}}
|
|
|
|
local.{{.ForeignKey.Column | titleCase}}.Valid = true
|
2016-07-15 07:45:24 +02:00
|
|
|
{{end}}
|
2016-07-17 08:57:08 +02:00
|
|
|
{{- if .ForeignKey.ForeignColumnNullable -}}
|
|
|
|
foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
|
2016-07-15 07:45:24 +02:00
|
|
|
{{end}}
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-07-18 02:50:01 +02:00
|
|
|
|
2016-07-18 02:52:55 +02:00
|
|
|
{{if not .Function.ReverseInserts -}}
|
2016-07-15 06:49:22 +02:00
|
|
|
if err := foreign.InsertX(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:57:08 +02:00
|
|
|
local.{{.Function.LocalAssignment}} = foreign.{{.Function.ForeignAssignment}}
|
2016-07-15 06:49:22 +02:00
|
|
|
if err := local.InsertX(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-18 02:50:01 +02:00
|
|
|
{{else -}}
|
|
|
|
if err := local.InsertX(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}}
|
|
|
|
if err := foreign.InsertX(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
{{end -}}
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-07-18 02:50:01 +02:00
|
|
|
check, err := local.{{.Function.Name}}X(tx)
|
2016-07-15 06:49:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-18 02:50:01 +02:00
|
|
|
if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {
|
|
|
|
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
|
2016-07-15 06:49:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:57:08 +02:00
|
|
|
{{end -}}
|
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
|
|
|
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
|
|
|
{{- template "relationship_to_one_test_helper" $rel -}}
|
2016-07-15 06:49:22 +02:00
|
|
|
{{end -}}
|
|
|
|
{{- end -}}
|