Remove hacks from to_one
This commit is contained in:
parent
55e0ae5736
commit
11098a95c3
5 changed files with 159 additions and 204 deletions
|
@ -1,33 +1,26 @@
|
|||
{{- define "relationship_to_one_helper" -}}
|
||||
{{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
|
||||
{{- with .Rel -}}{{/* Rel holds the text helper data, passed in through preserveDot */}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
||||
// {{.Function.Name}}G pointed to by the foreign key.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
|
||||
return {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), mods...)
|
||||
}
|
||||
|
||||
// {{.Function.Name}} pointed to by the foreign key.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) ({{$varNameSingular}}Query) {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Where("{{.ForeignTable.ColumnName}}={{if $dot.Dialect.IndexPlaceholders}}$1{{else}}?{{end}}", {{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}),
|
||||
}
|
||||
|
||||
queryMods = append(queryMods, mods...)
|
||||
|
||||
query := {{.ForeignTable.NamePluralGo}}(exec, queryMods...)
|
||||
queries.SetFrom(query.Query, "{{.ForeignTable.Name | $dot.SchemaTable}}")
|
||||
|
||||
return query
|
||||
}
|
||||
{{- end -}}{{/* end with */}}
|
||||
{{end -}}{{/* end define */}}
|
||||
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_helper" (preserveDot $dot $txt) -}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
||||
// {{$txt.Function.Name}}G pointed to by the foreign key.
|
||||
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
|
||||
return {{$txt.Function.Receiver}}.{{$txt.Function.Name}}(boil.GetDB(), mods...)
|
||||
}
|
||||
|
||||
// {{$txt.Function.Name}} pointed to by the foreign key.
|
||||
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) ({{$varNameSingular}}Query) {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Where("{{$txt.ForeignTable.ColumnName}}={{if $dot.Dialect.IndexPlaceholders}}$1{{else}}?{{end}}", {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}),
|
||||
}
|
||||
|
||||
queryMods = append(queryMods, mods...)
|
||||
|
||||
query := {{$txt.ForeignTable.NamePluralGo}}(exec, queryMods...)
|
||||
queries.SetFrom(query.Query, "{{$txt.ForeignTable.Name | $dot.SchemaTable}}")
|
||||
|
||||
return query
|
||||
}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
{{- define "relationship_to_one_eager_helper" -}}
|
||||
{{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
|
||||
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
|
||||
{{- with .Rel -}}
|
||||
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
|
||||
{{- $slice := printf "%sSlice" .LocalTable.NameGo}}
|
||||
// Load{{.Function.Name}} allows an eager lookup of values, cached into the
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
|
||||
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
|
||||
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo}}
|
||||
// Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
|
||||
// loaded structs of the objects.
|
||||
func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{.LocalTable.NameGo}}
|
||||
var object *{{.LocalTable.NameGo}}
|
||||
func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{$txt.LocalTable.NameGo}}
|
||||
var object *{{$txt.LocalTable.NameGo}}
|
||||
|
||||
count := 1
|
||||
if singular {
|
||||
object = {{$arg}}.(*{{.LocalTable.NameGo}})
|
||||
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
|
||||
} else {
|
||||
slice = *{{$arg}}.(*{{$slice}})
|
||||
count = len(slice)
|
||||
|
@ -20,10 +22,10 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
|
|||
|
||||
args := make([]interface{}, count)
|
||||
if singular {
|
||||
args[0] = object.{{.LocalTable.ColumnNameGo}}
|
||||
args[0] = object.{{$txt.LocalTable.ColumnNameGo}}
|
||||
} else {
|
||||
for i, obj := range slice {
|
||||
args[i] = obj.{{.LocalTable.ColumnNameGo}}
|
||||
args[i] = obj.{{$txt.LocalTable.ColumnNameGo}}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,17 +40,17 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
|
|||
|
||||
results, err := e.Query(query, args...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to eager load {{.ForeignTable.NameGo}}")
|
||||
return errors.Wrap(err, "failed to eager load {{$txt.ForeignTable.NameGo}}")
|
||||
}
|
||||
defer results.Close()
|
||||
|
||||
var resultSlice []*{{.ForeignTable.NameGo}}
|
||||
var resultSlice []*{{$txt.ForeignTable.NameGo}}
|
||||
if err = queries.Bind(results, &resultSlice); err != nil {
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable.NameGo}}")
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice {{$txt.ForeignTable.NameGo}}")
|
||||
}
|
||||
|
||||
{{if not $dot.NoHooks -}}
|
||||
if len({{.ForeignTable.Name | singular | camelCase}}AfterSelectHooks) != 0 {
|
||||
if len({{$txt.ForeignTable.Name | singular | camelCase}}AfterSelectHooks) != 0 {
|
||||
for _, obj := range resultSlice {
|
||||
if err := obj.doAfterSelectHooks(e); err != nil {
|
||||
return err
|
||||
|
@ -61,21 +63,21 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
|
|||
if object.R == nil {
|
||||
object.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
object.R.{{.Function.Name}} = resultSlice[0]
|
||||
object.R.{{$txt.Function.Name}} = resultSlice[0]
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, foreign := range resultSlice {
|
||||
for _, local := range slice {
|
||||
{{if .Function.UsesBytes -}}
|
||||
if 0 == bytes.Compare(local.{{.Function.LocalAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
|
||||
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if local.{{.Function.LocalAssignment}} == foreign.{{.Function.ForeignAssignment}} {
|
||||
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
if local.R == nil {
|
||||
local.R = &{{$varNameSingular}}R{}
|
||||
}
|
||||
local.R.{{.Function.Name}} = foreign
|
||||
local.R.{{$txt.Function.Name}} = foreign
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -83,14 +85,5 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo
|
|||
|
||||
return nil
|
||||
}
|
||||
{{- end -}}{{- /* end with */ -}}
|
||||
{{end -}}{{- /* end define */ -}}
|
||||
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_eager_helper" (preserveDot $dot $txt) -}}
|
||||
{{- end -}}
|
||||
{{end}}
|
||||
{{end -}}{{/* range */}}
|
||||
{{end}}{{/* join table */}}
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
{{- define "relationship_to_one_setops_helper" -}}
|
||||
{{- $dot := .Dot -}}{{/* .Dot holds the root templateData struct, passed in through preserveDot */}}
|
||||
{{- with .Rel -}}
|
||||
{{- /* Begin execution of template for one-to-one setops */ -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
||||
{{- $localNameSingular := .ForeignKey.Table | singular | camelCase}}
|
||||
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
||||
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to related.
|
||||
// Adds {{.Function.Receiver}} to related.R.{{.Function.ForeignName}}.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec boil.Executor, insert bool, related *{{.ForeignTable.NameGo}}) error {
|
||||
// Set{{$txt.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
||||
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to related.
|
||||
// Adds {{$txt.Function.Receiver}} to related.R.{{$txt.Function.ForeignName}}.
|
||||
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) error {
|
||||
var err error
|
||||
if insert {
|
||||
if err = related.Insert(exec); err != nil {
|
||||
|
@ -14,101 +17,101 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
|||
}
|
||||
}
|
||||
|
||||
{{if .Function.OneToOne -}}
|
||||
oldVal := related.{{.Function.ForeignAssignment}}
|
||||
related.{{.Function.ForeignAssignment}} = {{.Function.Receiver}}.{{.Function.LocalAssignment}}
|
||||
{{if$txt.Function.OneToOne -}}
|
||||
oldVal := related.{{$txt.Function.ForeignAssignment}}
|
||||
related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
|
||||
{{if .ForeignKey.ForeignColumnNullable -}}
|
||||
related.{{.ForeignTable.ColumnNameGo}}.Valid = true
|
||||
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
|
||||
{{end -}}
|
||||
if err = related.Update(exec, "{{.ForeignKey.ForeignColumn}}"); err != nil {
|
||||
related.{{.Function.ForeignAssignment}} = oldVal
|
||||
related.{{$txt.Function.ForeignAssignment}} = oldVal
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
{{else -}}
|
||||
oldVal := {{.Function.Receiver}}.{{.Function.LocalAssignment}}
|
||||
related.{{.Function.ForeignAssignment}} = {{.Function.Receiver}}.{{.Function.LocalAssignment}}
|
||||
{{.Function.Receiver}}.{{.Function.LocalAssignment}} = related.{{.Function.ForeignAssignment}}
|
||||
if err = {{.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
||||
{{.Function.Receiver}}.{{.Function.LocalAssignment}} = oldVal
|
||||
oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
|
||||
related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}
|
||||
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}}
|
||||
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
||||
{{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
{{end -}}
|
||||
|
||||
if {{.Function.Receiver}}.R == nil {
|
||||
{{.Function.Receiver}}.R = &{{$localNameSingular}}R{
|
||||
{{.Function.Name}}: related,
|
||||
if {{$txt.Function.Receiver}}.R == nil {
|
||||
{{$txt.Function.Receiver}}.R = &{{$localNameSingular}}R{
|
||||
{{$txt.Function.Name}}: related,
|
||||
}
|
||||
} else {
|
||||
{{.Function.Receiver}}.R.{{.Function.Name}} = related
|
||||
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = related
|
||||
}
|
||||
|
||||
{{if (or .ForeignKey.Unique .Function.OneToOne) -}}
|
||||
{{if (or .ForeignKey.Unique$txt.Function.OneToOne) -}}
|
||||
if related.R == nil {
|
||||
related.R = &{{$varNameSingular}}R{
|
||||
{{.Function.ForeignName}}: {{.Function.Receiver}},
|
||||
{{$txt.Function.ForeignName}}: {{$txt.Function.Receiver}},
|
||||
}
|
||||
} else {
|
||||
related.R.{{.Function.ForeignName}} = {{.Function.Receiver}}
|
||||
related.R.{{$txt.Function.ForeignName}} = {{$txt.Function.Receiver}}
|
||||
}
|
||||
{{else -}}
|
||||
if related.R == nil {
|
||||
related.R = &{{$varNameSingular}}R{
|
||||
{{.Function.ForeignName}}: {{.LocalTable.NameGo}}Slice{{"{"}}{{.Function.Receiver}}{{"}"}},
|
||||
{{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}{{$txt.Function.Receiver}}{{"}"}},
|
||||
}
|
||||
} else {
|
||||
related.R.{{.Function.ForeignName}} = append(related.R.{{.Function.ForeignName}}, {{.Function.Receiver}})
|
||||
related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, {{$txt.Function.Receiver}})
|
||||
}
|
||||
{{end -}}
|
||||
|
||||
{{if .ForeignKey.Nullable}}
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}.Valid = true
|
||||
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
|
||||
{{end -}}
|
||||
return nil
|
||||
}
|
||||
|
||||
{{- if or (.ForeignKey.Nullable) (and .Function.OneToOne .ForeignKey.ForeignColumnNullable)}}
|
||||
// Remove{{.Function.Name}} relationship.
|
||||
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to nil.
|
||||
// Removes {{.Function.Receiver}} from all passed in related items' relationships struct (Optional).
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(exec boil.Executor, related *{{.ForeignTable.NameGo}}) error {
|
||||
{{- if or (.ForeignKey.Nullable) (and$txt.Function.OneToOne .ForeignKey.ForeignColumnNullable)}}
|
||||
// Remove{{$txt.Function.Name}} relationship.
|
||||
// Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to nil.
|
||||
// Removes {{$txt.Function.Receiver}} from all passed in related items' relationships struct (Optional).
|
||||
func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error {
|
||||
var err error
|
||||
|
||||
{{if .Function.OneToOne -}}
|
||||
related.{{.ForeignTable.ColumnNameGo}}.Valid = false
|
||||
{{if$txt.Function.OneToOne -}}
|
||||
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false
|
||||
if err = related.Update(exec, "{{.ForeignKey.ForeignColumn}}"); err != nil {
|
||||
related.{{.ForeignTable.ColumnNameGo}}.Valid = true
|
||||
related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
{{else -}}
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}.Valid = false
|
||||
if err = {{.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
||||
{{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}.Valid = true
|
||||
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false
|
||||
if err = {{$txt.Function.Receiver}}.Update(exec, "{{.ForeignKey.Column}}"); err != nil {
|
||||
{{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
{{end -}}
|
||||
|
||||
{{.Function.Receiver}}.R.{{.Function.Name}} = nil
|
||||
{{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = nil
|
||||
if related == nil || related.R == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
{{if .ForeignKey.Unique -}}
|
||||
related.R.{{.Function.ForeignName}} = nil
|
||||
related.R.{{$txt.Function.ForeignName}} = nil
|
||||
{{else -}}
|
||||
for i, ri := range related.R.{{.Function.ForeignName}} {
|
||||
{{if .Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare({{.Function.Receiver}}.{{.Function.LocalAssignment}}, ri.{{.Function.LocalAssignment}}) {
|
||||
for i, ri := range related.R.{{$txt.Function.ForeignName}} {
|
||||
{{if $txt.Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare({{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}, ri.{{$txt.Function.LocalAssignment}}) {
|
||||
{{else -}}
|
||||
if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} {
|
||||
if {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} != ri.{{$txt.Function.LocalAssignment}} {
|
||||
{{end -}}
|
||||
continue
|
||||
}
|
||||
|
||||
ln := len(related.R.{{.Function.ForeignName}})
|
||||
ln := len(related.R.{{$txt.Function.ForeignName}})
|
||||
if ln > 1 && i < ln-1 {
|
||||
related.R.{{.Function.ForeignName}}[i] = related.R.{{.Function.ForeignName}}[ln-1]
|
||||
related.R.{{$txt.Function.ForeignName}}[i] = related.R.{{$txt.Function.ForeignName}}[ln-1]
|
||||
}
|
||||
related.R.{{.Function.ForeignName}} = related.R.{{.Function.ForeignName}}[:ln-1]
|
||||
related.R.{{$txt.Function.ForeignName}} = related.R.{{$txt.Function.ForeignName}}[:ln-1]
|
||||
break
|
||||
}
|
||||
{{end -}}
|
||||
|
@ -116,15 +119,5 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e
|
|||
return nil
|
||||
}
|
||||
{{end -}}{{/* if foreignkey nullable */}}
|
||||
{{- end -}}{{/* end with */}}
|
||||
{{- end -}}{{/* end define */}}
|
||||
|
||||
{{- /* Begin execution of template for one-to-one setops */ -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_setops_helper" (preserveDot $dot $txt) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}{{/* range */}}
|
||||
{{- end -}}{{/* join table */}}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
{{- define "relationship_to_one_test_helper"}}
|
||||
{{- $dot := .Dot -}}
|
||||
{{- with .Rel}}
|
||||
func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
func test{{$txt.LocalTable.NameGo}}ToOne{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) {
|
||||
tx := MustTx(boil.Begin())
|
||||
defer tx.Rollback()
|
||||
|
||||
var foreign {{.ForeignTable.NameGo}}
|
||||
var local {{.LocalTable.NameGo}}
|
||||
var foreign {{$txt.ForeignTable.NameGo}}
|
||||
var local {{$txt.LocalTable.NameGo}}
|
||||
{{if .ForeignKey.Nullable -}}
|
||||
local.{{.ForeignKey.Column | titleCase}}.Valid = true
|
||||
{{end}}
|
||||
|
@ -14,12 +16,12 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
|
|||
foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
|
||||
{{end}}
|
||||
|
||||
{{if not .Function.OneToOne -}}
|
||||
{{if not $txt.Function.OneToOne -}}
|
||||
if err := foreign.Insert(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
local.{{.Function.LocalAssignment}} = foreign.{{.Function.ForeignAssignment}}
|
||||
local.{{$txt.Function.LocalAssignment}} = foreign.{{$txt.Function.ForeignAssignment}}
|
||||
if err := local.Insert(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -28,49 +30,40 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}}
|
||||
foreign.{{$txt.Function.ForeignAssignment}} = local.{{$txt.Function.LocalAssignment}}
|
||||
if err := foreign.Insert(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
{{end -}}
|
||||
|
||||
check, err := local.{{.Function.Name}}(tx).One()
|
||||
check, err := local.{{$txt.Function.Name}}(tx).One()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
{{if .Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare(check.{{.Function.ForeignAssignment}}, foreign.{{.Function.ForeignAssignment}}) {
|
||||
{{if $txt.Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare(check.{{$txt.Function.ForeignAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} {
|
||||
if check.{{$txt.Function.ForeignAssignment}} != foreign.{{$txt.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}})
|
||||
t.Errorf("want: %v, got %v", foreign.{{$txt.Function.ForeignAssignment}}, check.{{$txt.Function.ForeignAssignment}})
|
||||
}
|
||||
|
||||
slice := {{.LocalTable.NameGo}}Slice{&local}
|
||||
if err = local.L.Load{{.Function.Name}}(tx, false, &slice); err != nil {
|
||||
slice := {{$txt.LocalTable.NameGo}}Slice{&local}
|
||||
if err = local.L.Load{{$txt.Function.Name}}(tx, false, &slice); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.{{.Function.Name}} == nil {
|
||||
if local.R.{{$txt.Function.Name}} == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
|
||||
local.R.{{.Function.Name}} = nil
|
||||
if err = local.L.Load{{.Function.Name}}(tx, true, &local); err != nil {
|
||||
local.R.{{$txt.Function.Name}} = nil
|
||||
if err = local.L.Load{{$txt.Function.Name}}(tx, true, &local); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if local.R.{{.Function.Name}} == nil {
|
||||
if local.R.{{$txt.Function.Name}} == nil {
|
||||
t.Error("struct should have been eager loaded")
|
||||
}
|
||||
}
|
||||
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_test_helper" (preserveDot $dot $txt) -}}
|
||||
{{end -}}
|
||||
{{- end -}}
|
||||
{{end -}}{{/* range */}}
|
||||
{{- end -}}{{/* join table */}}
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
{{- define "relationship_to_one_setops_test_helper" -}}
|
||||
{{- $dot := .Dot -}}
|
||||
{{- with .Rel -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}}
|
||||
{{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}}
|
||||
{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
|
||||
func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
func test{{$txt.LocalTable.NameGo}}ToOneSetOp{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) {
|
||||
var err error
|
||||
|
||||
tx := MustTx(boil.Begin())
|
||||
defer tx.Rollback()
|
||||
|
||||
var a {{.LocalTable.NameGo}}
|
||||
var b {{.ForeignTable.NameGo}}
|
||||
{{if not .Function.OneToOne -}}
|
||||
var c {{.ForeignTable.NameGo}}
|
||||
{{- end}}
|
||||
var a {{$txt.LocalTable.NameGo}}
|
||||
var b, c {{$txt.ForeignTable.NameGo}}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
||||
|
@ -22,11 +21,9 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
|||
if err = randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
{{if not .Function.OneToOne -}}
|
||||
if err = randomize.Struct(seed, &c, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
if err := a.Insert(tx); err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -35,26 +32,26 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i, x := range []*{{.ForeignTable.NameGo}}{&b{{if not .Function.OneToOne}}, &c{{end}}} {
|
||||
err = a.Set{{.Function.Name}}(tx, i != 0, x)
|
||||
for i, x := range []*{{$txt.ForeignTable.NameGo}}{&b, &c} {
|
||||
err = a.Set{{$txt.Function.Name}}(tx, i != 0, x)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
{{if .Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
|
||||
{{if $txt.Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
||||
if a.{{$txt.Function.LocalAssignment}} != x.{{$txt.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}})
|
||||
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}})
|
||||
}
|
||||
if a.R.{{.Function.Name}} != x {
|
||||
if a.R.{{$txt.Function.Name}} != x {
|
||||
t.Error("relationship struct not set to correct value")
|
||||
}
|
||||
|
||||
{{if .Function.OneToOne -}}
|
||||
zero := reflect.Zero(reflect.TypeOf(x.{{.Function.ForeignAssignment}}))
|
||||
reflect.Indirect(reflect.ValueOf(&x.{{.Function.ForeignAssignment}})).Set(zero)
|
||||
{{if $txt.Function.OneToOne -}}
|
||||
zero := reflect.Zero(reflect.TypeOf(x.{{$txt.Function.ForeignAssignment}}))
|
||||
reflect.Indirect(reflect.ValueOf(&x.{{$txt.Function.ForeignAssignment}})).Set(zero)
|
||||
|
||||
xrel := x.R
|
||||
if err = x.Reload(tx); err != nil {
|
||||
|
@ -62,43 +59,43 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
|||
}
|
||||
x.R = xrel
|
||||
{{else -}}
|
||||
zero := reflect.Zero(reflect.TypeOf(a.{{.Function.LocalAssignment}}))
|
||||
reflect.Indirect(reflect.ValueOf(&a.{{.Function.LocalAssignment}})).Set(zero)
|
||||
zero := reflect.Zero(reflect.TypeOf(a.{{$txt.Function.LocalAssignment}}))
|
||||
reflect.Indirect(reflect.ValueOf(&a.{{$txt.Function.LocalAssignment}})).Set(zero)
|
||||
|
||||
if err = a.Reload(tx); err != nil {
|
||||
t.Fatal("failed to reload", err)
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
{{if .Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
|
||||
{{if $txt.Function.UsesBytes -}}
|
||||
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
|
||||
{{else -}}
|
||||
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
||||
if a.{{$txt.Function.LocalAssignment}} != x.{{$txt.Function.ForeignAssignment}} {
|
||||
{{end -}}
|
||||
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}})
|
||||
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}})
|
||||
}
|
||||
|
||||
{{if .ForeignKey.Unique -}}
|
||||
if x.R.{{.Function.ForeignName}} != &a {
|
||||
if x.R.{{$txt.Function.ForeignName}} != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
{{else -}}
|
||||
if x.R.{{.Function.ForeignName}}[0] != &a {
|
||||
if x.R.{{$txt.Function.ForeignName}}[0] != &a {
|
||||
t.Error("failed to append to foreign relationship struct")
|
||||
}
|
||||
{{end -}}
|
||||
}
|
||||
}
|
||||
{{- if or (.ForeignKey.Nullable) (and .Function.OneToOne .ForeignKey.ForeignColumnNullable)}}
|
||||
{{- if or (.ForeignKey.Nullable) (and $txt.Function.OneToOne .ForeignKey.ForeignColumnNullable)}}
|
||||
|
||||
func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
||||
func test{{$txt.LocalTable.NameGo}}ToOneRemoveOp{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) {
|
||||
var err error
|
||||
|
||||
tx := MustTx(boil.Begin())
|
||||
defer tx.Rollback()
|
||||
|
||||
var a {{.LocalTable.NameGo}}
|
||||
var b {{.ForeignTable.NameGo}}
|
||||
var a {{$txt.LocalTable.NameGo}}
|
||||
var b {{$txt.ForeignTable.NameGo}}
|
||||
|
||||
seed := randomize.NewSeed()
|
||||
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
||||
|
@ -112,15 +109,15 @@ func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = a.Set{{.Function.Name}}(tx, true, &b); err != nil {
|
||||
if err = a.Set{{$txt.Function.Name}}(tx, true, &b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = a.Remove{{.Function.Name}}(tx, &b); err != nil {
|
||||
if err = a.Remove{{$txt.Function.Name}}(tx, &b); err != nil {
|
||||
t.Error("failed to remove relationship")
|
||||
}
|
||||
|
||||
count, err := a.{{.Function.Name}}(tx).Count()
|
||||
count, err := a.{{$txt.Function.Name}}(tx).Count()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
@ -128,38 +125,24 @@ func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function
|
|||
t.Error("want no relationships remaining")
|
||||
}
|
||||
|
||||
if a.R.{{.Function.Name}} != nil {
|
||||
if a.R.{{$txt.Function.Name}} != nil {
|
||||
t.Error("R struct entry should be nil")
|
||||
}
|
||||
|
||||
{{if .Function.OneToOne -}}
|
||||
if b.{{.ForeignTable.ColumnNameGo}}.Valid {
|
||||
if a.{{$txt.LocalTable.ColumnNameGo}}.Valid {
|
||||
t.Error("R struct entry should be nil")
|
||||
}
|
||||
{{else -}}
|
||||
if a.{{.LocalTable.ColumnNameGo}}.Valid {
|
||||
t.Error("R struct entry should be nil")
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
{{if .ForeignKey.Unique -}}
|
||||
if b.R.{{.Function.ForeignName}} != nil {
|
||||
if b.R.{{$txt.Function.ForeignName}} != nil {
|
||||
t.Error("failed to remove a from b's relationships")
|
||||
}
|
||||
{{else -}}
|
||||
if len(b.R.{{.Function.ForeignName}}) != 0 {
|
||||
if len(b.R.{{$txt.Function.ForeignName}}) != 0 {
|
||||
t.Error("failed to remove a from b's relationships")
|
||||
}
|
||||
{{- end}}
|
||||
}
|
||||
{{end -}}{{/* end if foreign key nullable */}}
|
||||
{{- end -}}{{/* with rel */}}
|
||||
{{- end -}}{{/* define */}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}}
|
||||
{{template "relationship_to_one_setops_test_helper" (preserveDot $dot $txt) -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}{{/* range */}}
|
||||
{{- end -}}{{/* join table */}}
|
||||
|
|
Loading…
Add table
Reference in a new issue