From 8188648a116c0be860f53a0aaecf616b40e2896c Mon Sep 17 00:00:00 2001 From: Aaron L Date: Sun, 14 Aug 2016 02:11:32 -0700 Subject: [PATCH] Delete randomize slice --- boil/testing.go | 40 ------------------------------------ templates_test/delete.tpl | 12 +++++++++++ templates_test/exists.tpl | 2 +- templates_test/find.tpl | 2 +- templates_test/finishers.tpl | 12 +++++------ templates_test/helpers.tpl | 11 ++++++++-- templates_test/insert.tpl | 4 ++-- templates_test/reload.tpl | 4 ++-- templates_test/select.tpl | 2 +- templates_test/update.tpl | 8 ++++---- 10 files changed, 38 insertions(+), 59 deletions(-) diff --git a/boil/testing.go b/boil/testing.go index 75dcbb2..c22ebaa 100644 --- a/boil/testing.go +++ b/boil/testing.go @@ -46,46 +46,6 @@ func (s *seed) nextInt() int { return nextInt } -// RandomizeSlice takes a pointer to a slice of pointers to objects -// and fills the pointed to objects with random data. -// It will ignore the fields in the blacklist. -func RandomizeSlice(obj interface{}, colTypes map[string]string, includeInvalid bool, blacklist ...string) error { - ptrSlice := reflect.ValueOf(obj) - typ := ptrSlice.Type() - ptrSlice = ptrSlice.Elem() - kind := typ.Kind() - - var structTyp reflect.Type - - for i, exp := range []reflect.Kind{reflect.Ptr, reflect.Slice, reflect.Ptr, reflect.Struct} { - if i != 0 { - typ = typ.Elem() - kind = typ.Kind() - } - - if kind != exp { - return errors.Errorf("[%d] RandomizeSlice object type should be *[]*Type but was: %s", i, ptrSlice.Type().String()) - } - - if kind == reflect.Struct { - structTyp = typ - } - } - - for i := 0; i < ptrSlice.Len(); i++ { - o := ptrSlice.Index(i) - if o.IsNil() { - o.Set(reflect.New(structTyp)) - } - - if err := RandomizeStruct(o.Interface(), colTypes, includeInvalid, blacklist...); err != nil { - return err - } - } - - return nil -} - // RandomizeStruct takes an object and fills it with random data. // It will ignore the fields in the blacklist. func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid bool, blacklist ...string) error { diff --git a/templates_test/delete.tpl b/templates_test/delete.tpl index 26c33dc..65df3cc 100644 --- a/templates_test/delete.tpl +++ b/templates_test/delete.tpl @@ -12,6 +12,10 @@ func Test{{$tableNamePlural}}Delete(t *testing.T) { defer tx.Rollback() {{$varNameSingular}} := &{{$tableNameSingular}}{} + if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil { + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) + } + if err = {{$varNameSingular}}.Insert(tx); err != nil { t.Error(err) } @@ -40,6 +44,10 @@ func Test{{$tableNamePlural}}QueryDeleteAll(t *testing.T) { defer tx.Rollback() {{$varNameSingular}} := &{{$tableNameSingular}}{} + if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil { + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) + } + if err = {{$varNameSingular}}.Insert(tx); err != nil { t.Error(err) } @@ -68,6 +76,10 @@ func Test{{$tableNamePlural}}SliceDeleteAll(t *testing.T) { defer tx.Rollback() {{$varNameSingular}} := &{{$tableNameSingular}}{} + if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil { + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) + } + if err = {{$varNameSingular}}.Insert(tx); err != nil { t.Error(err) } diff --git a/templates_test/exists.tpl b/templates_test/exists.tpl index 76114da..ddf06cf 100644 --- a/templates_test/exists.tpl +++ b/templates_test/exists.tpl @@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Exists(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() diff --git a/templates_test/find.tpl b/templates_test/find.tpl index 25ac3d3..daa4022 100644 --- a/templates_test/find.tpl +++ b/templates_test/find.tpl @@ -5,7 +5,7 @@ func Test{{$tableNamePlural}}Find(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() diff --git a/templates_test/finishers.tpl b/templates_test/finishers.tpl index 31ed1d7..e43246f 100644 --- a/templates_test/finishers.tpl +++ b/templates_test/finishers.tpl @@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -30,7 +30,7 @@ func Test{{$tableNamePlural}}One(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -56,10 +56,10 @@ func Test{{$tableNamePlural}}All(t *testing.T) { {{$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) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } if err := boil.RandomizeStruct({{$varNameSingular}}Two, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -91,10 +91,10 @@ func Test{{$tableNamePlural}}Count(t *testing.T) { {{$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) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } if err := boil.RandomizeStruct({{$varNameSingular}}Two, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() diff --git a/templates_test/helpers.tpl b/templates_test/helpers.tpl index d9e051a..bdb3e29 100644 --- a/templates_test/helpers.tpl +++ b/templates_test/helpers.tpl @@ -5,6 +5,8 @@ var {{$varNameSingular}}DBTypes = map[string]string{{"{"}}{{.Table.Columns | columnDBTypes | makeStringMap}}{{"}"}} func Test{{$tableNamePlural}}InPrimaryKeyArgs(t *testing.T) { + t.Parallel() + var err error var o {{$tableNameSingular}} o = {{$tableNameSingular}}{} @@ -27,11 +29,16 @@ func Test{{$tableNamePlural}}InPrimaryKeyArgs(t *testing.T) { } func Test{{$tableNamePlural}}SliceInPrimaryKeyArgs(t *testing.T) { + t.Parallel() + var err error o := make({{$tableNameSingular}}Slice, 3) - if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil { - t.Errorf("Could not randomize slice: %s", err) + for i := range o { + o[i] = &{{$tableNameSingular}}{} + if err = boil.RandomizeStruct(o[i], {{$varNameSingular}}DBTypes, true); err != nil { + t.Errorf("Could not randomize struct: %s", err) + } } args := o.inPrimaryKeyArgs() diff --git a/templates_test/insert.tpl b/templates_test/insert.tpl index 4fc9160..5cb6bf6 100644 --- a/templates_test/insert.tpl +++ b/templates_test/insert.tpl @@ -8,7 +8,7 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -36,7 +36,7 @@ func Test{{$tableNamePlural}}InsertWhitelist(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() diff --git a/templates_test/reload.tpl b/templates_test/reload.tpl index 499e825..5b6cdf4 100644 --- a/templates_test/reload.tpl +++ b/templates_test/reload.tpl @@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Reload(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -30,7 +30,7 @@ func Test{{$tableNamePlural}}ReloadAll(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() diff --git a/templates_test/select.tpl b/templates_test/select.tpl index 6de93e9..a796129 100644 --- a/templates_test/select.tpl +++ b/templates_test/select.tpl @@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Select(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}ColumnsWithDefault...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() diff --git a/templates_test/update.tpl b/templates_test/update.tpl index 0ad9a28..34bc99b 100644 --- a/templates_test/update.tpl +++ b/templates_test/update.tpl @@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Update(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -30,7 +30,7 @@ func Test{{$tableNamePlural}}Update(t *testing.T) { } if err = boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } if err = {{$varNameSingular}}.Update(tx); err != nil { @@ -43,7 +43,7 @@ func Test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) { {{$varNameSingular}} := &{{$tableNameSingular}}{} if err := boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } tx, err := boil.Begin() @@ -66,7 +66,7 @@ func Test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) { } if err = boil.RandomizeStruct({{$varNameSingular}}, {{$varNameSingular}}DBTypes, true, {{$varNameSingular}}PrimaryKeyColumns...); err != nil { - t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) + t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err) } // Remove Primary keys and unique columns from what we plan to update