2016-07-17 08:57:08 +02:00
|
|
|
{{- define "relationship_to_one_test_helper"}}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- $dot := .Dot -}}
|
|
|
|
{{- with .Rel -}}
|
2016-09-16 17:02:32 +02:00
|
|
|
{{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}}
|
|
|
|
{{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}}
|
2016-09-17 08:04:58 +02:00
|
|
|
{{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}
|
2016-08-15 11:36:38 +02:00
|
|
|
func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
var foreign {{.ForeignTable.NameGo}}
|
|
|
|
var local {{.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-14 10:08:30 +02:00
|
|
|
{{if not .Function.OneToOne -}}
|
|
|
|
if err := foreign.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
local.{{.Function.LocalAssignment}} = foreign.{{.Function.ForeignAssignment}}
|
|
|
|
if err := local.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
{{else -}}
|
|
|
|
if err := local.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-18 02:50:01 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}}
|
|
|
|
if err := foreign.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
{{end -}}
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
check, err := local.{{.Function.Name}}(tx).One()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-15 06:49:22 +02:00
|
|
|
|
2016-09-16 17:02:32 +02:00
|
|
|
{{if $usesBytes -}}
|
2016-09-16 17:22:09 +02:00
|
|
|
if 0 != bytes.Compare(check.{{.Function.ForeignAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
|
|
|
|
}
|
2016-08-18 03:53:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
slice := {{.LocalTable.NameGo}}Slice{&local}
|
|
|
|
if err = local.L.Load{{.Function.Name}}(tx, false, &slice); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if local.R.{{.Function.Name}} == nil {
|
|
|
|
t.Error("struct should have been eager loaded")
|
|
|
|
}
|
2016-08-18 03:53:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
local.R.{{.Function.Name}} = nil
|
|
|
|
if err = local.L.Load{{.Function.Name}}(tx, true, &local); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if local.R.{{.Function.Name}} == nil {
|
|
|
|
t.Error("struct should have been eager loaded")
|
|
|
|
}
|
2016-07-15 06:49:22 +02:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:57:08 +02:00
|
|
|
{{end -}}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- end -}}
|
2016-07-17 08:57:08 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
|
|
|
{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}}
|
2016-07-15 06:49:22 +02:00
|
|
|
{{end -}}
|
|
|
|
{{- end -}}
|