From bc711472820da6da65677a7178c3a9d6e8b33843 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sat, 27 Aug 2016 19:10:46 -0700 Subject: [PATCH] WIP - Trying to fix bugs --- templates/relationship_to_many_setops.tpl | 42 +++++++++ .../relationship_to_many_setops.tpl | 87 ++++++++++++++++++- templates_test/relationship_to_one_setops.tpl | 10 +++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/templates/relationship_to_many_setops.tpl b/templates/relationship_to_many_setops.tpl index 3fe2701..394e2c0 100644 --- a/templates/relationship_to_many_setops.tpl +++ b/templates/relationship_to_many_setops.tpl @@ -13,6 +13,48 @@ // of the {{$table.Name | singular}}, optionally inserting them as new records. // 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 { + 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 } {{- if .ForeignColumnNullable}} diff --git a/templates_test/relationship_to_many_setops.tpl b/templates_test/relationship_to_many_setops.tpl index 05afbef..28dbb5d 100644 --- a/templates_test/relationship_to_many_setops.tpl +++ b/templates_test/relationship_to_many_setops.tpl @@ -3,13 +3,98 @@ {{- $dot := . -}} {{- $table := .Table -}} {{- range .Table.ToManyRelationships -}} - {{- $varNameSingular := .ForeignTable | singular | camelCase -}} + {{- $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 -}} {{- $rel := textsFromRelationship $dot.Tables $table .}} 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}} diff --git a/templates_test/relationship_to_one_setops.tpl b/templates_test/relationship_to_one_setops.tpl index 74f5404..3af9ba3 100644 --- a/templates_test/relationship_to_one_setops.tpl +++ b/templates_test/relationship_to_one_setops.tpl @@ -51,6 +51,16 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na if 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}}