Refactored and finished insert tests

This commit is contained in:
Patrick O'brien 2016-07-14 15:09:05 +10:00
parent b7f8c132df
commit c424780458
10 changed files with 83 additions and 52 deletions

View file

@ -114,7 +114,7 @@ func IsValueMatch(obj interface{}, columns []string, values []interface{}) []err
// RandomizeSlice takes a pointer to a slice of pointers to objects
// and fills the pointed to objects with random data.
// It will ignore the fields in the blacklist.
func RandomizeSlice(obj interface{}, colTypes map[string]string, blacklist ...string) error {
func RandomizeSlice(obj interface{}, colTypes map[string]string, includeInvalid bool, blacklist ...string) error {
ptrSlice := reflect.ValueOf(obj)
typ := ptrSlice.Type()
ptrSlice = ptrSlice.Elem()
@ -140,7 +140,7 @@ func RandomizeSlice(obj interface{}, colTypes map[string]string, blacklist ...st
for i := 0; i < ptrSlice.Len(); i++ {
o := ptrSlice.Index(i)
o.Set(reflect.New(structTyp))
if err := RandomizeStruct(o.Interface(), colTypes, blacklist...); err != nil {
if err := RandomizeStruct(o.Interface(), colTypes, includeInvalid, blacklist...); err != nil {
return err
}
}
@ -150,7 +150,7 @@ func RandomizeSlice(obj interface{}, colTypes map[string]string, blacklist ...st
// RandomizeStruct takes an object and fills it with random data.
// It will ignore the fields in the blacklist.
func RandomizeStruct(str interface{}, colTypes map[string]string, blacklist ...string) error {
func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid bool, blacklist ...string) error {
// Don't modify blacklist
copyBlacklist := make([]string, len(blacklist))
copy(copyBlacklist, blacklist)
@ -188,7 +188,7 @@ func RandomizeStruct(str interface{}, colTypes map[string]string, blacklist ...s
}
fieldDBType := colTypes[typ.Field(i).Name]
if err := randomizeField(fieldVal, fieldDBType); err != nil {
if err := randomizeField(fieldVal, fieldDBType, includeInvalid); err != nil {
return err
}
}
@ -214,14 +214,19 @@ func randDate(sd int) time.Time {
return t
}
func randomizeField(field reflect.Value, fieldType string) error {
func randomizeField(field reflect.Value, fieldType string, includeInvalid bool) error {
kind := field.Kind()
typ := field.Type()
var newVal interface{}
if kind == reflect.Struct {
b := rand.Intn(2) == 1
var b bool
if includeInvalid {
b = rand.Intn(2) == 1
} else {
b = true
}
switch typ {
case typeNullBool:
if b {
@ -347,11 +352,13 @@ func randomizeField(field reflect.Value, fieldType string) error {
case reflect.Uint64:
newVal = uint64(sd.nextInt())
case reflect.Bool:
if sd.nextInt()%2 == 0 {
newVal = true
var b bool
if includeInvalid {
b = sd.nextInt()%2 == 0
} else {
newVal = false
b = true
}
newVal = b
case reflect.String:
if fieldType == "interval" {
newVal = strconv.Itoa((sd.nextInt()%26)+2) + " days"

View file

@ -155,7 +155,7 @@ func TestRandomizeStruct(t *testing.T) {
"NullInterval": "interval",
}
err := RandomizeStruct(&testStruct, fieldTypes, "Ignore")
err := RandomizeStruct(&testStruct, fieldTypes, true, "Ignore")
if err != nil {
t.Fatal(err)
}

View file

@ -1,17 +1,19 @@
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
var (
{{$varNameSingular}}Columns = []string{{"{"}}{{.Table.Columns | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
{{$varNameSingular}}ColumnsWithoutDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault false | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}}
{{$varNameSingular}}ColumnsWithDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault true | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}}
{{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
{{$varNameSingular}}AutoIncrementColumns = []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement true | columnNames | stringMap .StringFuncs.quoteWrap | join "," }}{{"}"}}
{{$varNameSingular}}AutoIncPrimaryKey = "{{- with autoIncPrimaryKey .Table.Columns .Table.PKey -}}{{.Name}}{{- end -}}"
{{$varNameSingular}}Columns = []string{{"{"}}{{.Table.Columns | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
{{$varNameSingular}}ColumnsWithoutDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault false | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}}
{{$varNameSingular}}ColumnsWithDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault true | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}}
{{$varNameSingular}}ColumnsWithSimpleDefault = []string{{"{"}}{{.Table.Columns | filterColumnsBySimpleDefault | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
{{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
{{$varNameSingular}}AutoIncrementColumns = []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement true | columnNames | stringMap .StringFuncs.quoteWrap | join "," }}{{"}"}}
{{$varNameSingular}}AutoIncPrimaryKey = "{{- with autoIncPrimaryKey .Table.Columns .Table.PKey -}}{{.Name}}{{- end -}}"
)
type (
{{$varNameSingular}}Slice []*{{$tableNameSingular}}
{{$tableNameSingular}}Hook func(*{{$tableNameSingular}}) error
{{$varNameSingular}}Query struct {
*boil.Query
}

View file

@ -11,7 +11,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
}
wl, returnColumns := o.generateInsertColumns(whitelist)
wl, returnColumns := o.generateInsertColumns(whitelist...)
var err error
if err := o.doBeforeCreateHooks(); err != nil {
@ -75,7 +75,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
}
// generateInsertColumns generates the whitelist columns and return columns for an insert statement
func (o *{{$tableNameSingular}}) generateInsertColumns(whitelist []string) ([]string, []string) {
func (o *{{$tableNameSingular}}) generateInsertColumns(whitelist ...string) ([]string, []string) {
var wl []string
wl = append(wl, whitelist...)

View file

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

View file

@ -26,7 +26,7 @@ func Test{{$tableNamePlural}}QueryDeleteAll(t *testing.T) {
}
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
@ -58,7 +58,7 @@ func Test{{$tableNamePlural}}SliceDeleteAll(t *testing.T) {
// insert random columns to test DeleteAll
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
@ -88,7 +88,7 @@ func Test{{$tableNamePlural}}Delete(t *testing.T) {
// insert random columns to test Delete
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}

View file

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

View file

@ -9,7 +9,7 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t)
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
@ -30,7 +30,7 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t)
y := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&y, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&y, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
@ -63,7 +63,7 @@ func Test{{$tableNamePlural}}One(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t)
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
@ -85,7 +85,7 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
@ -117,7 +117,7 @@ func Test{{$tableNamePlural}}Count(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}

View file

@ -32,7 +32,7 @@ func Test{{$tableNamePlural}}InPrimaryKeyArgs(t *testing.T) {
var o {{$tableNameSingular}}
o = {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeStruct(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Could not randomize struct: %s", err)
}
@ -53,7 +53,7 @@ func Test{{$tableNamePlural}}SliceInPrimaryKeyArgs(t *testing.T) {
var err error
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Could not randomize slice: %s", err)
}

View file

@ -19,7 +19,7 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes); err != nil {
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
@ -43,34 +43,33 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err)
}
{{with .Table.Columns | filterColumnsByAutoIncrement true | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}
// Ensure the auto increment columns are returned in the object
if errs = boil.IsZeroValue(item, false, {{.}}); errs != nil {
for _, e := range errs {
t.Errorf("Expected auto-increment columns to be greater than 0, err: %s\n", e)
for _, c := range {{$varNameSingular}}AutoIncrementColumns {
// Ensure the auto increment columns are returned in the object.
if errs = boil.IsZeroValue(item, false, c); errs != nil {
for _, e := range errs {
t.Errorf("Expected auto-increment columns to be greater than 0, err: %s\n", e)
}
}
}
{{end}}
{{with .Table.Columns | filterColumnsBySimpleDefault}}
simpleDefaults := []string{{"{"}}{{. | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}}
defaultValues := []interface{}{{"{"}}{{. | defaultValues | join ", "}}{{"}"}}
defaultValues := []interface{}{{"{"}}{{.Table.Columns | filterColumnsBySimpleDefault | defaultValues | join ", "}}{{"}"}}
if len(simpleDefaults) != len(defaultValues) {
t.Fatalf("Mismatch between slice lengths: %d, %d", len(simpleDefaults), len(defaultValues))
}
// Ensure the simple default column values are returned correctly.
if len({{$varNameSingular}}ColumnsWithSimpleDefault) > 0 && len(defaultValues) > 0 {
if len({{$varNameSingular}}ColumnsWithSimpleDefault) != len(defaultValues) {
t.Fatalf("Mismatch between slice lengths: %d, %d", len({{$varNameSingular}}ColumnsWithSimpleDefault), len(defaultValues))
}
if errs = boil.IsValueMatch(item, simpleDefaults, defaultValues); errs != nil {
for _, e := range errs {
t.Errorf("Expected default value to match column value, err: %s\n", e);
if errs = boil.IsValueMatch(item, {{$varNameSingular}}ColumnsWithSimpleDefault, defaultValues); errs != nil {
for _, e := range errs {
t.Errorf("Expected default value to match column value, err: %s\n", e);
}
}
}
{{end}}
{{with .Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false}}
regularCols := []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}}
// Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values.
regularCols := []string{{"{"}}{{. | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}}
for _, c := range regularCols {
rv := reflect.Indirect(reflect.ValueOf(item))
field := rv.FieldByName(strmangle.TitleCase(c))
@ -82,5 +81,28 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
t.Errorf("Expected column %s to be zero value, got: %v, wanted: %v", c, fv, zv)
}
}
{{end}}
item = &{{$tableNameSingular}}{}
wl, rc := item.generateInsertColumns()
if !reflect.DeepEqual(rc, {{$varNameSingular}}ColumnsWithDefault) {
t.Errorf("Expected return columns to contain all columns with default values:\n\nGot: %v\nWanted: %v", rc, {{$varNameSingular}}ColumnsWithDefault)
}
if !reflect.DeepEqual(wl, {{$varNameSingular}}ColumnsWithoutDefault) {
t.Errorf("Expected whitelist to contain all columns without default values:\n\nGot: %v\nWanted: %v", wl, {{$varNameSingular}}ColumnsWithoutDefault)
}
if err = boil.RandomizeStruct(item, {{$varNameSingular}}DBTypes, false); err != nil {
t.Errorf("Unable to randomize item: %s", err)
}
wl, rc = item.generateInsertColumns()
if len(rc) > 0 {
t.Errorf("Expected return columns to contain no columns:\n\nGot: %v", rc)
}
if !reflect.DeepEqual(wl, {{$varNameSingular}}Columns) {
t.Errorf("Expected whitelist to contain all columns values:\n\nGot: %v\nWanted: %v", wl, {{$varNameSingular}}Columns)
}
}