Add MustTx helper, move deletes to bottom of tests

This commit is contained in:
Patrick O'brien 2016-07-15 15:32:36 +10:00
parent e78f19f3e9
commit 726fae9af1
6 changed files with 24 additions and 16 deletions

View file

@ -197,7 +197,9 @@ var defaultSingletonTestTemplateImports = map[string]imports{
`"math/rand"`, `"math/rand"`,
`"bytes"`, `"bytes"`,
}, },
thirdParty: importList{}, thirdParty: importList{
`"github.com/nullbio/sqlboiler/boil"`,
},
}, },
} }

View file

@ -6,9 +6,6 @@
func Test{{$tableNamePlural}}(t *testing.T) { func Test{{$tableNamePlural}}(t *testing.T) {
var err error var err error
// Start from a clean slate
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 2) o := make({{$varNameSingular}}Slice, 2)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -63,4 +60,6 @@ func Test{{$tableNamePlural}}(t *testing.T) {
if len(res) != 3 { if len(res) != 3 {
t.Errorf("Expected 3 {{$tableNameSingular}} rows, got %d", len(res)) t.Errorf("Expected 3 {{$tableNameSingular}} rows, got %d", len(res))
} }
{{$varNamePlural}}DeleteAllRows(t)
} }

View file

@ -6,8 +6,6 @@
func Test{{$tableNamePlural}}Find(t *testing.T) { func Test{{$tableNamePlural}}Find(t *testing.T) {
var err error var err error
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3) o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -41,4 +39,6 @@ func Test{{$tableNamePlural}}Find(t *testing.T) {
t.Errorf("Expected all other columns to be zero value, but column %s was %#v", v, val.Interface()) t.Errorf("Expected all other columns to be zero value, but column %s was %#v", v, val.Interface())
} }
} }
{{$varNamePlural}}DeleteAllRows(t)
} }

View file

@ -6,8 +6,6 @@
func Test{{$tableNamePlural}}Bind(t *testing.T) { func Test{{$tableNamePlural}}Bind(t *testing.T) {
var err error var err error
{{$varNamePlural}}DeleteAllRows(t)
o := {{$tableNameSingular}}{} o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
@ -55,13 +53,13 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) {
for i := 0; i < len(y); i++ { for i := 0; i < len(y); i++ {
{{$varNameSingular}}CompareVals(y[i], k[i], t) {{$varNameSingular}}CompareVals(y[i], k[i], t)
} }
{{$varNamePlural}}DeleteAllRows(t)
} }
func Test{{$tableNamePlural}}One(t *testing.T) { func Test{{$tableNamePlural}}One(t *testing.T) {
var err error var err error
{{$varNamePlural}}DeleteAllRows(t)
o := {{$tableNameSingular}}{} o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
@ -77,13 +75,13 @@ func Test{{$tableNamePlural}}One(t *testing.T) {
} }
{{$varNameSingular}}CompareVals(&o, j, t) {{$varNameSingular}}CompareVals(&o, j, t)
{{$varNamePlural}}DeleteAllRows(t)
} }
func Test{{$tableNamePlural}}All(t *testing.T) { func Test{{$tableNamePlural}}All(t *testing.T) {
var err error var err error
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3) o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -109,13 +107,13 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
for i := 0; i < len(o); i++ { for i := 0; i < len(o); i++ {
{{$varNameSingular}}CompareVals(o[i], j[i], t) {{$varNameSingular}}CompareVals(o[i], j[i], t)
} }
{{$varNamePlural}}DeleteAllRows(t)
} }
func Test{{$tableNamePlural}}Count(t *testing.T) { func Test{{$tableNamePlural}}Count(t *testing.T) {
var err error var err error
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3) o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -137,4 +135,6 @@ func Test{{$tableNamePlural}}Count(t *testing.T) {
if c != 3 { if c != 3 {
t.Errorf("Expected 3 results from count {{$tableNameSingular}}, got %d", c) t.Errorf("Expected 3 results from count {{$tableNameSingular}}, got %d", c)
} }
{{$varNamePlural}}DeleteAllRows(t)
} }

View file

@ -16,8 +16,6 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
nullTime := null.NewTime(time.Time{}, true) nullTime := null.NewTime(time.Time{}, true)
_ = nullTime _ = nullTime
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3) o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil { if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
@ -105,4 +103,6 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
if !reflect.DeepEqual(wl, {{$varNameSingular}}Columns) { if !reflect.DeepEqual(wl, {{$varNameSingular}}Columns) {
t.Errorf("Expected whitelist to contain all columns values:\n\nGot: %v\nWanted: %v", wl, {{$varNameSingular}}Columns) t.Errorf("Expected whitelist to contain all columns values:\n\nGot: %v\nWanted: %v", wl, {{$varNameSingular}}Columns)
} }
{{$varNamePlural}}DeleteAllRows(t)
} }

View file

@ -1,5 +1,12 @@
var dbNameRand *rand.Rand var dbNameRand *rand.Rand
func MustTx(transactor boil.Transactor, err error) boil.Transactor {
if err != nil {
panic(fmt.Sprintf("Cannot create a transactor: %s", err))
}
return transactor
}
func initDBNameRand(input string) { func initDBNameRand(input string) {
sum := md5.Sum([]byte(input)) sum := md5.Sum([]byte(input))