sqlboiler/templates_test/relationship_to_one.tpl

70 lines
2.1 KiB
Smarty
Raw Normal View History

2016-09-18 20:13:11 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
{{- $txt := txtsFromFKey $dot.PkgName $dot.Tables $dot.Table . -}}
2016-09-18 20:13:11 +02:00
func test{{$txt.LocalTable.NameGo}}ToOne{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) {
tx := MustTx(boil.Begin())
defer tx.Rollback()
2016-07-15 06:49:22 +02:00
2016-09-18 20:13:11 +02:00
var foreign {{$txt.ForeignTable.NameGo}}
var local {{$txt.LocalTable.NameGo}}
{{if .ForeignKey.Nullable -}}
local.{{.ForeignKey.Column | titleCase}}.Valid = true
{{end}}
{{- if .ForeignKey.ForeignColumnNullable -}}
foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
{{end}}
2016-07-15 06:49:22 +02:00
2016-09-18 20:13:11 +02:00
{{if not $txt.Function.OneToOne -}}
if err := foreign.Insert(tx); err != nil {
t.Fatal(err)
}
2016-07-15 06:49:22 +02:00
2016-09-18 20:13:11 +02:00
local.{{$txt.Function.LocalAssignment}} = foreign.{{$txt.Function.ForeignAssignment}}
if err := local.Insert(tx); err != nil {
t.Fatal(err)
}
{{else -}}
if err := local.Insert(tx); err != nil {
t.Fatal(err)
}
2016-09-18 20:13:11 +02:00
foreign.{{$txt.Function.ForeignAssignment}} = local.{{$txt.Function.LocalAssignment}}
if err := foreign.Insert(tx); err != nil {
t.Fatal(err)
}
{{end -}}
2016-07-15 06:49:22 +02:00
2016-09-18 20:13:11 +02:00
check, err := local.{{$txt.Function.Name}}(tx).One()
if err != nil {
t.Fatal(err)
}
2016-07-15 06:49:22 +02:00
2016-09-18 20:13:11 +02:00
{{if $txt.Function.UsesBytes -}}
if 0 != bytes.Compare(check.{{$txt.Function.ForeignAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
{{else -}}
2016-09-18 20:13:11 +02:00
if check.{{$txt.Function.ForeignAssignment}} != foreign.{{$txt.Function.ForeignAssignment}} {
{{end -}}
2016-09-18 20:13:11 +02:00
t.Errorf("want: %v, got %v", foreign.{{$txt.Function.ForeignAssignment}}, check.{{$txt.Function.ForeignAssignment}})
}
2016-08-18 03:53:08 +02:00
2016-09-18 20:13:11 +02:00
slice := {{$txt.LocalTable.NameGo}}Slice{&local}
if err = local.L.Load{{$txt.Function.Name}}(tx, false, &slice); err != nil {
t.Fatal(err)
}
2016-09-18 20:13:11 +02:00
if local.R.{{$txt.Function.Name}} == nil {
t.Error("struct should have been eager loaded")
}
2016-08-18 03:53:08 +02:00
2016-09-18 20:13:11 +02:00
local.R.{{$txt.Function.Name}} = nil
if err = local.L.Load{{$txt.Function.Name}}(tx, true, &local); err != nil {
t.Fatal(err)
}
2016-09-18 20:13:11 +02:00
if local.R.{{$txt.Function.Name}} == nil {
t.Error("struct should have been eager loaded")
}
2016-07-15 06:49:22 +02:00
}
2016-09-18 20:13:11 +02:00
{{end -}}{{/* range */}}
{{- end -}}{{/* join table */}}