2016-08-26 16:59:28 +02:00
|
|
|
{{- define "relationship_to_one_setops_test_helper" -}}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- $dot := .Dot -}}
|
|
|
|
{{- with .Rel -}}
|
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-08-26 16:59:28 +02:00
|
|
|
func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
var a {{.LocalTable.NameGo}}
|
2016-09-18 07:10:19 +02:00
|
|
|
var b {{.ForeignTable.NameGo}}
|
|
|
|
{{if not .Function.OneToOne -}}
|
|
|
|
var c {{.ForeignTable.NameGo}}
|
|
|
|
{{- end}}
|
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-18 07:10:19 +02:00
|
|
|
{{if not .Function.OneToOne -}}
|
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)
|
|
|
|
}
|
2016-09-18 07:10:19 +02:00
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
if err := a.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = b.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-18 07:10:19 +02:00
|
|
|
for i, x := range []*{{.ForeignTable.NameGo}}{&b{{if not .Function.OneToOne}}, &c{{end}}} {
|
2016-09-14 10:08:30 +02:00
|
|
|
err = a.Set{{.Function.Name}}(tx, i != 0, x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-17 09:02:03 +02:00
|
|
|
{{if .Function.UsesBytes -}}
|
2016-09-16 09:22:12 +02:00
|
|
|
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
|
|
|
|
{{else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}})
|
|
|
|
}
|
|
|
|
if a.R.{{.Function.Name}} != x {
|
|
|
|
t.Error("relationship struct not set to correct value")
|
|
|
|
}
|
|
|
|
|
2016-09-18 07:10:19 +02:00
|
|
|
{{if .Function.OneToOne -}}
|
|
|
|
zero := reflect.Zero(reflect.TypeOf(x.{{.Function.ForeignAssignment}}))
|
|
|
|
reflect.Indirect(reflect.ValueOf(&x.{{.Function.ForeignAssignment}})).Set(zero)
|
|
|
|
|
|
|
|
xrel := x.R
|
|
|
|
if err = x.Reload(tx); err != nil {
|
|
|
|
t.Fatal("failed to reload", err)
|
|
|
|
}
|
|
|
|
x.R = xrel
|
|
|
|
{{else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
zero := reflect.Zero(reflect.TypeOf(a.{{.Function.LocalAssignment}}))
|
|
|
|
reflect.Indirect(reflect.ValueOf(&a.{{.Function.LocalAssignment}})).Set(zero)
|
|
|
|
|
|
|
|
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-17 09:02:03 +02:00
|
|
|
{{if .Function.UsesBytes -}}
|
2016-09-16 09:22:12 +02:00
|
|
|
if 0 != bytes.Compare(a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}}) {
|
|
|
|
{{else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
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 -}}
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
2016-09-18 07:10:19 +02:00
|
|
|
{{- if or (.ForeignKey.Nullable) (and .Function.OneToOne .ForeignKey.ForeignColumnNullable)}}
|
2016-08-26 16:59:28 +02:00
|
|
|
|
|
|
|
func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
var a {{.LocalTable.NameGo}}
|
|
|
|
var b {{.ForeignTable.NameGo}}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.Set{{.Function.Name}}(tx, true, &b); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = a.Remove{{.Function.Name}}(tx, &b); err != nil {
|
|
|
|
t.Error("failed to remove relationship")
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := a.{{.Function.Name}}(tx).Count()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
t.Error("want no relationships remaining")
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.R.{{.Function.Name}} != nil {
|
|
|
|
t.Error("R struct entry should be nil")
|
|
|
|
}
|
|
|
|
|
2016-09-18 07:10:19 +02:00
|
|
|
{{if .Function.OneToOne -}}
|
|
|
|
if b.{{.ForeignTable.ColumnNameGo}}.Valid {
|
|
|
|
t.Error("R struct entry should be nil")
|
|
|
|
}
|
|
|
|
{{else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
if a.{{.LocalTable.ColumnNameGo}}.Valid {
|
|
|
|
t.Error("R struct entry should be nil")
|
|
|
|
}
|
2016-09-18 07:10:19 +02:00
|
|
|
{{- end}}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
{{if .ForeignKey.Unique -}}
|
|
|
|
if b.R.{{.Function.ForeignName}} != nil {
|
|
|
|
t.Error("failed to remove a from b's relationships")
|
|
|
|
}
|
|
|
|
{{else -}}
|
|
|
|
if len(b.R.{{.Function.ForeignName}}) != 0 {
|
|
|
|
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 */}}
|
|
|
|
{{- end -}}{{/* with rel */}}
|
|
|
|
{{- end -}}{{/* define */}}
|
2016-08-26 16:59:28 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
2016-09-14 10:08:30 +02:00
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
2016-09-16 09:22:12 +02:00
|
|
|
{{- $txt := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}}
|
|
|
|
{{template "relationship_to_one_setops_test_helper" (preserveDot $dot $txt) -}}
|
2016-08-26 16:59:28 +02:00
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|