Destruct ugly code bits from boil package
This commit is contained in:
parent
b0cd6828b2
commit
b34a8e3212
2 changed files with 0 additions and 252 deletions
104
boil/testing.go
104
boil/testing.go
|
@ -1,7 +1,6 @@
|
||||||
package boil
|
package boil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -47,73 +46,6 @@ func (s *seed) nextInt() int {
|
||||||
return nextInt
|
return nextInt
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsZeroValue checks if the variables with matching columns in obj
|
|
||||||
// are or are not zero values, depending on whether shouldZero is true or false
|
|
||||||
func IsZeroValue(obj interface{}, shouldZero bool, columns ...string) []error {
|
|
||||||
val := reflect.Indirect(reflect.ValueOf(obj))
|
|
||||||
|
|
||||||
var errs []error
|
|
||||||
for _, c := range columns {
|
|
||||||
field := val.FieldByName(strmangle.TitleCase(c))
|
|
||||||
if !field.IsValid() {
|
|
||||||
panic(fmt.Sprintf("Unable to find variable with column name %s", c))
|
|
||||||
}
|
|
||||||
|
|
||||||
zv := reflect.Zero(field.Type())
|
|
||||||
if shouldZero && !reflect.DeepEqual(field.Interface(), zv.Interface()) {
|
|
||||||
errs = append(errs, errors.Errorf("Column with name %s is not zero value: %#v, %#v", c, field.Interface(), zv.Interface()))
|
|
||||||
} else if !shouldZero && reflect.DeepEqual(field.Interface(), zv.Interface()) {
|
|
||||||
errs = append(errs, errors.Errorf("Column with name %s is zero value: %#v, %#v", c, field.Interface(), zv.Interface()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return errs
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValueMatch checks whether the variables in obj with matching column names
|
|
||||||
// match the values in the values slice.
|
|
||||||
func IsValueMatch(obj interface{}, columns []string, values []interface{}) []error {
|
|
||||||
val := reflect.Indirect(reflect.ValueOf(obj))
|
|
||||||
|
|
||||||
var errs []error
|
|
||||||
for i, c := range columns {
|
|
||||||
field := val.FieldByName(strmangle.TitleCase(c))
|
|
||||||
if !field.IsValid() {
|
|
||||||
panic(fmt.Sprintf("Unable to find variable with column name %s", c))
|
|
||||||
}
|
|
||||||
|
|
||||||
typ := field.Type().String()
|
|
||||||
if typ == "time.Time" || typ == "null.Time" {
|
|
||||||
var timeField reflect.Value
|
|
||||||
var valTimeStr string
|
|
||||||
if typ == "time.Time" {
|
|
||||||
valTimeStr = values[i].(time.Time).String()
|
|
||||||
timeField = field
|
|
||||||
} else {
|
|
||||||
valTimeStr = values[i].(null.Time).Time.String()
|
|
||||||
timeField = field.FieldByName("Time")
|
|
||||||
validField := field.FieldByName("Valid")
|
|
||||||
if validField.Interface() != values[i].(null.Time).Valid {
|
|
||||||
errs = append(errs, errors.Errorf("Null.Time column with name %s Valid field does not match: %v ≠ %v", c, values[i].(null.Time).Valid, validField.Interface()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rgxValidTime.MatchString(valTimeStr) && timeField.Interface() == reflect.Zero(timeField.Type()).Interface()) ||
|
|
||||||
(!rgxValidTime.MatchString(valTimeStr) && timeField.Interface() != reflect.Zero(timeField.Type()).Interface()) {
|
|
||||||
errs = append(errs, errors.Errorf("Time column with name %s Time field does not match: %v ≠ %v", c, values[i], timeField.Interface()))
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(field.Interface(), values[i]) {
|
|
||||||
errs = append(errs, errors.Errorf("Column with name %s does not match value: %#v ≠ %#v", c, values[i], field.Interface()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return errs
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomizeSlice takes a pointer to a slice of pointers to objects
|
// RandomizeSlice takes a pointer to a slice of pointers to objects
|
||||||
// and fills the pointed to objects with random data.
|
// and fills the pointed to objects with random data.
|
||||||
// It will ignore the fields in the blacklist.
|
// It will ignore the fields in the blacklist.
|
||||||
|
@ -207,42 +139,6 @@ func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomizeValidatedStruct takes an object fills its validated columns with random data.
|
|
||||||
func RandomizeValidatedStruct(obj interface{}, validatedCols []string, colTypes map[string]string) error {
|
|
||||||
// Check if it's pointer
|
|
||||||
value := reflect.ValueOf(obj)
|
|
||||||
kind := value.Kind()
|
|
||||||
if kind != reflect.Ptr {
|
|
||||||
return errors.Errorf("Outer element should be a pointer, given a non-pointer: %T", obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if it's a struct
|
|
||||||
value = value.Elem()
|
|
||||||
kind = value.Kind()
|
|
||||||
if kind != reflect.Struct {
|
|
||||||
return errors.Errorf("Inner element should be a struct, given a non-struct: %T", obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
typ := value.Type()
|
|
||||||
nFields := value.NumField()
|
|
||||||
|
|
||||||
// Iterate through fields
|
|
||||||
for i := 0; i < nFields; i++ {
|
|
||||||
fieldVal := value.Field(i)
|
|
||||||
fieldTyp := typ.Field(i)
|
|
||||||
for _, v := range validatedCols {
|
|
||||||
if strmangle.TitleCase(v) == fieldTyp.Name {
|
|
||||||
if err := randomizeField(fieldVal, colTypes[fieldTyp.Name], false); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// randDate generates a random time.Time between 1850 and 2050.
|
// randDate generates a random time.Time between 1850 and 2050.
|
||||||
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
|
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
|
||||||
// not cause mismatches in the test data comparisons.
|
// not cause mismatches in the test data comparisons.
|
||||||
|
|
|
@ -7,119 +7,6 @@ import (
|
||||||
"gopkg.in/nullbio/null.v4"
|
"gopkg.in/nullbio/null.v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsZeroValue(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
o := struct {
|
|
||||||
A []byte
|
|
||||||
B time.Time
|
|
||||||
C null.Time
|
|
||||||
D null.Int64
|
|
||||||
E int64
|
|
||||||
}{}
|
|
||||||
|
|
||||||
if errs := IsZeroValue(o, true, "A", "B", "C", "D", "E"); errs != nil {
|
|
||||||
for _, e := range errs {
|
|
||||||
t.Errorf("%s", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
colNames := []string{"A", "B", "C", "D", "E"}
|
|
||||||
for _, c := range colNames {
|
|
||||||
if err := IsZeroValue(o, true, c); err != nil {
|
|
||||||
t.Errorf("Expected %s to be zero value: %s", c, err[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
o.A = []byte("asdf")
|
|
||||||
o.B = time.Now()
|
|
||||||
o.C = null.NewTime(time.Now(), false)
|
|
||||||
o.D = null.NewInt64(2, false)
|
|
||||||
o.E = 5
|
|
||||||
|
|
||||||
if errs := IsZeroValue(o, false, "A", "B", "C", "D", "E"); errs != nil {
|
|
||||||
for _, e := range errs {
|
|
||||||
t.Errorf("%s", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range colNames {
|
|
||||||
if err := IsZeroValue(o, false, c); err != nil {
|
|
||||||
t.Errorf("Expected %s to be non-zero value: %s", c, err[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsValueMatch(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
var errs []error
|
|
||||||
var values []interface{}
|
|
||||||
|
|
||||||
o := struct {
|
|
||||||
A []byte
|
|
||||||
B time.Time
|
|
||||||
C null.Time
|
|
||||||
D null.Int64
|
|
||||||
E int64
|
|
||||||
}{}
|
|
||||||
|
|
||||||
values = []interface{}{
|
|
||||||
[]byte(nil),
|
|
||||||
time.Time{},
|
|
||||||
null.Time{},
|
|
||||||
null.Int64{},
|
|
||||||
int64(0),
|
|
||||||
}
|
|
||||||
|
|
||||||
cols := []string{"A", "B", "C", "D", "E"}
|
|
||||||
errs = IsValueMatch(o, cols, values)
|
|
||||||
if errs != nil {
|
|
||||||
for _, e := range errs {
|
|
||||||
t.Errorf("%s", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
values = []interface{}{
|
|
||||||
[]byte("hi"),
|
|
||||||
time.Date(2007, 11, 2, 1, 1, 1, 1, time.UTC),
|
|
||||||
null.NewTime(time.Date(2007, 11, 2, 1, 1, 1, 1, time.UTC), true),
|
|
||||||
null.NewInt64(5, false),
|
|
||||||
int64(6),
|
|
||||||
}
|
|
||||||
|
|
||||||
errs = IsValueMatch(o, cols, values)
|
|
||||||
// Expect 6 errors
|
|
||||||
// 5 for each column and an additional 1 for the invalid Valid field match
|
|
||||||
if len(errs) != 6 {
|
|
||||||
t.Errorf("Expected 6 errors, got: %d", len(errs))
|
|
||||||
for _, e := range errs {
|
|
||||||
t.Errorf("%s", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
o.A = []byte("hi")
|
|
||||||
o.B = time.Date(2007, 11, 2, 1, 1, 1, 1, time.UTC)
|
|
||||||
o.C = null.NewTime(time.Date(2007, 11, 2, 1, 1, 1, 1, time.UTC), true)
|
|
||||||
o.D = null.NewInt64(5, false)
|
|
||||||
o.E = 6
|
|
||||||
|
|
||||||
errs = IsValueMatch(o, cols, values)
|
|
||||||
if errs != nil {
|
|
||||||
for _, e := range errs {
|
|
||||||
t.Errorf("%s", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
o.B = time.Date(2007, 11, 2, 2, 2, 2, 2, time.UTC)
|
|
||||||
errs = IsValueMatch(o, cols, values)
|
|
||||||
if errs != nil {
|
|
||||||
for _, e := range errs {
|
|
||||||
t.Errorf("%s", e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRandomizeStruct(t *testing.T) {
|
func TestRandomizeStruct(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
@ -190,38 +77,3 @@ func TestRandomizeStruct(t *testing.T) {
|
||||||
t.Errorf("the null values are not being randomized: %#v", testStruct)
|
t.Errorf("the null values are not being randomized: %#v", testStruct)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRandomizeValidatedStruct(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
var testStruct = struct {
|
|
||||||
Int1 int
|
|
||||||
NullInt1 null.Int
|
|
||||||
UUID1 string
|
|
||||||
UUID2 string
|
|
||||||
}{}
|
|
||||||
|
|
||||||
validatedCols := []string{
|
|
||||||
"uuid1",
|
|
||||||
"uuid2",
|
|
||||||
}
|
|
||||||
fieldTypes := map[string]string{
|
|
||||||
"Int": "integer",
|
|
||||||
"NullInt": "integer",
|
|
||||||
"UUID1": "uuid",
|
|
||||||
"UUID2": "uuid",
|
|
||||||
}
|
|
||||||
|
|
||||||
err := RandomizeValidatedStruct(&testStruct, validatedCols, fieldTypes)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if testStruct.Int1 != 0 || testStruct.NullInt1.Int != 0 ||
|
|
||||||
testStruct.NullInt1.Valid != false {
|
|
||||||
t.Errorf("the regular values are being randomized when they should be zero vals: %#v", testStruct)
|
|
||||||
}
|
|
||||||
|
|
||||||
if testStruct.UUID1 == "" || testStruct.UUID2 == "" {
|
|
||||||
t.Errorf("the validated values should be set: %#v", testStruct)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue