From 98e0b1f2ad9767d28cc620002e5b533eefc4a45e Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Sat, 11 Jun 2016 18:30:56 +1000 Subject: [PATCH] Fixed unit test comparison bug for Null types * Set randomized field values to zero value for objects with Valid false This ensures the types will match the zero values returned from the db. * Reverted make statements to proper initialization format --- boil/reflect.go | 92 ++++++++++++++++++++++++++----- cmds/templates_test/find.tpl | 2 +- cmds/templates_test/finishers.tpl | 2 +- 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/boil/reflect.go b/boil/reflect.go index 6af57ef..99c882d 100644 --- a/boil/reflect.go +++ b/boil/reflect.go @@ -310,39 +310,100 @@ func randomizeField(field reflect.Value) error { var newVal interface{} if kind == reflect.Struct { + b := rand.Intn(2) == 1 switch typ { case typeNullBool: - newVal = null.NewBool(rand.Intn(2) == 1, rand.Intn(2) == 1) + if b { + newVal = null.NewBool(rand.Intn(2) == 1, b) + } else { + newVal = null.NewBool(false, false) + } case typeNullString: - newVal = null.NewString(randStr(5+rand.Intn(25)), rand.Intn(2) == 1) + if b { + newVal = null.NewString(randStr(5+rand.Intn(25)), b) + } else { + newVal = null.NewString("", false) + } case typeNullTime: - newVal = null.NewTime(randDate(), rand.Intn(2) == 1) + if b { + newVal = null.NewTime(randDate(), b) + } else { + newVal = null.NewTime(time.Time{}, false) + } case typeTime: newVal = randDate() case typeNullFloat32: - newVal = null.NewFloat32(float32(rand.Intn(9))/10.0+float32(rand.Intn(9)), rand.Intn(2) == 1) + if b { + newVal = null.NewFloat32(float32(rand.Intn(9))/10.0+float32(rand.Intn(9)), b) + } else { + newVal = null.NewFloat32(0.0, false) + } case typeNullFloat64: - newVal = null.NewFloat64(float64(rand.Intn(9))/10.0+float64(rand.Intn(9)), rand.Intn(2) == 1) + if b { + newVal = null.NewFloat64(float64(rand.Intn(9))/10.0+float64(rand.Intn(9)), b) + } else { + newVal = null.NewFloat64(0.0, false) + } case typeNullInt: - newVal = null.NewInt(rand.Int(), rand.Intn(2) == 1) + if b { + newVal = null.NewInt(rand.Int(), b) + } else { + newVal = null.NewInt(0, false) + } case typeNullInt8: - newVal = null.NewInt8(int8(rand.Intn(int(math.MaxInt8))), rand.Intn(2) == 1) + if b { + newVal = null.NewInt8(int8(rand.Intn(int(math.MaxInt8))), b) + } else { + newVal = null.NewInt8(0, false) + } case typeNullInt16: - newVal = null.NewInt16(int16(rand.Intn(int(math.MaxInt16))), rand.Intn(2) == 1) + if b { + newVal = null.NewInt16(int16(rand.Intn(int(math.MaxInt16))), b) + } else { + newVal = null.NewInt16(0, false) + } case typeNullInt32: - newVal = null.NewInt32(rand.Int31(), rand.Intn(2) == 1) + if b { + newVal = null.NewInt32(rand.Int31(), b) + } else { + newVal = null.NewInt32(0, false) + } case typeNullInt64: - newVal = null.NewInt64(rand.Int63(), rand.Intn(2) == 1) + if b { + newVal = null.NewInt64(rand.Int63(), b) + } else { + newVal = null.NewInt64(0, false) + } case typeNullUint: - newVal = null.NewUint(uint(rand.Int()), rand.Intn(2) == 1) + if b { + newVal = null.NewUint(uint(rand.Int()), b) + } else { + newVal = null.NewUint(0, false) + } case typeNullUint8: - newVal = null.NewUint8(uint8(rand.Intn(int(math.MaxInt8))), rand.Intn(2) == 1) + if b { + newVal = null.NewUint8(uint8(rand.Intn(int(math.MaxInt8))), b) + } else { + newVal = null.NewUint8(0, false) + } case typeNullUint16: - newVal = null.NewUint16(uint16(rand.Intn(int(math.MaxInt16))), rand.Intn(2) == 1) + if b { + newVal = null.NewUint16(uint16(rand.Intn(int(math.MaxInt16))), b) + } else { + newVal = null.NewUint16(0, false) + } case typeNullUint32: - newVal = null.NewUint32(uint32(rand.Int31()), rand.Intn(2) == 1) + if b { + newVal = null.NewUint32(uint32(rand.Int31()), b) + } else { + newVal = null.NewUint32(0, false) + } case typeNullUint64: - newVal = null.NewUint64(uint64(rand.Int63()), rand.Intn(2) == 1) + if b { + newVal = null.NewUint64(uint64(rand.Int63()), b) + } else { + newVal = null.NewUint64(0, false) + } } } else { switch kind { @@ -388,6 +449,7 @@ func randomizeField(field reflect.Value) error { return fmt.Errorf("unsupported type: %T", typ.String()) } } + field.Set(reflect.ValueOf(newVal)) return nil diff --git a/cmds/templates_test/find.tpl b/cmds/templates_test/find.tpl index 914de8a..8e48959 100644 --- a/cmds/templates_test/find.tpl +++ b/cmds/templates_test/find.tpl @@ -19,7 +19,7 @@ func Test{{$tableNamePlural}}Find(t *testing.T) { } } - j := make({{$varNameSingular}}Slice, 0, 3) + j := make({{$varNameSingular}}Slice, 3) // Perform all Find queries and assign result objects to slice for comparison for i := 0; i < len(j); i++ { j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}}) diff --git a/cmds/templates_test/finishers.tpl b/cmds/templates_test/finishers.tpl index 80152d9..036d8ef 100644 --- a/cmds/templates_test/finishers.tpl +++ b/cmds/templates_test/finishers.tpl @@ -94,7 +94,7 @@ func Test{{$tableNamePlural}}Count(t *testing.T) { {{$varNamePlural}}DeleteAllRows(t) - o := make({{$varNameSingular}}Slice, 0, 3) + o := make({{$varNameSingular}}Slice, 3) if err = boil.RandomizeSlice(&o); err != nil { t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) }