Delete randomize slice
This commit is contained in:
parent
d25712f929
commit
8188648a11
10 changed files with 38 additions and 59 deletions
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue