Change entire project tests.

This commit is contained in:
Aaron L 2016-08-14 01:58:36 -07:00
parent c278bb6667
commit d25712f929
16 changed files with 232 additions and 453 deletions

View file

@ -178,15 +178,9 @@ var defaultTestTemplateImports = imports{
standard: importList{
`"testing"`,
`"reflect"`,
`"time"`,
`"fmt"`,
`"bytes"`,
},
thirdParty: importList{
`"github.com/pkg/errors"`,
`"gopkg.in/nullbio/null.v4"`,
`"github.com/vattle/sqlboiler/boil"`,
`"github.com/vattle/sqlboiler/boil/qm"`,
`"github.com/vattle/sqlboiler/strmangle"`,
},
}

View file

@ -59,11 +59,7 @@ func (o *{{$tableNameSingular}}Slice) ReloadAllG() error {
// ReloadAll refetches every row with matching primary key column values
// and overwrites the original object slice with the newly updated slice.
func (o *{{$tableNameSingular}}Slice) ReloadAll(exec boil.Executor) error {
if o == nil {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for reload all")
}
if len(*o) == 0 {
if o == nil || len(*o) == 0 {
return nil
}

View file

@ -3,62 +3,11 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}(t *testing.T) {
var err error
t.Parallel()
o := make({{$tableNameSingular}}Slice, 2)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
query := {{$tableNamePlural}}(nil)
if query.Query == nil {
t.Error("expected a query, got nothing")
}
// insert two random objects to test DeleteAll
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
// Delete all rows to give a clean slate
err = {{$tableNamePlural}}G().DeleteAll()
if err != nil {
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
}
// Check number of rows in table to ensure DeleteAll was successful
var c int64
c, err = {{$tableNamePlural}}G().Count()
if c != 0 {
t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c)
}
o = make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
// Ensure Count is valid
c, err = {{$tableNamePlural}}G().Count()
if c != 3 {
t.Errorf("Expected {{.Table.Name}} table to have 3 rows, but got %d", c)
}
// Attempt to retrieve all objects
res, err := {{$tableNamePlural}}G().All()
if err != nil {
t.Errorf("Unable to retrieve all {{$tableNamePlural}}, err: %s", err)
}
if len(res) != 3 {
t.Errorf("Expected 3 {{$tableNameSingular}} rows, got %d", len(res))
}
{{$varNamePlural}}DeleteAllRows(t)
}

View file

@ -2,118 +2,88 @@
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func {{$varNamePlural}}DeleteAllRows(t *testing.T) {
// Delete all rows to give a clean slate
err := {{$tableNamePlural}}G().DeleteAll()
func Test{{$tableNamePlural}}Delete(t *testing.T) {
t.Parallel()
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
t.Fatal(err)
}
defer tx.Rollback()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
if err = {{$varNameSingular}}.Delete(tx); err != nil {
t.Error(err)
}
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
if count != 0 {
t.Error("want zero records, got:", count)
}
}
func Test{{$tableNamePlural}}QueryDeleteAll(t *testing.T) {
var err error
var c int64
t.Parallel()
// Start from a clean slate
{{$varNamePlural}}DeleteAllRows(t)
// Check number of rows in table to ensure DeleteAll was successful
c, err = {{$tableNamePlural}}G().Count()
if c != 0 {
t.Errorf("Expected 0 rows after ObjDeleteAllRows() call, but got %d rows", c)
}
o := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
// Test DeleteAll() query function
err = {{$tableNamePlural}}G().DeleteAll()
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
t.Fatal(err)
}
defer tx.Rollback()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
// Check number of rows in table to ensure DeleteAll was successful
c, err = {{$tableNamePlural}}G().Count()
if err = {{$tableNamePlural}}(tx).DeleteAll(); err != nil {
t.Error(err)
}
if c != 0 {
t.Errorf("Expected 0 rows after Obj().DeleteAll() call, but got %d rows", c)
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
if count != 0 {
t.Error("want zero records, got:", count)
}
}
func Test{{$tableNamePlural}}SliceDeleteAll(t *testing.T) {
var err error
var c int64
t.Parallel()
// insert random columns to test DeleteAll
o := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
tx, err := boil.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
slice := {{$tableNameSingular}}Slice{{"{"}}{{$varNameSingular}}{{"}"}}
if err = slice.DeleteAll(tx); err != nil {
t.Error(err)
}
// test DeleteAll slice function
if err = o.DeleteAllG(); err != nil {
t.Errorf("Unable to objSlice.DeleteAll(): %s", err)
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
// Check number of rows in table to ensure DeleteAll was successful
c, err = {{$tableNamePlural}}G().Count()
if c != 0 {
t.Errorf("Expected 0 rows after objSlice.DeleteAll() call, but got %d rows", c)
}
}
func Test{{$tableNamePlural}}Delete(t *testing.T) {
var err error
var c int64
// insert random columns to test Delete
o := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
o[0].DeleteG()
// Check number of rows in table to ensure DeleteAll was successful
c, err = {{$tableNamePlural}}G().Count()
if c != 2 {
t.Errorf("Expected 2 rows after obj.Delete() call, but got %d rows", c)
}
o[1].DeleteG()
o[2].DeleteG()
// Check number of rows in table to ensure Delete worked for all rows
c, err = {{$tableNamePlural}}G().Count()
if c != 0 {
t.Errorf("Expected 0 rows after all obj.Delete() calls, but got %d rows", c)
if count != 0 {
t.Error("want zero records, got:", count)
}
}

View file

@ -2,49 +2,30 @@
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $pkeyArgs := .Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", " -}}
func Test{{$tableNamePlural}}Exists(t *testing.T) {
var err error
t.Parallel()
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
tx, err := boil.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
// Check Exists finds existing rows
e, err := {{$tableNameSingular}}ExistsG({{$pkeyArgs}})
{{$pkeyArgs := .Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice (printf "%s." $varNameSingular) | join ", " -}}
e, err := {{$tableNameSingular}}Exists(tx, {{$pkeyArgs}})
if err != nil {
t.Errorf("Unable to check if {{$tableNameSingular}} exists: %s", err)
}
if e != true {
t.Errorf("Expected {{$tableNameSingular}}ExistsG to return true, but got false.")
}
whereClause := strmangle.WhereClause(1, {{$varNameSingular}}PrimaryKeyColumns)
e, err = {{$tableNamePlural}}G(qm.Where(whereClause, boil.GetStructValues(o, {{$varNameSingular}}PrimaryKeyColumns...)...)).Exists()
if err != nil {
t.Errorf("Unable to check if {{$tableNameSingular}} exists: %s", err)
}
if e != true {
t.Errorf("Expected ExistsG to return true, but got false.")
}
o = {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
e, err = {{$tableNamePlural}}G(qm.Where(whereClause, boil.GetStructValues(o, {{$varNameSingular}}PrimaryKeyColumns...)...)).Exists()
if err != nil {
t.Errorf("Unable to check if {{$tableNameSingular}} exists: %s", err)
}
if e != false {
t.Errorf("Expected ExistsG to return false, but got true.")
}
{{$varNamePlural}}DeleteAllRows(t)
}

View file

@ -3,43 +3,27 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Find(t *testing.T) {
var err error
o := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
for i := 0; i < len(o); i++ {
if err = o[i].InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
tx, err := boil.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
j := make({{$tableNameSingular}}Slice, 3)
// Perform all Find queries and assign result objects to slice for comparison
for i := 0; i < len(j); i++ {
j[i], err = {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o[i]." | join ", "}})
err = {{$varNameSingular}}CompareVals(o[i], j[i], true); if err != nil {
t.Error(err)
}
{{$varNameSingular}}Found, err := {{$tableNameSingular}}Find(tx, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice (printf "%s." $varNameSingular) | join ", "}})
if err != nil {
t.Error(err)
}
f, err := {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o[0]." | join ", "}}, {{$varNameSingular}}PrimaryKeyColumns...)
{{range $key, $value := .Table.PKey.Columns}}
if o[0].{{titleCase $value}} != f.{{titleCase $value}} {
t.Errorf("Expected primary key values to match, {{titleCase $value}} did not match")
if {{$varNameSingular}}Found == nil {
t.Error("want a record, got nil")
}
{{end}}
colsWithoutPrimKeys := strmangle.SetComplement({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns)
fRef := reflect.ValueOf(f).Elem()
for _, v := range colsWithoutPrimKeys {
val := fRef.FieldByName(v)
if val.IsValid() {
t.Errorf("Expected all other columns to be zero value, but column %s was %#v", v, val.Interface())
}
}
{{$varNamePlural}}DeleteAllRows(t)
}

View file

@ -3,145 +3,119 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Bind(t *testing.T) {
var err error
t.Parallel()
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
j := {{$tableNameSingular}}{}
err = {{$tableNamePlural}}G(qm.Where(`{{whereClause 1 .Table.PKey.Columns}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
if err != nil {
t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err)
}
err = {{$varNameSingular}}CompareVals(&o, &j, true); if err != nil {
t.Error(err)
}
// insert 3 rows, attempt to bind into slice
{{$varNamePlural}}DeleteAllRows(t)
y := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&y, {{$varNameSingular}}DBTypes, true); err != nil {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(y); i++ {
err = y[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", y[i], err)
}
}
k := {{$tableNameSingular}}Slice{}
err = {{$tableNamePlural}}G().Bind(&k)
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to call Bind on {{$tableNameSingular}} slice of objects: %s", err)
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
if len(k) != 3 {
t.Errorf("Expected 3 results, got %d", len(k))
if err = {{$tableNamePlural}}(tx).Bind({{$varNameSingular}}); err != nil {
t.Error(err)
}
for i := 0; i < len(y); i++ {
err = {{$varNameSingular}}CompareVals(y[i], k[i], true); if err != nil {
t.Error(err)
}
}
{{$varNamePlural}}DeleteAllRows(t)
}
func Test{{$tableNamePlural}}One(t *testing.T) {
var err error
t.Parallel()
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
j, err := {{$tableNamePlural}}G().One()
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to fetch One {{$tableNameSingular}} result:\n#%v\nErr: %s", j, err)
t.Fatal(err)
}
defer tx.Rollback()
err = {{$varNameSingular}}CompareVals(&o, j, true); if err != nil {
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
{{$varNamePlural}}DeleteAllRows(t)
if x, err := {{$tableNamePlural}}(tx).One(); err != nil {
t.Error(err)
} else if x == nil {
t.Error("expected to get a non nil record")
}
}
func Test{{$tableNamePlural}}All(t *testing.T) {
var err error
t.Parallel()
o := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
{{$varNameSingular}}One := &{{$tableNameSingular}}{}
{{$varNameSingular}}Two := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}One, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
if err := boil.RandomizeStruct({{$varNameSingular}}Two, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
j, err := {{$tableNamePlural}}G().All()
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to fetch All {{$tableNameSingular}} results: %s", err)
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}One.Insert(tx); err != nil {
t.Error(err)
}
if err = {{$varNameSingular}}Two.Insert(tx); err != nil {
t.Error(err)
}
if len(j) != 3 {
t.Errorf("Expected 3 results, got %d", len(j))
slice, err := {{$tableNamePlural}}(tx).All()
if err != nil {
t.Error(err)
}
for i := 0; i < len(o); i++ {
err = {{$varNameSingular}}CompareVals(o[i], j[i], true); if err != nil {
t.Error(err)
}
if len(slice) != 2 {
t.Error("want 2 records, got:", len(slice))
}
{{$varNamePlural}}DeleteAllRows(t)
}
func Test{{$tableNamePlural}}Count(t *testing.T) {
var err error
t.Parallel()
o := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
{{$varNameSingular}}One := &{{$tableNameSingular}}{}
{{$varNameSingular}}Two := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}One, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
if err := boil.RandomizeStruct({{$varNameSingular}}Two, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test Count
for i := 0; i < len(o); i++ {
err = o[i].InsertG()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
c, err := {{$tableNamePlural}}G().Count()
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to count query {{$tableNameSingular}}: %s", err)
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}One.Insert(tx); err != nil {
t.Error(err)
}
if err = {{$varNameSingular}}Two.Insert(tx); err != nil {
t.Error(err)
}
if c != 3 {
t.Errorf("Expected 3 results from count {{$tableNameSingular}}, got %d", c)
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Error(err)
}
{{$varNamePlural}}DeleteAllRows(t)
if count != 2 {
t.Error("want 2 records, got:", count)
}
}

View file

@ -4,44 +4,6 @@
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
var {{$varNameSingular}}DBTypes = map[string]string{{"{"}}{{.Table.Columns | columnDBTypes | makeStringMap}}{{"}"}}
var (
_ = bytes.Equal
_ = time.Second
_ = null.Bool.IsZero
)
func {{$varNameSingular}}CompareVals(o *{{$tableNameSingular}}, j *{{$tableNameSingular}}, equal bool, blacklist ...string) error {
{{- range $key, $value := .Table.Columns -}}
{{if eq $value.Type "null.Time"}}
if ((equal && o.{{titleCase $value.Name}}.Time.UTC().Format("02/01/2006") != j.{{titleCase $value.Name}}.Time.UTC().Format("02/01/2006")) ||
(!equal && o.{{titleCase $value.Name}}.Time.UTC().Format("02/01/2006") == j.{{titleCase $value.Name}}.Time.UTC().Format("02/01/2006"))) &&
!strmangle.SetInclude("{{$value.Name}}", blacklist) {
return errors.New(fmt.Sprintf("NullTime {{$value.Name}} unexpected value, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Time.Format("02/01/2006"), j.{{titleCase $value.Name}}.Time.Format("02/01/2006")))
}
{{else if eq $value.Type "time.Time"}}
if ((equal && o.{{titleCase $value.Name}}.UTC().Format("02/01/2006") != j.{{titleCase $value.Name}}.UTC().Format("02/01/2006")) ||
(!equal && o.{{titleCase $value.Name}}.UTC().Format("02/01/2006") == j.{{titleCase $value.Name}}.UTC().Format("02/01/2006"))) &&
!strmangle.SetInclude("{{$value.Name}}", blacklist) {
return errors.New(fmt.Sprintf("Time {{$value.Name}} unexpected value, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Format("02/01/2006"), j.{{titleCase $value.Name}}.Format("02/01/2006")))
}
{{else if eq $value.Type "[]byte"}}
if ((equal && !bytes.Equal(o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}})) ||
(!equal && bytes.Equal(o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}))) &&
!strmangle.SetInclude("{{$value.Name}}", blacklist) {
return errors.New(fmt.Sprintf("Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}))
}
{{else}}
if ((equal && j.{{titleCase $value.Name}} != o.{{titleCase $value.Name}}) ||
(!equal && j.{{titleCase $value.Name}} == o.{{titleCase $value.Name}})) &&
!strmangle.SetInclude("{{$value.Name}}", blacklist) {
return errors.New(fmt.Sprintf("Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}))
}
{{end}}
{{- end -}}
return nil
}
func Test{{$tableNamePlural}}InPrimaryKeyArgs(t *testing.T) {
var err error
var o {{$tableNameSingular}}

View file

@ -23,6 +23,7 @@ func {{$varNameSingular}}AfterUpdateHook(o *{{$tableNameSingular}}) error {
}
func Test{{$tableNamePlural}}Hooks(t *testing.T) {
t.Skip("skipping for transactions")
var err error
empty := &{{$tableNameSingular}}{}
@ -41,5 +42,4 @@ func Test{{$tableNamePlural}}Hooks(t *testing.T) {
}
{{$varNameSingular}}BeforeCreateHooks = []{{$tableNameSingular}}Hook{}
{{$varNamePlural}}DeleteAllRows(t)
}

View file

@ -4,6 +4,8 @@
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $parent := . -}}
func Test{{$tableNamePlural}}Insert(t *testing.T) {
t.Parallel()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -30,6 +32,8 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
}
func Test{{$tableNamePlural}}InsertWhitelist(t *testing.T) {
t.Parallel()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)

View file

@ -8,6 +8,8 @@
{{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
t.Parallel()
var err error
tx := MustTx(boil.Begin())
defer tx.Rollback()

View file

@ -1,5 +1,7 @@
{{- define "relationship_to_one_test_helper"}}
func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) {
t.Parallel()
tx := MustTx(boil.Begin())
defer tx.Rollback()
@ -12,7 +14,6 @@ func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true
{{end}}
{{if not .Function.ReverseInserts -}}
if err := foreign.Insert(tx); err != nil {
t.Fatal(err)

View file

@ -3,93 +3,49 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Reload(t *testing.T) {
var err error
t.Parallel()
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = o.InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
// Create another copy of the object
o1, err := {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
if err != nil {
t.Errorf("Unable to find {{$tableNameSingular}} row.")
}
// Randomize the struct values again, except for the primary key values, so we can call update.
err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...)
if err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct members excluding primary keys: %s", err)
}
colsWithoutPrimKeys := strmangle.SetComplement({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns)
if err = o.UpdateG(colsWithoutPrimKeys...); err != nil {
t.Errorf("Unable to update the {{$tableNameSingular}} row: %s", err)
}
if err = o1.ReloadG(); err != nil {
t.Errorf("Unable to reload {{$tableNameSingular}} object: %s", err)
}
err = {{$varNameSingular}}CompareVals(&o, o1, true); if err != nil {
t.Error(err)
}
{{$varNamePlural}}DeleteAllRows(t)
}
func Test{{$tableNamePlural}}ReloadAll(t *testing.T) {
var err error
o1 := make({{$tableNameSingular}}Slice, 3)
o2 := make({{$tableNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o1, {{$varNameSingular}}DBTypes, false); err != nil {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
for i := 0; i < len(o1); i++ {
if err = o1[i].InsertG(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o1[i], err)
}
}
for i := 0; i < len(o1); i++ {
o2[i], err = {{$tableNameSingular}}FindG({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o1[i]." | join ", "}})
if err != nil {
t.Errorf("Unable to find {{$tableNameSingular}} row.")
}
err = {{$varNameSingular}}CompareVals(o1[i], o2[i], true); if err != nil {
t.Error(err)
}
}
// Randomize the struct values again, except for the primary key values, so we can call update.
err = boil.RandomizeSlice(&o1, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...)
tx, err := boil.Begin()
if err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice excluding primary keys: %s", err)
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
colsWithoutPrimKeys := strmangle.SetComplement({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns)
for i := 0; i < len(o1); i++ {
if err = o1[i].UpdateG(colsWithoutPrimKeys...); err != nil {
t.Errorf("Unable to update the {{$tableNameSingular}} row: %s", err)
}
if err = {{$varNameSingular}}.Reload(tx); err != nil {
t.Error(err)
}
}
func Test{{$tableNamePlural}}ReloadAll(t *testing.T) {
t.Parallel()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
tx, err := boil.Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
if err = {{$varNameSingular}}.Insert(tx); err != nil {
t.Error(err)
}
slice := {{$tableNameSingular}}Slice{{"{"}}{{$varNameSingular}}{{"}"}}
if err = slice.ReloadAll(tx); err != nil {
t.Error(err)
}
if err = o2.ReloadAllG(); err != nil {
t.Errorf("Unable to reload {{$tableNameSingular}} object: %s", err)
}
for i := 0; i < len(o1); i++ {
err = {{$varNameSingular}}CompareVals(o2[i], o1[i], true); if err != nil {
t.Error(err)
}
}
{{$varNamePlural}}DeleteAllRows(t)
}

View file

@ -3,6 +3,8 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Select(t *testing.T) {
t.Parallel()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)

View file

@ -3,6 +3,8 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Update(t *testing.T) {
t.Parallel()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -37,6 +39,8 @@ func Test{{$tableNamePlural}}Update(t *testing.T) {
}
func Test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
t.Parallel()
{{$varNameSingular}} := &{{$tableNameSingular}}{}
if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)

View file

@ -5,10 +5,10 @@
func Test{{$tableNamePlural}}Upsert(t *testing.T) {
var err error
o := {{$tableNameSingular}}{}
{{$varNameSingular}} := {{$tableNameSingular}}{}
// Attempt the INSERT side of an UPSERT
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
if err = boil.RandomizeStruct(&{{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
@ -18,32 +18,32 @@ func Test{{$tableNamePlural}}Upsert(t *testing.T) {
}
defer tx.Rollback()
if err = o.Upsert(tx, false, nil, nil); err != nil {
if err = {{$varNameSingular}}.Upsert(tx, false, nil, nil); err != nil {
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
}
compare, err := {{$tableNameSingular}}Find(tx, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
count, err := {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Errorf("Unable to find {{$tableNameSingular}}: %s", err)
}
err = {{$varNameSingular}}CompareVals(&o, compare, true); if err != nil {
t.Error(err)
}
if count != 1 {
t.Error("want one record, got:", count)
}
// Attempt the UPDATE side of an UPSERT
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
if err = boil.RandomizeStruct(&{{$varNameSingular}}, {{$varNameSingular}}DBTypes, false, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = o.Upsert(tx, true, nil, nil); err != nil {
if err = {{$varNameSingular}}.Upsert(tx, true, nil, nil); err != nil {
t.Errorf("Unable to upsert {{$tableNameSingular}}: %s", err)
}
compare, err = {{$tableNameSingular}}Find(tx, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
count, err = {{$tableNamePlural}}(tx).Count()
if err != nil {
t.Errorf("Unable to find {{$tableNameSingular}}: %s", err)
}
err = {{$varNameSingular}}CompareVals(&o, compare, true); if err != nil {
t.Error(err)
}
if count != 1 {
t.Error("want one record, got:", count)
}
}