Fix wording of testing

This commit is contained in:
Aaron L 2016-08-14 03:07:23 -07:00
parent ff981ee885
commit 6a040bc11c

View file

@ -116,116 +116,117 @@ func randDate(sd int) time.Time {
return t return t
} }
func randomizeField(field reflect.Value, fieldType string, includeInvalid bool) error { func randomizeField(field reflect.Value, fieldType string, canBeNull bool) error {
kind := field.Kind() kind := field.Kind()
typ := field.Type() typ := field.Type()
var newVal interface{} var newVal interface{}
if kind == reflect.Struct { if kind == reflect.Struct {
var b bool var notNull bool
if includeInvalid { if canBeNull {
b = rand.Intn(2) == 1 notNull = rand.Intn(2) == 1
} else { } else {
b = true notNull = false
} }
switch typ { switch typ {
case typeNullBool: case typeNullBool:
if b { if notNull {
newVal = null.NewBool(sd.nextInt()%2 == 0, b) newVal = null.NewBool(sd.nextInt()%2 == 0, true)
} else { } else {
newVal = null.NewBool(false, false) newVal = null.NewBool(false, false)
} }
case typeNullString: case typeNullString:
if fieldType == "uuid" { if fieldType == "uuid" {
newVal = null.NewString(uuid.NewV4().String(), true) newVal = null.NewString(uuid.NewV4().String(), true)
} else if b { } else if notNull {
switch fieldType { switch fieldType {
case "interval": case "interval":
newVal = null.NewString(strconv.Itoa((sd.nextInt()%26)+2)+" days", b) newVal = null.NewString(strconv.Itoa((sd.nextInt()%26)+2)+" days", true)
default: default:
newVal = null.NewString(randStr(1, sd.nextInt()), b) newVal = null.NewString(randStr(1, sd.nextInt()), true)
} }
} else { } else {
newVal = null.NewString("", false) newVal = null.NewString("", false)
} }
case typeNullTime: case typeNullTime:
if b { if notNull {
newVal = null.NewTime(randDate(sd.nextInt()), b) newVal = null.NewTime(randDate(sd.nextInt()), true)
} else { } else {
newVal = null.NewTime(time.Time{}, false) newVal = null.NewTime(time.Time{}, false)
} }
case typeTime: case typeTime:
newVal = randDate(sd.nextInt()) newVal = randDate(sd.nextInt())
case typeNullFloat32: case typeNullFloat32:
if b { if notNull {
newVal = null.NewFloat32(float32(sd.nextInt()%10)/10.0+float32(sd.nextInt()%10), b) newVal = null.NewFloat32(float32(sd.nextInt()%10)/10.0+float32(sd.nextInt()%10), true)
} else { } else {
newVal = null.NewFloat32(0.0, false) newVal = null.NewFloat32(0.0, false)
} }
case typeNullFloat64: case typeNullFloat64:
if b { if notNull {
newVal = null.NewFloat64(float64(sd.nextInt()%10)/10.0+float64(sd.nextInt()%10), b) newVal = null.NewFloat64(float64(sd.nextInt()%10)/10.0+float64(sd.nextInt()%10), true)
} else { } else {
newVal = null.NewFloat64(0.0, false) newVal = null.NewFloat64(0.0, false)
} }
case typeNullInt: case typeNullInt:
if b { if notNull {
newVal = null.NewInt(sd.nextInt(), b) newVal = null.NewInt(sd.nextInt(), true)
} else { } else {
newVal = null.NewInt(0, false) newVal = null.NewInt(0, false)
} }
case typeNullInt8: case typeNullInt8:
if b { if notNull {
newVal = null.NewInt8(int8(sd.nextInt()), b) newVal = null.NewInt8(int8(sd.nextInt()), true)
} else { } else {
newVal = null.NewInt8(0, false) newVal = null.NewInt8(0, false)
} }
case typeNullInt16: case typeNullInt16:
if b { if notNull {
newVal = null.NewInt16(int16(sd.nextInt()), b) newVal = null.NewInt16(int16(sd.nextInt()), true)
} else { } else {
newVal = null.NewInt16(0, false) newVal = null.NewInt16(0, false)
} }
case typeNullInt32: case typeNullInt32:
if b { if notNull {
newVal = null.NewInt32(int32(sd.nextInt()), b) newVal = null.NewInt32(int32(sd.nextInt()), true)
} else { } else {
newVal = null.NewInt32(0, false) newVal = null.NewInt32(0, false)
} }
case typeNullInt64: case typeNullInt64:
if b { if notNull {
newVal = null.NewInt64(int64(sd.nextInt()), b) newVal = null.NewInt64(int64(sd.nextInt()), true)
} else { } else {
newVal = null.NewInt64(0, false) newVal = null.NewInt64(0, false)
} }
case typeNullUint: case typeNullUint:
if b { if notNull {
newVal = null.NewUint(uint(sd.nextInt()), b) newVal = null.NewUint(uint(sd.nextInt()), true)
} else { } else {
newVal = null.NewUint(0, false) newVal = null.NewUint(0, false)
} }
case typeNullUint8: case typeNullUint8:
if b { if notNull {
newVal = null.NewUint8(uint8(sd.nextInt()), b) newVal = null.NewUint8(uint8(sd.nextInt()), true)
} else { } else {
newVal = null.NewUint8(0, false) newVal = null.NewUint8(0, false)
} }
case typeNullUint16: case typeNullUint16:
if b { if notNull {
newVal = null.NewUint16(uint16(sd.nextInt()), b) newVal = null.NewUint16(uint16(sd.nextInt()), true)
} else { } else {
newVal = null.NewUint16(0, false) newVal = null.NewUint16(0, false)
} }
case typeNullUint32: case typeNullUint32:
if b { if notNull {
newVal = null.NewUint32(uint32(sd.nextInt()), b) newVal = null.NewUint32(uint32(sd.nextInt()), true)
} else { } else {
newVal = null.NewUint32(0, false) newVal = null.NewUint32(0, false)
} }
case typeNullUint64: case typeNullUint64:
if b { if notNull {
newVal = null.NewUint64(uint64(sd.nextInt()), b) newVal = null.NewUint64(uint64(sd.nextInt()), true)
} else { } else {
newVal = null.NewUint64(0, false) newVal = null.NewUint64(0, false)
} }
@ -257,13 +258,7 @@ func randomizeField(field reflect.Value, fieldType string, includeInvalid bool)
case reflect.Uint64: case reflect.Uint64:
newVal = uint64(sd.nextInt()) newVal = uint64(sd.nextInt())
case reflect.Bool: case reflect.Bool:
var b bool newVal = sd.nextInt()%2 == 0
if includeInvalid {
b = sd.nextInt()%2 == 0
} else {
b = true
}
newVal = b
case reflect.String: case reflect.String:
switch fieldType { switch fieldType {
case "interval": case "interval":