WIP - Trying to fix bugs
This commit is contained in:
parent
d778401a7b
commit
bc71147282
3 changed files with 138 additions and 1 deletions
|
@ -13,6 +13,48 @@
|
||||||
// of the {{$table.Name | singular}}, optionally inserting them as new records.
|
// of the {{$table.Name | singular}}, optionally inserting them as new records.
|
||||||
// Appends related to R.{{$rel.Function.Name}}.
|
// Appends related to R.{{$rel.Function.Name}}.
|
||||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function.Name}}(exec boil.Executor, insert bool, related ...*{{$rel.ForeignTable.NameGo}}) error {
|
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function.Name}}(exec boil.Executor, insert bool, related ...*{{$rel.ForeignTable.NameGo}}) error {
|
||||||
|
var err error
|
||||||
|
for _, rel := range related {
|
||||||
|
rel.{{$rel.Function.ForeignAssignment}} = {{$rel.Function.Receiver}}.{{$rel.Function.LocalAssignment}}
|
||||||
|
{{if .ForeignColumnNullable -}}
|
||||||
|
rel.{{$rel.ForeignTable.ColumnNameGo}}.Valid = true
|
||||||
|
{{end -}}
|
||||||
|
if insert {
|
||||||
|
if err = rel.Insert(exec); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to insert into foreign table")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err = rel.Update(exec, "{{.ForeignColumn}}"); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to update foreign table")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{{if .ToJoinTable -}}
|
||||||
|
for _, rel := range related {
|
||||||
|
query := `insert into "{{.JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`
|
||||||
|
values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}, rel.{{$rel.ForeignTable.ColumnNameGo}}}
|
||||||
|
|
||||||
|
if boil.DebugMode {
|
||||||
|
fmt.Fprintln(boil.DebugWriter, query)
|
||||||
|
fmt.Fprintln(boil.DebugWriter, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = exec.Exec(query, values...)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to insert into join table")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{{end -}}
|
||||||
|
|
||||||
|
if {{$rel.Function.Receiver}}.R == nil {
|
||||||
|
{{$rel.Function.Receiver}}.R = &{{$rel.LocalTable.NameGo}}R{
|
||||||
|
{{$rel.Function.Name}}: related,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
{{$rel.Function.Receiver}}.R.{{$rel.Function.Name}} = append({{$rel.Function.Receiver}}.R.{{$rel.Function.Name}}, related...)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
{{- if .ForeignColumnNullable}}
|
{{- if .ForeignColumnNullable}}
|
||||||
|
|
|
@ -3,13 +3,98 @@
|
||||||
{{- $dot := . -}}
|
{{- $dot := . -}}
|
||||||
{{- $table := .Table -}}
|
{{- $table := .Table -}}
|
||||||
{{- range .Table.ToManyRelationships -}}
|
{{- range .Table.ToManyRelationships -}}
|
||||||
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
|
{{- $varNameSingular := .Table | singular | camelCase -}}
|
||||||
|
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
|
||||||
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
|
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
|
||||||
{{- template "relationship_to_one_setops_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
|
{{- template "relationship_to_one_setops_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
{{- $rel := textsFromRelationship $dot.Tables $table .}}
|
{{- $rel := textsFromRelationship $dot.Tables $table .}}
|
||||||
|
|
||||||
func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) {
|
func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
tx := MustTx(boil.Begin())
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
var a {{$rel.LocalTable.NameGo}}
|
||||||
|
var b, c, d, e {{$rel.ForeignTable.NameGo}}
|
||||||
|
|
||||||
|
seed := randomize.NewSeed()
|
||||||
|
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
foreigners := []*{{$rel.ForeignTable.NameGo}}{&b, &c, &d, &e}
|
||||||
|
for _, x := range foreigners {
|
||||||
|
if err = randomize.Struct(seed, x, {{$foreignVarNameSingular}}DBTypes, false, {{$foreignVarNameSingular}}PrimaryKeyColumns...); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := a.Insert(tx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err = b.Insert(tx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err = c.Insert(tx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
foreignersSplitByInsertion := [][]*{{$rel.ForeignTable.NameGo}}{
|
||||||
|
{&b, &c},
|
||||||
|
{&d, &e},
|
||||||
|
}
|
||||||
|
for i, x := range foreignersSplitByInsertion {
|
||||||
|
err = a.Add{{$rel.Function.Name}}(tx, i != 0, x...)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
first := foreigners[i*2]
|
||||||
|
second := foreigners[i*2]
|
||||||
|
|
||||||
|
{{if not .ToJoinTable -}}
|
||||||
|
if a.{{$rel.Function.LocalAssignment}} != first.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}})
|
||||||
|
}
|
||||||
|
if a.{{$rel.Function.LocalAssignment}} != second.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}})
|
||||||
|
}
|
||||||
|
|
||||||
|
zero := reflect.Zero(reflect.TypeOf(first.{{$rel.Function.ForeignAssignment}}))
|
||||||
|
reflect.Indirect(reflect.ValueOf(&first.{{$rel.Function.ForeignAssignment}})).Set(zero)
|
||||||
|
reflect.Indirect(reflect.ValueOf(&second.{{$rel.Function.ForeignAssignment}})).Set(zero)
|
||||||
|
|
||||||
|
if err = first.Reload(tx); err != nil {
|
||||||
|
t.Fatal("failed to reload", err)
|
||||||
|
}
|
||||||
|
if err = second.Reload(tx); err != nil {
|
||||||
|
t.Fatal("failed to reload", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.{{$rel.Function.LocalAssignment}} != first.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}})
|
||||||
|
}
|
||||||
|
if a.{{$rel.Function.LocalAssignment}} != second.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, second.{{$rel.Function.ForeignAssignment}})
|
||||||
|
}
|
||||||
|
{{end -}}
|
||||||
|
|
||||||
|
if a.R.{{$rel.Function.Name}}[0] != first {
|
||||||
|
t.Error("relationship struct slice not set to correct value")
|
||||||
|
}
|
||||||
|
if a.R.{{$rel.Function.Name}}[1] != second {
|
||||||
|
t.Error("relationship slice struct not set to correct value")
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := a.{{$rel.Function.Name}}(tx).Count()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if want := int64((i+1)*2); count != want {
|
||||||
|
t.Error("want", want, "got", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{{if .ForeignColumnNullable}}
|
{{if .ForeignColumnNullable}}
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,16 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
||||||
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
||||||
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}})
|
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if .ForeignKey.Unique -}}
|
||||||
|
if x.R.{{.Function.ForeignName}} != &a {
|
||||||
|
t.Error("failed to append to foreign relationship struct")
|
||||||
|
}
|
||||||
|
{{else -}}
|
||||||
|
if x.R.{{.Function.ForeignName}}[0] != &a {
|
||||||
|
t.Error("failed to append to foreign relationship struct")
|
||||||
|
}
|
||||||
|
{{end -}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
{{- if .ForeignKey.Nullable}}
|
{{- if .ForeignKey.Nullable}}
|
||||||
|
|
Loading…
Reference in a new issue