diff --git a/boilingcore/output.go b/boilingcore/output.go index c7ce10943..78ba978e0 100644 --- a/boilingcore/output.go +++ b/boilingcore/output.go @@ -246,7 +246,7 @@ func writeFile(outFolder string, fileName string, input *bytes.Buffer, format bo } path := filepath.Join(outFolder, fileName) - if err := testHarnessWriteFile(path, byt, 0664); err != nil { + if err := testHarnessWriteFile(path, byt, 0o664); err != nil { return errors.Wrapf(err, "failed to write output file %s", path) } diff --git a/go.mod b/go.mod index 7b0b76439..48c5d6e46 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/huandu/xstrings v1.3.2 // indirect github.com/imdario/mergo v0.3.13 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12 github.com/lib/pq v1.10.6 github.com/microsoft/go-mssqldb v0.17.0 github.com/mitchellh/copystructure v1.2.0 // indirect diff --git a/go.sum b/go.sum index 282d58112..e227f681f 100644 --- a/go.sum +++ b/go.sum @@ -298,6 +298,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= +github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12 h1:DQVOxR9qdYEybJUr/c7ku34r3PfajaMYXZwgDM7KuSk= +github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12/go.mod h1:u9MdXq/QageOOSGp7qG4XAQsYUMP+V5zEel/Vrl6OOc= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= diff --git a/importers/imports.go b/importers/imports.go index adbfa74ed..671fae816 100644 --- a/importers/imports.go +++ b/importers/imports.go @@ -156,7 +156,6 @@ func MapFromInterface(intf interface{}) (Map, error) { m[name] = s return nil }) - if err != nil { return nil, err } @@ -282,6 +281,11 @@ func NewDefaultImports() Collection { `"testing"`, }, }, + "boil_relationship_test": { + Standard: List{ + `"testing"`, + }, + }, } return col diff --git a/templates/test/singleton/boil_relationship_test.go.tpl b/templates/test/singleton/boil_relationship_test.go.tpl new file mode 100644 index 000000000..9d335e0cb --- /dev/null +++ b/templates/test/singleton/boil_relationship_test.go.tpl @@ -0,0 +1,165 @@ +// TestToOne tests cannot be run in parallel +// or deadlocks can occur. +func TestToOne(t *testing.T) { +{{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $fkey := .FKeys -}} + {{- $ltable := $.Aliases.Table $fkey.Table -}} + {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} + {{- $relAlias := $ltable.Relationship $fkey.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}", test{{$ltable.UpSingular}}ToOne{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) + {{end -}}{{- /* fkey range */ -}} + {{- end -}}{{- /* if join table */ -}} +{{- end -}}{{- /* tables range */ -}} +} + +// TestOneToOne tests cannot be run in parallel +// or deadlocks can occur. +func TestOneToOne(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToOneRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} + {{- $relAlias := $ftable.Relationship $rel.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOne{{$ftable.UpSingular}}Using{{$relAlias.Local}}) + {{end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToMany tests cannot be run in parallel +// or deadlocks can occur. +func TestToMany(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToMany{{$relAlias.Local}}) + {{end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToOneSet tests cannot be run in parallel +// or deadlocks can occur. +func TestToOneSet(t *testing.T) { +{{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $fkey := .FKeys -}} + {{- $ltable := $.Aliases.Table $fkey.Table -}} + {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} + {{- $relAlias := $ltable.Relationship $fkey.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) + {{end -}}{{- /* fkey range */ -}} + {{- end -}}{{- /* if join table */ -}} +{{- end -}}{{- /* tables range */ -}} +} + +// TestToOneRemove tests cannot be run in parallel +// or deadlocks can occur. +func TestToOneRemove(t *testing.T) { +{{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $fkey := .FKeys -}} + {{- if $fkey.Nullable -}} + {{- $ltable := $.Aliases.Table $fkey.Table -}} + {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} + {{- $relAlias := $ltable.Relationship $fkey.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) + {{end -}}{{- /* if foreign key nullable */ -}} + {{- end -}}{{- /* fkey range */ -}} + {{- end -}}{{- /* if join table */ -}} +{{- end -}}{{- /* tables range */ -}} +} + +// TestOneToOneSet tests cannot be run in parallel +// or deadlocks can occur. +func TestOneToOneSet(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToOneRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} + {{- $relAlias := $ftable.Relationship $rel.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) + {{end -}}{{- /* range to one relationships */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestOneToOneRemove tests cannot be run in parallel +// or deadlocks can occur. +func TestOneToOneRemove(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToOneRelationships -}} + {{- if $rel.ForeignColumnNullable -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} + {{- $relAlias := $ftable.Relationship $rel.Name -}} + t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) + {{end -}}{{- /* if foreign column nullable */ -}} + {{- end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToManyAdd tests cannot be run in parallel +// or deadlocks can occur. +func TestToManyAdd(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyAddOp{{$relAlias.Local}}) + {{end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToManySet tests cannot be run in parallel +// or deadlocks can occur. +func TestToManySet(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} + {{- else -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManySetOp{{$relAlias.Local}}) + {{end -}}{{- /* if foreign column nullable */ -}} + {{- end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} + +// TestToManyRemove tests cannot be run in parallel +// or deadlocks can occur. +func TestToManyRemove(t *testing.T) { + {{- range .Tables}} + {{- if or .IsJoinTable .IsView -}} + {{- else -}} + {{- range $rel := .ToManyRelationships -}} + {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} + {{- else -}} + {{- $ltable := $.Aliases.Table $rel.Table -}} + {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} + t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyRemoveOp{{$relAlias.Local}}) + {{end -}}{{- /* if foreign column nullable */ -}} + {{- end -}}{{- /* range */ -}} + {{- end -}}{{- /* outer if join table */ -}} + {{- end -}}{{- /* outer tables range */ -}} +} diff --git a/templates/test/singleton/boil_suites_test.go.tpl b/templates/test/singleton/boil_suites_test.go.tpl index cdaea8508..f60abe76c 100644 --- a/templates/test/singleton/boil_suites_test.go.tpl +++ b/templates/test/singleton/boil_suites_test.go.tpl @@ -165,172 +165,6 @@ func TestInsert(t *testing.T) { {{- end -}} } -// TestToOne tests cannot be run in parallel -// or deadlocks can occur. -func TestToOne(t *testing.T) { -{{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $fkey := .FKeys -}} - {{- $ltable := $.Aliases.Table $fkey.Table -}} - {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} - {{- $relAlias := $ltable.Relationship $fkey.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}", test{{$ltable.UpSingular}}ToOne{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) - {{end -}}{{- /* fkey range */ -}} - {{- end -}}{{- /* if join table */ -}} -{{- end -}}{{- /* tables range */ -}} -} - -// TestOneToOne tests cannot be run in parallel -// or deadlocks can occur. -func TestOneToOne(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToOneRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} - {{- $relAlias := $ftable.Relationship $rel.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOne{{$ftable.UpSingular}}Using{{$relAlias.Local}}) - {{end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToMany tests cannot be run in parallel -// or deadlocks can occur. -func TestToMany(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToMany{{$relAlias.Local}}) - {{end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToOneSet tests cannot be run in parallel -// or deadlocks can occur. -func TestToOneSet(t *testing.T) { -{{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $fkey := .FKeys -}} - {{- $ltable := $.Aliases.Table $fkey.Table -}} - {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} - {{- $relAlias := $ltable.Relationship $fkey.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) - {{end -}}{{- /* fkey range */ -}} - {{- end -}}{{- /* if join table */ -}} -{{- end -}}{{- /* tables range */ -}} -} - -// TestToOneRemove tests cannot be run in parallel -// or deadlocks can occur. -func TestToOneRemove(t *testing.T) { -{{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $fkey := .FKeys -}} - {{- if $fkey.Nullable -}} - {{- $ltable := $.Aliases.Table $fkey.Table -}} - {{- $ftable := $.Aliases.Table $fkey.ForeignTable -}} - {{- $relAlias := $ltable.Relationship $fkey.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Foreign}}) - {{end -}}{{- /* if foreign key nullable */ -}} - {{- end -}}{{- /* fkey range */ -}} - {{- end -}}{{- /* if join table */ -}} -{{- end -}}{{- /* tables range */ -}} -} - -// TestOneToOneSet tests cannot be run in parallel -// or deadlocks can occur. -func TestOneToOneSet(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToOneRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} - {{- $relAlias := $ftable.Relationship $rel.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneSetOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) - {{end -}}{{- /* range to one relationships */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestOneToOneRemove tests cannot be run in parallel -// or deadlocks can occur. -func TestOneToOneRemove(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToOneRelationships -}} - {{- if $rel.ForeignColumnNullable -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $ftable := $.Aliases.Table $rel.ForeignTable -}} - {{- $relAlias := $ftable.Relationship $rel.Name -}} - t.Run("{{$ltable.UpSingular}}To{{$ftable.UpSingular}}Using{{$relAlias.Local}}", test{{$ltable.UpSingular}}OneToOneRemoveOp{{$ftable.UpSingular}}Using{{$relAlias.Local}}) - {{end -}}{{- /* if foreign column nullable */ -}} - {{- end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToManyAdd tests cannot be run in parallel -// or deadlocks can occur. -func TestToManyAdd(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyAddOp{{$relAlias.Local}}) - {{end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToManySet tests cannot be run in parallel -// or deadlocks can occur. -func TestToManySet(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} - {{- else -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManySetOp{{$relAlias.Local}}) - {{end -}}{{- /* if foreign column nullable */ -}} - {{- end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - -// TestToManyRemove tests cannot be run in parallel -// or deadlocks can occur. -func TestToManyRemove(t *testing.T) { - {{- range .Tables}} - {{- if or .IsJoinTable .IsView -}} - {{- else -}} - {{- range $rel := .ToManyRelationships -}} - {{- if not (or $rel.ForeignColumnNullable $rel.ToJoinTable)}} - {{- else -}} - {{- $ltable := $.Aliases.Table $rel.Table -}} - {{- $relAlias := $.Aliases.ManyRelationship $rel.ForeignTable $rel.Name $rel.JoinTable $rel.JoinLocalFKeyName -}} - t.Run("{{$ltable.UpSingular}}To{{$relAlias.Local}}", test{{$ltable.UpSingular}}ToManyRemoveOp{{$relAlias.Local}}) - {{end -}}{{- /* if foreign column nullable */ -}} - {{- end -}}{{- /* range */ -}} - {{- end -}}{{- /* outer if join table */ -}} - {{- end -}}{{- /* outer tables range */ -}} -} - func TestReload(t *testing.T) { {{- range .Tables}} {{- if or .IsJoinTable .IsView -}}