sqlboiler/templates_test/relationship_to_one.tpl

70 lines
2 KiB
Smarty
Raw Normal View History

{{- 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()
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}}
{{- 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
{{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)
}
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)
}
{{else -}}
2016-08-01 07:10:10 +02:00
if err := local.Insert(tx); err != nil {
t.Fatal(err)
}
foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}}
2016-08-01 07:10:10 +02:00
if err := foreign.Insert(tx); err != nil {
t.Fatal(err)
}
{{end -}}
2016-07-15 06:49:22 +02:00
check, err := local.{{.Function.Name}}(tx).One()
2016-07-15 06:49:22 +02:00
if err != nil {
t.Fatal(err)
}
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-08-18 03:53:08 +02:00
2016-08-24 07:07:51 +02:00
slice := {{.LocalTable.NameGo}}Slice{&local}
2016-08-27 06:40:11 +02:00
if err = local.R.Load{{.Function.Name}}(tx, false, &slice); err != nil {
2016-08-18 03:53:08 +02:00
t.Fatal(err)
}
2016-08-27 06:40:11 +02:00
if local.R.{{.Function.Name}} == nil {
2016-08-18 03:53:08 +02:00
t.Error("struct should have been eager loaded")
}
2016-08-27 06:40:11 +02:00
local.R.{{.Function.Name}} = nil
if err = local.R.Load{{.Function.Name}}(tx, true, &local); err != nil {
2016-08-18 03:53:08 +02:00
t.Fatal(err)
}
2016-08-27 06:40:11 +02:00
if local.R.{{.Function.Name}} == nil {
2016-08-18 03:53:08 +02:00
t.Error("struct should have been eager loaded")
}
2016-07-15 06:49:22 +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 -}}