2016-08-26 16:59:28 +02:00
|
|
|
{{- define "relationship_to_one_setops_test_helper" -}}
|
2016-08-27 07:53:36 +02:00
|
|
|
{{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}}
|
|
|
|
{{- $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-08-27 07:04:49 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
tx := MustTx(boil.Begin())
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
var a {{.LocalTable.NameGo}}
|
|
|
|
var b, c {{.ForeignTable.NameGo}}
|
|
|
|
|
2016-08-27 07:43:50 +02:00
|
|
|
seed := randomize.NewSeed()
|
2016-08-27 07:53:36 +02:00
|
|
|
if err = randomize.Struct(seed, &a, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
2016-08-27 07:04:49 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-27 07:53:36 +02:00
|
|
|
if err = randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false, {{$foreignVarNameSingular}}PrimaryKeyColumns...); err != nil {
|
2016-08-27 07:04:49 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-27 07:53:36 +02:00
|
|
|
if err = randomize.Struct(seed, &c, {{$foreignVarNameSingular}}DBTypes, false, {{$foreignVarNameSingular}}PrimaryKeyColumns...); err != nil {
|
2016-08-27 07:04:49 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-27 07:43:50 +02:00
|
|
|
if err := a.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-27 07:04:49 +02:00
|
|
|
if err = b.Insert(tx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, x := range []*{{.ForeignTable.NameGo}}{&b, &c} {
|
|
|
|
err = a.Set{{.Function.Name}}(tx, i != 0, x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
zero := reflect.Zero(reflect.TypeOf(a.{{.Function.LocalAssignment}}))
|
2016-08-27 07:43:50 +02:00
|
|
|
reflect.Indirect(reflect.ValueOf(&a.{{.Function.LocalAssignment}})).Set(zero)
|
2016-08-27 07:04:49 +02:00
|
|
|
|
|
|
|
if err = a.Reload(tx); err != nil {
|
|
|
|
t.Fatal("failed to reload", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
2016-08-27 07:43:50 +02:00
|
|
|
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}})
|
2016-08-27 07:04:49 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
}
|
|
|
|
{{- if .ForeignKey.Nullable}}
|
|
|
|
|
|
|
|
func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
|
|
|
|
}
|
|
|
|
{{end -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
|
|
|
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}}
|
|
|
|
|
|
|
|
{{template "relationship_to_one_setops_test_helper" $rel -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|