Add ToManySet test.
This commit is contained in:
parent
4ba1849297
commit
128ecb4a77
1 changed files with 104 additions and 0 deletions
|
@ -97,6 +97,110 @@ func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.
|
||||||
{{if .ForeignColumnNullable}}
|
{{if .ForeignColumnNullable}}
|
||||||
|
|
||||||
func test{{$rel.LocalTable.NameGo}}ToManySetOp{{$rel.Function.Name}}(t *testing.T) {
|
func test{{$rel.LocalTable.NameGo}}ToManySetOp{{$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)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = a.Add{{$rel.Function.Name}}(tx, false, &b, &c)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := a.{{$rel.Function.Name}}(tx).Count()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if count != 2 {
|
||||||
|
t.Error("count was wrong:", 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = a.Set{{$rel.Function.Name}}(tx, true, &d, &e)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err = a.{{$rel.Function.Name}}(tx).Count()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if count != 2 {
|
||||||
|
t.Error("count was wrong:", 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
{{- if .ToJoinTable}}
|
||||||
|
|
||||||
|
if len(b.R.{{$rel.Function.ForeignName}}) != 0 {
|
||||||
|
t.Error("relationship was not removed properly from the slice")
|
||||||
|
}
|
||||||
|
if len(c.R.{{$rel.Function.ForeignName}}) != 0 {
|
||||||
|
t.Error("relationship was not removed properly from the slice")
|
||||||
|
}
|
||||||
|
if d.R.{{$rel.Function.ForeignName}}[0] != &a {
|
||||||
|
t.Error("relationship was not added properly to the slice")
|
||||||
|
}
|
||||||
|
if e.R.{{$rel.Function.ForeignName}}[0] != &a {
|
||||||
|
t.Error("relationship was not added properly to the slice")
|
||||||
|
}
|
||||||
|
{{- else}}
|
||||||
|
|
||||||
|
if b.{{$rel.ForeignTable.ColumnNameGo}}.Valid {
|
||||||
|
t.Error("want b's foreign key value to be nil")
|
||||||
|
}
|
||||||
|
if c.{{$rel.ForeignTable.ColumnNameGo}}.Valid {
|
||||||
|
t.Error("want c's foreign key value to be nil")
|
||||||
|
}
|
||||||
|
if a.{{$rel.Function.LocalAssignment}} != d.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, d.{{$rel.Function.ForeignAssignment}})
|
||||||
|
}
|
||||||
|
if a.{{$rel.Function.LocalAssignment}} != e.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
t.Error("foreign key was wrong value", a.{{$rel.Function.LocalAssignment}}, e.{{$rel.Function.ForeignAssignment}})
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.R.{{$rel.Function.ForeignName}} != nil {
|
||||||
|
t.Error("relationship was not removed properly from the foreign slice")
|
||||||
|
}
|
||||||
|
if c.R.{{$rel.Function.ForeignName}} != nil {
|
||||||
|
t.Error("relationship was not removed properly from the foreign slice")
|
||||||
|
}
|
||||||
|
if d.R.{{$rel.Function.ForeignName}} != &a {
|
||||||
|
t.Error("relationship was not added properly to the foreign slice")
|
||||||
|
}
|
||||||
|
if e.R.{{$rel.Function.ForeignName}} != &a {
|
||||||
|
t.Error("relationship was not added properly to the foreign slice")
|
||||||
|
}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
if a.R.{{$rel.Function.Name}}[0] != &d {
|
||||||
|
t.Error("relationship struct slice not set to correct value")
|
||||||
|
}
|
||||||
|
if a.R.{{$rel.Function.Name}}[1] != &e {
|
||||||
|
t.Error("relationship struct slice not set to correct value")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func test{{$rel.LocalTable.NameGo}}ToManyRemoveOp{{$rel.Function.Name}}(t *testing.T) {
|
func test{{$rel.LocalTable.NameGo}}ToManyRemoveOp{{$rel.Function.Name}}(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue