Update schema w/ nullbyte and byte, fix randomizer

This commit is contained in:
Patrick O'brien 2016-11-12 13:47:59 +10:00
parent 86ca3bbcea
commit 6f0fce21b8
2 changed files with 22 additions and 5 deletions

View file

@ -4,6 +4,7 @@ package randomize
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"math/rand"
"reflect" "reflect"
"regexp" "regexp"
"sort" "sort"
@ -34,6 +35,7 @@ var (
typeNullUint32 = reflect.TypeOf(null.Uint32{}) typeNullUint32 = reflect.TypeOf(null.Uint32{})
typeNullUint64 = reflect.TypeOf(null.Uint64{}) typeNullUint64 = reflect.TypeOf(null.Uint64{})
typeNullString = reflect.TypeOf(null.String{}) typeNullString = reflect.TypeOf(null.String{})
typeNullByte = reflect.TypeOf(null.Byte{})
typeNullBool = reflect.TypeOf(null.Bool{}) typeNullBool = reflect.TypeOf(null.Bool{})
typeNullTime = reflect.TypeOf(null.Time{}) typeNullTime = reflect.TypeOf(null.Time{})
typeNullBytes = reflect.TypeOf(null.Bytes{}) typeNullBytes = reflect.TypeOf(null.Bytes{})
@ -341,7 +343,7 @@ func randomizeField(s *Seed, field reflect.Value, fieldType string, canBeNull bo
// only get zero values for non byte slices // only get zero values for non byte slices
// to stop mysql from being a jerk // to stop mysql from being a jerk
if isNull && kind != reflect.Slice { if isNull && kind != reflect.Slice {
value = getVariableZeroValue(s, kind) value = getVariableZeroValue(s, kind, typ)
} else { } else {
value = getVariableRandValue(s, kind, typ) value = getVariableRandValue(s, kind, typ)
} }
@ -457,6 +459,8 @@ func getStructNullValue(s *Seed, typ reflect.Type) interface{} {
return null.NewUint64(0, false) return null.NewUint64(0, false)
case typeNullBytes: case typeNullBytes:
return null.NewBytes(nil, false) return null.NewBytes(nil, false)
case typeNullByte:
return null.NewByte(byte(0), false)
} }
return nil return nil
@ -501,13 +505,21 @@ func getStructRandValue(s *Seed, typ reflect.Type) interface{} {
return null.NewUint64(uint64(s.nextInt()), true) return null.NewUint64(uint64(s.nextInt()), true)
case typeNullBytes: case typeNullBytes:
return null.NewBytes(randByteSlice(s, 1), true) return null.NewBytes(randByteSlice(s, 1), true)
case typeNullByte:
return null.NewByte(byte(rand.Intn(125-65)+65), true)
} }
return nil return nil
} }
// getVariableZeroValue for the matching type. // getVariableZeroValue for the matching type.
func getVariableZeroValue(s *Seed, kind reflect.Kind) interface{} { func getVariableZeroValue(s *Seed, kind reflect.Kind, typ reflect.Type) interface{} {
switch typ.String() {
case "types.Byte":
// Decimal 65 is 'A'. 0 is not a valid UTF8, so cannot use a zero value here.
return types.Byte(65)
}
switch kind { switch kind {
case reflect.Float32: case reflect.Float32:
return float32(0) return float32(0)
@ -548,6 +560,11 @@ func getVariableZeroValue(s *Seed, kind reflect.Kind) interface{} {
// The randomness is really an incrementation of the global seed, // The randomness is really an incrementation of the global seed,
// this is done to avoid duplicate key violations. // this is done to avoid duplicate key violations.
func getVariableRandValue(s *Seed, kind reflect.Kind, typ reflect.Type) interface{} { func getVariableRandValue(s *Seed, kind reflect.Kind, typ reflect.Type) interface{} {
switch typ.String() {
case "types.Byte":
return types.Byte(rand.Intn(125-65) + 65)
}
switch kind { switch kind {
case reflect.Float32: case reflect.Float32:
return float32(float32(s.nextInt()%10)/10.0 + float32(s.nextInt()%10)) return float32(float32(s.nextInt()%10)/10.0 + float32(s.nextInt()%10))

View file

@ -37,8 +37,8 @@ CREATE TABLE magic (
byte_zero "char", byte_zero "char",
byte_one "char" NULL, byte_one "char" NULL,
byte_two "char" NOT NULL, byte_two "char" NULL DEFAULT 'a',
byte_three "char" NULL DEFAULT 'a', byte_three "char" NOT NULL,
byte_four "char" NOT NULL DEFAULT 'b', byte_four "char" NOT NULL DEFAULT 'b',
big_int_zero bigint, big_int_zero bigint,