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
This commit is contained in:
Patrick O'brien 2016-06-11 18:30:56 +10:00
parent ca9d296f99
commit 98e0b1f2ad
3 changed files with 79 additions and 17 deletions

View file

@ -310,39 +310,100 @@ func randomizeField(field reflect.Value) error {
var newVal interface{} var newVal interface{}
if kind == reflect.Struct { if kind == reflect.Struct {
b := rand.Intn(2) == 1
switch typ { switch typ {
case typeNullBool: 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: 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: 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: case typeTime:
newVal = randDate() newVal = randDate()
case typeNullFloat32: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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 { } else {
switch kind { switch kind {
@ -388,6 +449,7 @@ func randomizeField(field reflect.Value) error {
return fmt.Errorf("unsupported type: %T", typ.String()) return fmt.Errorf("unsupported type: %T", typ.String())
} }
} }
field.Set(reflect.ValueOf(newVal)) field.Set(reflect.ValueOf(newVal))
return nil return nil

View file

@ -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 // Perform all Find queries and assign result objects to slice for comparison
for i := 0; i < len(j); i++ { for i := 0; i < len(j); i++ {
j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}}) j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}})

View file

@ -94,7 +94,7 @@ func Test{{$tableNamePlural}}Count(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t) {{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 0, 3) o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o); err != nil { if err = boil.RandomizeSlice(&o); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err) t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
} }