2016-09-18 20:13:11 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
2016-09-18 20:18:43 +02:00
|
|
|
{{- $txt := txtsFromFKey $dot.PkgName $dot.Tables $dot.Table .}}
|
2016-08-27 07:53:36 +02:00
|
|
|
{{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}}
|
2016-09-17 09:02:03 +02:00
|
|
|
{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
|
2016-09-18 20:13:11 +02:00
|
|
|
func test{{$txt.LocalTable.NameGo}}ToOneSetOp{{$txt.ForeignTable.NameGo}}_{{$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-18 20:13:11 +02:00
|
|
|
var a {{$txt.LocalTable.NameGo}}
|
|
|
|
var b, c {{$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-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, &c, {{$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)
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
for i, x := range []*{{$txt.ForeignTable.NameGo}}{&b, &c} {
|
|
|
|
err = a.Set{{$txt.Function.Name}}(tx, i != 0, x)
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
{{if $txt.Function.UsesBytes -}}
|
|
|
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if a.{{$txt.Function.LocalAssignment}} != x.{{$txt.Function.ForeignAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-09-18 20:13:11 +02:00
|
|
|
if a.R.{{$txt.Function.Name}} != x {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("relationship struct not set to correct value")
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
{{if $txt.Function.OneToOne -}}
|
|
|
|
zero := reflect.Zero(reflect.TypeOf(x.{{$txt.Function.ForeignAssignment}}))
|
|
|
|
reflect.Indirect(reflect.ValueOf(&x.{{$txt.Function.ForeignAssignment}})).Set(zero)
|
2016-09-18 07:10:19 +02:00
|
|
|
|
|
|
|
xrel := x.R
|
|
|
|
if err = x.Reload(tx); err != nil {
|
|
|
|
t.Fatal("failed to reload", err)
|
|
|
|
}
|
|
|
|
x.R = xrel
|
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
zero := reflect.Zero(reflect.TypeOf(a.{{$txt.Function.LocalAssignment}}))
|
|
|
|
reflect.Indirect(reflect.ValueOf(&a.{{$txt.Function.LocalAssignment}})).Set(zero)
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
if err = a.Reload(tx); err != nil {
|
|
|
|
t.Fatal("failed to reload", err)
|
|
|
|
}
|
2016-09-18 07:10:19 +02:00
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
{{if $txt.Function.UsesBytes -}}
|
|
|
|
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if a.{{$txt.Function.LocalAssignment}} != x.{{$txt.Function.ForeignAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}})
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{{if .ForeignKey.Unique -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if x.R.{{$txt.Function.ForeignName}} != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("failed to append to foreign relationship struct")
|
|
|
|
}
|
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if x.R.{{$txt.Function.ForeignName}}[0] != &a {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("failed to append to foreign relationship struct")
|
|
|
|
}
|
|
|
|
{{end -}}
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
2016-09-18 20:13:11 +02:00
|
|
|
{{- if or (.ForeignKey.Nullable) (and $txt.Function.OneToOne .ForeignKey.ForeignColumnNullable)}}
|
2016-08-26 16:59:28 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
func test{{$txt.LocalTable.NameGo}}ToOneRemoveOp{{$txt.ForeignTable.NameGo}}_{{$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-18 20:13:11 +02:00
|
|
|
var a {{$txt.LocalTable.NameGo}}
|
|
|
|
var b {{$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-16 17:22:09 +02:00
|
|
|
if err = randomize.Struct(seed, &b, {{$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-18 20:13:11 +02:00
|
|
|
if err = a.Set{{$txt.Function.Name}}(tx, true, &b); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
if err = a.Remove{{$txt.Function.Name}}(tx, &b); err != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("failed to remove relationship")
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
count, err := a.{{$txt.Function.Name}}(tx).Count()
|
2016-09-14 10:08:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
t.Error("want no relationships remaining")
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
if a.R.{{$txt.Function.Name}} != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("R struct entry should be nil")
|
|
|
|
}
|
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
if a.{{$txt.LocalTable.ColumnNameGo}}.Valid {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("R struct entry should be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
{{if .ForeignKey.Unique -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if b.R.{{$txt.Function.ForeignName}} != nil {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("failed to remove a from b's relationships")
|
|
|
|
}
|
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if len(b.R.{{$txt.Function.ForeignName}}) != 0 {
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("failed to remove a from b's relationships")
|
|
|
|
}
|
2016-09-18 07:10:19 +02:00
|
|
|
{{- end}}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}{{/* end if foreign key nullable */}}
|
2016-09-18 20:13:11 +02:00
|
|
|
{{- end -}}{{/* range */}}
|
|
|
|
{{- end -}}{{/* join table */}}
|