2016-07-17 08:57:08 +02:00
|
|
|
{{- define "relationship_to_one_test_helper"}}
|
2016-08-15 11:36:38 +02:00
|
|
|
func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
2016-08-14 10:58:36 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
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:52:55 +02:00
|
|
|
{{if not .Function.ReverseInserts -}}
|
2016-08-01 07:10:10 +02:00
|
|
|
if err := foreign.Insert(tx); err != nil {
|
2016-07-15 06:49:22 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:57:08 +02:00
|
|
|
local.{{.Function.LocalAssignment}} = foreign.{{.Function.ForeignAssignment}}
|
2016-08-01 07:10:10 +02:00
|
|
|
if err := local.Insert(tx); err != nil {
|
2016-07-15 06:49:22 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-18 02:50:01 +02:00
|
|
|
{{else -}}
|
2016-08-01 07:10:10 +02:00
|
|
|
if err := local.Insert(tx); err != nil {
|
2016-07-18 02:50:01 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}}
|
2016-08-01 07:10:10 +02:00
|
|
|
if err := foreign.Insert(tx); err != nil {
|
2016-07-18 02:50:01 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
{{end -}}
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-08-01 07:10:10 +02:00
|
|
|
check, err := local.{{.Function.Name}}(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 -}}
|