Added finisher template tests

* Added randDate function
This commit is contained in:
Patrick O'brien 2016-06-11 00:22:08 +10:00
parent 8cb58511a0
commit e99bb09856
6 changed files with 131 additions and 6 deletions

View file

@ -285,6 +285,24 @@ func RandomizeStruct(str interface{}, blacklist ...string) error {
return nil
}
// randDate generates a random time.Time between 1850 and 2050.
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
// not cause mismatches in the test data comparisons.
func randDate() time.Time {
t := time.Date(
1850+rand.Intn(200),
time.Month(1+rand.Intn(12)),
1+rand.Intn(25),
0,
0,
0,
0,
time.UTC,
)
return t
}
func randomizeField(field reflect.Value) error {
kind := field.Kind()
typ := field.Type()
@ -298,11 +316,9 @@ func randomizeField(field reflect.Value) error {
case typeNullString:
newVal = null.NewString(randStr(5+rand.Intn(25)), rand.Intn(2) == 1)
case typeNullTime:
randTime := rand.Int63n(int64(time.Hour) * 24 * 365 * 65)
newVal = null.NewTime(time.Unix(0, 0).Add(time.Duration(randTime)), rand.Intn(2) == 1)
newVal = null.NewTime(randDate(), rand.Intn(2) == 1)
case typeTime:
randTime := rand.Int63n(int64(time.Hour) * 24 * 365 * 65)
newVal = time.Now().Add(time.Duration(randTime))
newVal = randDate()
case typeNullFloat32:
newVal = null.NewFloat32(rand.Float32(), rand.Intn(2) == 1)
case typeNullFloat64:

View file

@ -3,7 +3,7 @@
type {{$varNameSingular}}Slice []*{{$tableNameSingular}}
func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
var o *{{$tableNameSingular}}
o := &{{$tableNameSingular}}{}
boil.SetLimit(q.Query, 1)

View file

@ -3,7 +3,7 @@
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
func Test{{$tableNamePlural}}All(t *testing.T) {
func Test{{$tableNamePlural}}(t *testing.T) {
var err error
// Start from a clean slate

View file

@ -0,0 +1,109 @@
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
{{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
func Test{{$tableNamePlural}}Bind(t *testing.T) {
}
func Test{{$tableNamePlural}}One(t *testing.T) {
var err error
{{$varNamePlural}}DeleteAllRows(t)
o := {{$tableNameSingular}}{}
if err = boil.RandomizeStruct(&o); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = o.Insert(); err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o, err)
}
j, err := {{$tableNamePlural}}().One()
if err != nil {
t.Errorf("Unable to fetch One {{$tableNameSingular}} result:\n#%v\nErr: %s", j, err)
}
{{range $key, $value := .Table.Columns}}
{{if eq $value.Type "null.Time"}}
if o.{{titleCase $value.Name}}.Time.Format("02/01/2006") != j.{{titleCase $value.Name}}.Time.Format("02/01/2006") {
t.Errorf("Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Time.Format("02/01/2006"), j.{{titleCase $value.Name}}.Time.Format("02/01/2006"))
}
{{else if eq $value.Type "time.Time"}}
if o.{{titleCase $value.Name}}.Format("02/01/2006") != j.{{titleCase $value.Name}}.Format("02/01/2006") {
t.Errorf("Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}.Format("02/01/2006"), j.{{titleCase $value.Name}}.Format("02/01/2006"))
}
{{else}}
if j.{{titleCase $value.Name}} != o.{{titleCase $value.Name}} {
t.Errorf("Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}})
}
{{end}}
{{end}}
}
func Test{{$tableNamePlural}}All(t *testing.T) {
var err error
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 3)
if err = boil.RandomizeSlice(&o); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(o); i++ {
err = o[i].Insert()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
j, err := {{$tableNamePlural}}().All()
if err != nil {
t.Errorf("Unable to fetch All {{$tableNameSingular}} results: %s", err)
}
if len(j) != 3 {
t.Errorf("Expected 3 results, got %d", len(j))
}
for i := 0; i < len(o); i++ {
{{range $key, $value := .Table.Columns}}
{{if eq $value.Type "null.Time"}}
if o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006") {
t.Errorf("%d) Expected NullTime {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Time.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Time.Format("02/01/2006"))
}
{{else if eq $value.Type "time.Time"}}
if o[i].{{titleCase $value.Name}}.Format("02/01/2006") != j[i].{{titleCase $value.Name}}.Format("02/01/2006") {
t.Errorf("%d) Expected Time {{$value.Name}} column string values to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}.Format("02/01/2006"), j[i].{{titleCase $value.Name}}.Format("02/01/2006"))
}
{{else}}
if j[i].{{titleCase $value.Name}} != o[i].{{titleCase $value.Name}} {
t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", i, o[i].{{titleCase $value.Name}}, j[i].{{titleCase $value.Name}})
}
{{end}}
{{end}}
}
}
func Test{{$tableNamePlural}}Count(t *testing.T) {
var err error
{{$varNamePlural}}DeleteAllRows(t)
o := make({{$varNameSingular}}Slice, 0, 3)
if err = boil.RandomizeSlice(&o); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} slice: %s", err)
}
// insert random columns to test DeleteAll
for i := 0; i < len(o); i++ {
err = o[i].Insert()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
}