sqlboiler/templates_test/relationship_to_one_setops.tpl

139 lines
4.4 KiB
Smarty
Raw Normal View History

2016-09-18 20:13:11 +02:00
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.FKeys -}}
2016-09-19 01:02:08 +02:00
{{- $txt := txtsFromFKey $dot.Tables $dot.Table .}}
{{- $varNameSingular := .Table | singular | camelCase -}}
{{- $foreignVarNameSingular := .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) {
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}}
seed := randomize.NewSeed()
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
t.Fatal(err)
}
if err = randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
t.Fatal(err)
}
if err = randomize.Struct(seed, &c, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); 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)
}
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)
if err != nil {
t.Fatal(err)
}
if a.R.{{$txt.Function.Name}} != x {
t.Error("relationship struct not set to correct value")
}
{{if .Unique -}}
if x.R.{{$txt.Function.ForeignName}} != &a {
t.Error("failed to append to foreign relationship struct")
}
{{else -}}
if x.R.{{$txt.Function.ForeignName}}[0] != &a {
t.Error("failed to append to foreign relationship struct")
}
{{end -}}
2016-09-18 20:13:11 +02:00
{{if $txt.Function.UsesBytes -}}
if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) {
{{else -}}
2016-09-18 20:13:11 +02:00
if a.{{$txt.Function.LocalAssignment}} != x.{{$txt.Function.ForeignAssignment}} {
{{end -}}
2016-09-18 20:13:11 +02:00
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}})
}
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)
if err = a.Reload(tx); err != nil {
t.Fatal("failed to reload", 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}}) {
{{else -}}
2016-09-18 20:13:11 +02:00
if a.{{$txt.Function.LocalAssignment}} != x.{{$txt.Function.ForeignAssignment}} {
{{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-19 01:02:08 +02:00
{{- if .Nullable}}
2016-09-18 20:13:11 +02:00
func test{{$txt.LocalTable.NameGo}}ToOneRemoveOp{{$txt.ForeignTable.NameGo}}_{{$txt.Function.Name}}(t *testing.T) {
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}}
seed := randomize.NewSeed()
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, strmangle.SetComplement({{$varNameSingular}}PrimaryKeyColumns, {{$varNameSingular}}ColumnsWithoutDefault)...); err != nil {
t.Fatal(err)
}
if err = randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, strmangle.SetComplement({{$foreignVarNameSingular}}PrimaryKeyColumns, {{$foreignVarNameSingular}}ColumnsWithoutDefault)...); err != nil {
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 {
t.Fatal(err)
}
2016-09-18 20:13:11 +02:00
if err = a.Remove{{$txt.Function.Name}}(tx, &b); err != nil {
t.Error("failed to remove relationship")
}
2016-09-18 20:13:11 +02:00
count, err := a.{{$txt.Function.Name}}(tx).Count()
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 {
t.Error("R struct entry should be nil")
}
2016-09-18 20:13:11 +02:00
if a.{{$txt.LocalTable.ColumnNameGo}}.Valid {
t.Error("foreign key value should be nil")
}
2016-09-19 01:02:08 +02:00
{{if .Unique -}}
2016-09-18 20:13:11 +02:00
if b.R.{{$txt.Function.ForeignName}} != nil {
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 {
t.Error("failed to remove a from b's relationships")
}
2016-09-18 07:10:19 +02:00
{{- end}}
}
{{end -}}{{/* end if foreign key nullable */}}
2016-09-18 20:13:11 +02:00
{{- end -}}{{/* range */}}
{{- end -}}{{/* join table */}}