Added RandomizeSlice function
This commit is contained in:
parent
1e412cec74
commit
ecb20cca4a
3 changed files with 88 additions and 21 deletions
|
@ -108,8 +108,8 @@ func BindAll(rows *sql.Rows, selectCols []string, obj interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
newStruct := reflect.New(structTyp)
|
||||
for rows.Next() {
|
||||
newStruct := reflect.New(structTyp)
|
||||
pointers := GetStructPointers(newStruct.Interface(), selectCols...)
|
||||
if err := rows.Scan(pointers...); err != nil {
|
||||
return fmt.Errorf("Unable to scan into pointers: %s", err)
|
||||
|
@ -201,6 +201,43 @@ func GetStructPointers(obj interface{}, columns ...string) []interface{} {
|
|||
return ret
|
||||
}
|
||||
|
||||
// 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{}, 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 fmt.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)
|
||||
o.Set(reflect.New(structTyp))
|
||||
if err := RandomizeStruct(o.Interface(), 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{}, blacklist ...string) error {
|
||||
|
@ -215,14 +252,14 @@ func RandomizeStruct(str interface{}, blacklist ...string) error {
|
|||
value := reflect.ValueOf(str)
|
||||
kind := value.Kind()
|
||||
if kind != reflect.Ptr {
|
||||
return fmt.Errorf("can only randomize pointers to structs, given: %T", str)
|
||||
return fmt.Errorf("Outer element should be a pointer, given a non-pointer: %T", str)
|
||||
}
|
||||
|
||||
// Check if it's a struct
|
||||
value = value.Elem()
|
||||
kind = value.Kind()
|
||||
if kind != reflect.Struct {
|
||||
return fmt.Errorf("can only randomize pointers to structs, given: %T", str)
|
||||
return fmt.Errorf("Inner element should be a struct, given a non-struct: %T", str)
|
||||
}
|
||||
|
||||
typ := value.Type()
|
||||
|
|
|
@ -2,21 +2,20 @@
|
|||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
||||
{{- $varNamePlural := camelCasePlural .Table.Name -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
func Test{{$tableNamePlural}}All(t *testing.T) {
|
||||
var err error
|
||||
|
||||
// Start from a clean slate
|
||||
{{$varNamePlural}}DeleteAllRows(t)
|
||||
|
||||
r := make([]{{$tableNameSingular}}, 2)
|
||||
r := make({{$varNameSingular}}Slice, 2)
|
||||
if err = boil.RandomizeSlice(&r); err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
||||
}
|
||||
|
||||
// insert two random columns to test DeleteAll
|
||||
for i := 0; i < len(r); i++ {
|
||||
err = boil.RandomizeStruct(&r[i])
|
||||
if err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
|
||||
}
|
||||
|
||||
err = r[i].Insert()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", r[i], err)
|
||||
|
@ -37,14 +36,12 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
|
|||
t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c)
|
||||
}
|
||||
|
||||
o := make([]{{$tableNameSingular}}, 3)
|
||||
o := make({{$varNameSingular}}Slice, 3)
|
||||
if err = boil.RandomizeSlice(&o); err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
||||
}
|
||||
|
||||
for i := 0; i < len(o); i++ {
|
||||
err = boil.RandomizeStruct(&o[i])
|
||||
if err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
|
||||
}
|
||||
|
||||
err = o[i].Insert()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
||||
{{- $varNamePlural := camelCasePlural .Table.Name -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
func {{$varNamePlural}}DeleteAllRows(t *testing.T) {
|
||||
// Delete all rows to give a clean slate
|
||||
err := {{$tableNamePlural}}().DeleteAll()
|
||||
|
@ -16,11 +17,11 @@ func Test{{$tableNamePlural}}Delete(t *testing.T) {
|
|||
// Start from a clean slate
|
||||
{{$varNamePlural}}DeleteAllRows(t)
|
||||
|
||||
r := make([]{{$tableNameSingular}}, 3)
|
||||
r := make({{$varNameSingular}}Slice, 3)
|
||||
|
||||
// insert random columns to test DeleteAll
|
||||
for i := 0; i < len(r); i++ {
|
||||
err = boil.RandomizeStruct(&r[i])
|
||||
err = boil.RandomizeStruct(r[i])
|
||||
if err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
|
||||
}
|
||||
|
@ -31,7 +32,7 @@ func Test{{$tableNamePlural}}Delete(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Test DeleteAll()
|
||||
// Test DeleteAll() query function
|
||||
err = {{$tableNamePlural}}().DeleteAll()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
|
||||
|
@ -45,10 +46,10 @@ func Test{{$tableNamePlural}}Delete(t *testing.T) {
|
|||
t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c)
|
||||
}
|
||||
|
||||
// insert random columns to test Delete
|
||||
o := make([]{{$tableNameSingular}}, 3)
|
||||
// insert random columns to test DeleteAll
|
||||
o := make({{$varNameSingular}}Slice, 3)
|
||||
for i := 0; i < len(o); i++ {
|
||||
err = boil.RandomizeStruct(&o[i])
|
||||
err = boil.RandomizeStruct(o[i])
|
||||
if err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
|
||||
}
|
||||
|
@ -59,4 +60,36 @@ func Test{{$tableNamePlural}}Delete(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// test DeleteAll slice function
|
||||
err = o.DeleteAll()
|
||||
|
||||
// Check number of rows in table to ensure DeleteAll was successful
|
||||
c, err = {{$tableNamePlural}}().Count()
|
||||
|
||||
if c != 0 {
|
||||
t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c)
|
||||
}
|
||||
|
||||
// insert random columns to test Delete
|
||||
o = make({{$varNameSingular}}Slice, 3)
|
||||
for i := 0; i < len(o); i++ {
|
||||
err = boil.RandomizeStruct(o[i])
|
||||
if err != nil {
|
||||
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
|
||||
}
|
||||
|
||||
err = o[i].Insert()
|
||||
if err != nil {
|
||||
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
||||
}
|
||||
}
|
||||
|
||||
//o[0].Delete()
|
||||
|
||||
// Check number of rows in table to ensure DeleteAll was successful
|
||||
c, err = {{$tableNamePlural}}().Count()
|
||||
|
||||
if c != 2 {
|
||||
t.Errorf("Expected {{.Table.Name}} table to have 2 rows, but got %d rows", c)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue