Near completion on the ToManyAdd setop

This commit is contained in:
Aaron L 2016-08-28 16:36:34 -07:00
parent e46daf0991
commit 4e70c9bb17
2 changed files with 46 additions and 27 deletions

View file

@ -24,11 +24,11 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
if err = rel.Insert(exec); err != nil {
return errors.Wrap(err, "failed to insert into foreign table")
}
} else {
}{{if not .ToJoinTable}} else {
if err = rel.Update(exec, "{{.ForeignColumn}}"); err != nil {
return errors.Wrap(err, "failed to update foreign table")
}
}
}{{end -}}
}
{{if .ToJoinTable -}}
@ -56,6 +56,28 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function
{{$rel.Function.Receiver}}.R.{{$rel.Function.Name}} = append({{$rel.Function.Receiver}}.R.{{$rel.Function.Name}}, related...)
}
{{if .ToJoinTable -}}
for _, rel := range related {
if rel.R == nil {
rel.R = &{{$rel.ForeignTable.NameGo}}R{
{{$rel.Function.ForeignName}}: {{$rel.LocalTable.NameGo}}Slice{{"{"}}{{$rel.Function.Receiver}}{{"}"}},
}
} else {
rel.R.{{$rel.Function.ForeignName}} = append(rel.R.{{$rel.Function.ForeignName}}, {{$rel.Function.Receiver}})
}
}
{{else -}}
for _, rel := range related {
if rel.R == nil {
rel.R = &{{$rel.ForeignTable.NameGo}}R{
{{$rel.Function.ForeignName}}: {{$rel.Function.Receiver}},
}
} else {
rel.R.{{$rel.Function.ForeignName}} = {{$rel.Function.Receiver}}
}
}
{{end -}}
return nil
}
{{- if .ForeignColumnNullable}}

View file

@ -3,15 +3,14 @@
{{- $dot := . -}}
{{- $table := .Table -}}
{{- range .Table.ToManyRelationships -}}
{{- $varNameSingular := .Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
{{- template "relationship_to_one_setops_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
{{- else -}}
{{- $varNameSingular := .Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
{{- $rel := textsFromRelationship $dot.Tables $table .}}
func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) {
t.Skip("WIP")
var err error
tx := MustTx(boil.Begin())
@ -45,33 +44,24 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
{&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]
first := x[0]
second := x[1]
{{- if .ToJoinTable}}
{{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 first.R.{{$rel.Function.ForeignName}}[i] != &a {
t.Error("relationship was not added properly to the slice")
}
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 second.R.{{$rel.Function.ForeignName}}[i+1] != &a {
t.Error("relationship was not added properly to the slice")
}
{{- else}}
if a.{{$rel.Function.LocalAssignment}} != first.{{$rel.Function.ForeignAssignment}} {
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, first.{{$rel.Function.ForeignAssignment}})
@ -79,13 +69,20 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
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 {
if first.R.{{$rel.Function.ForeignName}} != &a {
t.Error("relationship was not added properly to the foreign slice")
}
if second.R.{{$rel.Function.ForeignName}} != &a {
t.Error("relationship was not added properly to the foreign slice")
}
{{- end}}
if a.R.{{$rel.Function.Name}}[i*2] != 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")
if a.R.{{$rel.Function.Name}}[i*2+1] != second {
t.Error("relationship struct slice not set to correct value")
}
count, err := a.{{$rel.Function.Name}}(tx).Count()