2016-07-06 08:02:35 +02:00
|
|
|
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
|
|
|
{{- $dbName := singular .Table.Name -}}
|
|
|
|
{{- $tableNamePlural := .Table.Name | plural | titleCase -}}
|
|
|
|
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
|
|
|
|
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
2016-07-09 18:31:09 +02:00
|
|
|
{{- $parent := . -}}
|
2016-07-06 08:02:35 +02:00
|
|
|
func Test{{$tableNamePlural}}Insert(t *testing.T) {
|
|
|
|
var err error
|
2016-07-13 18:51:40 +02:00
|
|
|
|
2016-07-08 18:39:36 +02:00
|
|
|
var errs []error
|
2016-07-13 18:51:40 +02:00
|
|
|
_ = errs
|
|
|
|
|
2016-07-08 18:39:36 +02:00
|
|
|
emptyTime := time.Time{}.String()
|
2016-07-13 18:51:40 +02:00
|
|
|
_ = emptyTime
|
2016-07-06 08:02:35 +02:00
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
nullTime := null.NewTime(time.Time{}, true)
|
|
|
|
_ = nullTime
|
2016-07-14 06:12:26 +02:00
|
|
|
|
2016-07-16 16:15:14 +02:00
|
|
|
o := make({{$tableNameSingular}}Slice, 3)
|
2016-07-14 07:09:05 +02:00
|
|
|
if err = boil.RandomizeSlice(&o, {{$varNameSingular}}DBTypes, true); err != nil {
|
2016-07-06 08:02:35 +02:00
|
|
|
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(o); i++ {
|
|
|
|
if err = o[i].Insert(); err != nil {
|
|
|
|
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 16:15:14 +02:00
|
|
|
j := make({{$tableNameSingular}}Slice, 3)
|
2016-07-06 08:02:35 +02:00
|
|
|
// Perform all Find queries and assign result objects to slice for comparison
|
|
|
|
for i := 0; i < len(j); i++ {
|
|
|
|
j[i], err = {{$tableNameSingular}}Find({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o[i]." | join ", "}})
|
|
|
|
{{$varNameSingular}}CompareVals(o[i], j[i], t)
|
|
|
|
}
|
|
|
|
|
2016-07-07 17:59:07 +02:00
|
|
|
{{$varNamePlural}}DeleteAllRows(t)
|
|
|
|
|
2016-07-06 08:02:35 +02:00
|
|
|
item := &{{$tableNameSingular}}{}
|
|
|
|
if err = item.Insert(); err != nil {
|
|
|
|
t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err)
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
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)
|
|
|
|
}
|
2016-07-08 18:39:36 +02:00
|
|
|
}
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
defaultValues := []interface{}{{"{"}}{{.Table.Columns | filterColumnsBySimpleDefault | defaultValues | join ", "}}{{"}"}}
|
2016-07-08 18:39:36 +02:00
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
// 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))
|
|
|
|
}
|
2016-07-08 18:39:36 +02:00
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-07-08 18:39:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
regularCols := []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}}
|
2016-07-14 06:12:26 +02:00
|
|
|
|
2016-07-14 07:09:05 +02:00
|
|
|
// Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values.
|
2016-07-14 06:12:26 +02:00
|
|
|
for _, c := range regularCols {
|
|
|
|
rv := reflect.Indirect(reflect.ValueOf(item))
|
|
|
|
field := rv.FieldByName(strmangle.TitleCase(c))
|
|
|
|
|
|
|
|
zv := reflect.Zero(field.Type()).Interface()
|
|
|
|
fv := field.Interface()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(zv, fv) {
|
|
|
|
t.Errorf("Expected column %s to be zero value, got: %v, wanted: %v", c, fv, zv)
|
|
|
|
}
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|
2016-07-14 07:09:05 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2016-07-15 07:32:36 +02:00
|
|
|
|
|
|
|
{{$varNamePlural}}DeleteAllRows(t)
|
2016-07-06 08:02:35 +02:00
|
|
|
}
|