Produce true one_to_one relationships.

- Generating code & test for one_to_one by calling existing to_one
  templates with the reversed texts function output as input.
- Add a section to reverse the order of the function inserts.
This commit is contained in:
Aaron L 2016-07-17 17:50:01 -07:00
parent 1513661e15
commit d4170806f5
4 changed files with 37 additions and 15 deletions

View file

@ -48,6 +48,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}X(exec b
{{- $dot := . -}} {{- $dot := . -}}
{{- range .Table.FKeys -}} {{- range .Table.FKeys -}}
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}} {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
// TO_ONE
{{- template "relationship_to_one_helper" $rel -}} {{- template "relationship_to_one_helper" $rel -}}
{{end -}} {{end -}}
{{- end -}} {{- end -}}

View file

@ -3,6 +3,9 @@
{{- $dot := . }} {{- $dot := . }}
{{- $table := .Table }} {{- $table := .Table }}
{{- range .Table.ToManyRelationships -}} {{- range .Table.ToManyRelationships -}}
{{- if .ForeignColumnUnique -}}
{{- template "relationship_to_one_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}}
// {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} // {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
@ -21,6 +24,17 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
return o return o
} }
// {{$rel.Function.Name}}XP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}XP(exec boil.Executor, selectCols ...string) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(exec, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
// {{$rel.Function.Name}}X retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor // {{$rel.Function.Name}}X retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}X(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) { func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}X(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
@ -59,15 +73,6 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
return ret, nil return ret, nil
} }
// {{$rel.Function.Name}}XP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor {{end -}}{{- /* if unique foreign key */ -}}
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. {{- end -}}{{- /* range relationships */ -}}
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}XP(exec boil.Executor, selectCols ...string) {{$rel.ForeignTable.Slice}} {
o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(exec, selectCols...)
if err != nil {
panic(boil.WrapErr(err))
}
return o
}
{{end -}}{{- /* range relationships */ -}}
{{- end -}}{{- /* outer if join table */ -}} {{- end -}}{{- /* outer if join table */ -}}

View file

@ -3,6 +3,9 @@
{{- $dot := . }} {{- $dot := . }}
{{- $table := .Table }} {{- $table := .Table }}
{{- range .Table.ToManyRelationships -}} {{- range .Table.ToManyRelationships -}}
{{- if .ForeignColumnUnique -}}
{{- template "relationship_to_one_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}}
func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
var err error var err error
@ -75,5 +78,6 @@ func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
} }
} }
{{end -}}{{- /* range */ -}} {{end -}}{{- /* if unique */ -}}
{{- end -}}{{- /* range */ -}}
{{- end -}}{{- /* outer if join table */ -}} {{- end -}}{{- /* outer if join table */ -}}

View file

@ -12,6 +12,8 @@ func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
{{end}} {{end}}
{{if not .Function.ReverseArgs -}}
if err := foreign.InsertX(tx); err != nil { if err := foreign.InsertX(tx); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -20,14 +22,24 @@ func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
if err := local.InsertX(tx); err != nil { if err := local.InsertX(tx); err != nil {
t.Fatal(err) t.Fatal(err)
} }
{{else -}}
if err := local.InsertX(tx); err != nil {
t.Fatal(err)
}
checkForeign, err := local.{{.Function.Name}}X(tx) foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}}
if err := foreign.InsertX(tx); err != nil {
t.Fatal(err)
}
{{end -}}
check, err := local.{{.Function.Name}}X(tx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if checkForeign.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} { if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, checkForeign.{{.Function.ForeignAssignment}}) t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
} }
} }