Skip to content

Commit

Permalink
Merge pull request #1330 from atzedus/extract-relationship-test-tpl
Browse files Browse the repository at this point in the history
relationship test extracted to standalone file
  • Loading branch information
stephenafamo authored Jan 2, 2024
2 parents 1ed4b2b + b44002c commit 682d8c9
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 168 deletions.
2 changes: 1 addition & 1 deletion boilingcore/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 5 additions & 1 deletion importers/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func MapFromInterface(intf interface{}) (Map, error) {
m[name] = s
return nil
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -282,6 +281,11 @@ func NewDefaultImports() Collection {
`"testing"`,
},
},
"boil_relationship_test": {
Standard: List{
`"testing"`,
},
},
}

return col
Expand Down
165 changes: 165 additions & 0 deletions templates/test/singleton/boil_relationship_test.go.tpl
Original file line number Diff line number Diff line change
@@ -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 */ -}}
}
166 changes: 0 additions & 166 deletions templates/test/singleton/boil_suites_test.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down

0 comments on commit 682d8c9

Please sign in to comment.