2016-08-26 16:59:28 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- $table := .Table -}}
|
|
|
|
{{- range .Table.ToManyRelationships -}}
|
2016-09-18 08:50:31 +02:00
|
|
|
{{- $varNameSingular := .Table | singular | camelCase -}}
|
|
|
|
{{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}}
|
2016-09-23 06:47:50 +02:00
|
|
|
{{- $txt := txtsFromToMany $dot.Tables $table .}}
|
|
|
|
func test{{$txt.LocalTable.NameGo}}ToManyAddOp{{$txt.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
var a {{$txt.LocalTable.NameGo}}
|
|
|
|
var b, c, d, e {{$txt.ForeignTable.NameGo}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
seed := randomize.NewSeed()
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
foreigners := []*{{$txt.ForeignTable.NameGo}}{&b, &c, &d, &e}
|
2016-09-14 10:08:30 +02:00
|
|
|
for _, x := range foreigners {
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, x, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
foreignersSplitByInsertion := [][]*{{$txt.ForeignTable.NameGo}}{
|
2016-09-14 10:08:30 +02:00
|
|
|
{&b, &c},
|
|
|
|
{&d, &e},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, x := range foreignersSplitByInsertion {
|
2016-09-23 06:47:50 +02:00
|
|
|
err = a.Add{{$txt.Function.Name}}(tx, i != 0, x...)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
first := x[0]
|
|
|
|
second := x[1]
|
|
|
|
{{- if .ToJoinTable}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if first.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if second.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the slice")
|
|
|
|
}
|
|
|
|
{{- else}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
{{if $txt.Function.UsesBytes -}}
|
|
|
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, first.{{$txt.Function.ForeignAssignment}}) {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, first.{{$txt.Function.ForeignAssignment}})
|
2016-09-16 09:22:12 +02:00
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, second.{{$txt.Function.ForeignAssignment}}) {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, second.{{$txt.Function.ForeignAssignment}})
|
2016-09-16 09:22:12 +02:00
|
|
|
}
|
|
|
|
{{else -}}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.{{$txt.Function.LocalAssignment}} != first.{{$txt.Function.ForeignAssignment}} {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, first.{{$txt.Function.ForeignAssignment}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.{{$txt.Function.LocalAssignment}} != second.{{$txt.Function.ForeignAssignment}} {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, second.{{$txt.Function.ForeignAssignment}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if first.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the foreign slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if second.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the foreign slice")
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.R.{{$txt.Function.Name}}[i*2] != first {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship struct slice not set to correct value")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.R.{{$txt.Function.Name}}[i*2+1] != second {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship struct slice not set to correct value")
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
count, err := a.{{$txt.Function.Name}}(tx).Count()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if want := int64((i+1)*2); count != want {
|
|
|
|
t.Error("want", want, "got", count)
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
2016-09-02 10:07:11 +02:00
|
|
|
{{- if (or .ForeignColumnNullable .ToJoinTable)}}
|
2016-08-26 16:59:28 +02:00
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
func test{{$txt.LocalTable.NameGo}}ToManySetOp{{$txt.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
var a {{$txt.LocalTable.NameGo}}
|
|
|
|
var b, c, d, e {{$txt.ForeignTable.NameGo}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
seed := randomize.NewSeed()
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
foreigners := []*{{$txt.ForeignTable.NameGo}}{&b, &c, &d, &e}
|
2016-09-14 10:08:30 +02:00
|
|
|
for _, x := range foreigners {
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, x, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
err = a.Set{{$txt.Function.Name}}(tx, false, &b, &c)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
count, err := a.{{$txt.Function.Name}}(tx).Count()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 2 {
|
|
|
|
t.Error("count was wrong:", count)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
err = a.Set{{$txt.Function.Name}}(tx, true, &d, &e)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
count, err = a.{{$txt.Function.Name}}(tx).Count()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 2 {
|
|
|
|
t.Error("count was wrong:", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- if .ToJoinTable}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if len(b.R.{{$txt.Function.ForeignName}}) != 0 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if len(c.R.{{$txt.Function.ForeignName}}) != 0 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if d.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if e.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the slice")
|
|
|
|
}
|
|
|
|
{{- else}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if b.{{$txt.ForeignTable.ColumnNameGo}}.Valid {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("want b's foreign key value to be nil")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if c.{{$txt.ForeignTable.ColumnNameGo}}.Valid {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("want c's foreign key value to be nil")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
{{if $txt.Function.UsesBytes -}}
|
|
|
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, d.{{$txt.Function.ForeignAssignment}}) {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, d.{{$txt.Function.ForeignAssignment}})
|
2016-09-16 09:22:12 +02:00
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, e.{{$txt.Function.ForeignAssignment}}) {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, e.{{$txt.Function.ForeignAssignment}})
|
2016-09-16 09:22:12 +02:00
|
|
|
}
|
|
|
|
{{else -}}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.{{$txt.Function.LocalAssignment}} != d.{{$txt.Function.ForeignAssignment}} {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, d.{{$txt.Function.ForeignAssignment}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.{{$txt.Function.LocalAssignment}} != e.{{$txt.Function.ForeignAssignment}} {
|
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, e.{{$txt.Function.ForeignAssignment}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if b.R.{{$txt.Function.ForeignName}} != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the foreign struct")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if c.R.{{$txt.Function.ForeignName}} != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the foreign struct")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if d.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the foreign struct")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if e.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the foreign struct")
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.R.{{$txt.Function.Name}}[0] != &d {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship struct slice not set to correct value")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.R.{{$txt.Function.Name}}[1] != &e {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship struct slice not set to correct value")
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
func test{{$txt.LocalTable.NameGo}}ToManyRemoveOp{{$txt.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
var a {{$txt.LocalTable.NameGo}}
|
|
|
|
var b, c, d, e {{$txt.ForeignTable.NameGo}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
seed := randomize.NewSeed()
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
foreigners := []*{{$txt.ForeignTable.NameGo}}{&b, &c, &d, &e}
|
2016-09-14 10:08:30 +02:00
|
|
|
for _, x := range foreigners {
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, x, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
err = a.Add{{$txt.Function.Name}}(tx, true, foreigners...)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
count, err := a.{{$txt.Function.Name}}(tx).Count()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 4 {
|
|
|
|
t.Error("count was wrong:", count)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
err = a.Remove{{$txt.Function.Name}}(tx, foreigners[:2]...)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
count, err = a.{{$txt.Function.Name}}(tx).Count()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 2 {
|
|
|
|
t.Error("count was wrong:", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- if .ToJoinTable}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if len(b.R.{{$txt.Function.ForeignName}}) != 0 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if len(c.R.{{$txt.Function.ForeignName}}) != 0 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the slice")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if d.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the foreign struct")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if e.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not added properly to the foreign struct")
|
|
|
|
}
|
|
|
|
{{- else}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if b.{{$txt.ForeignTable.ColumnNameGo}}.Valid {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("want b's foreign key value to be nil")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if c.{{$txt.ForeignTable.ColumnNameGo}}.Valid {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("want c's foreign key value to be nil")
|
|
|
|
}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if b.R.{{$txt.Function.ForeignName}} != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the foreign struct")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if c.R.{{$txt.Function.ForeignName}} != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship was not removed properly from the foreign struct")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if d.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship to a should have been preserved")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if e.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship to a should have been preserved")
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
|
2016-09-23 06:47:50 +02:00
|
|
|
if len(a.R.{{$txt.Function.Name}}) != 2 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("should have preserved two relationships")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removal doesn't do a stable deletion for performance so we have to flip the order
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.R.{{$txt.Function.Name}}[1] != &d {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship to d should have been preserved")
|
|
|
|
}
|
2016-09-23 06:47:50 +02:00
|
|
|
if a.R.{{$txt.Function.Name}}[0] != &e {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship to e should have been preserved")
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
|
|
|
{{end -}}
|
|
|
|
{{- end -}}{{- /* range relationships */ -}}
|
|
|
|
{{- end -}}{{- /* outer if join table */ -}}
|