Add singles imports to TestTemplates

* Add byte slice comparison helper to test templates
This commit is contained in:
Patrick O'brien 2016-06-12 16:41:04 +10:00
parent 98e0b1f2ad
commit 3f289848ce
5 changed files with 30 additions and 4 deletions

View file

@ -104,6 +104,7 @@ var sqlBoilerSinglesTestImports = map[string]imports{
`"os"`,
`"strconv"`,
`"math/rand"`,
`"bytes"`,
},
thirdparty: importList{},
},

View file

@ -122,6 +122,15 @@ func TestTemplates(t *testing.T) {
t.Fatalf("Unable to initialize templates: %s", err)
}
cmdData.SingleTestTemplates, err = loadTemplates("templates_test/singles")
if err != nil {
t.Fatalf("Unable to initialize single test templates: %s", err)
}
if len(cmdData.SingleTestTemplates) == 0 {
t.Errorf("SingleTestTemplates is empty.")
}
cmdData.OutFolder, err = ioutil.TempDir("", "templates")
if err != nil {
t.Fatalf("Unable to create tempdir: %s", err)

View file

@ -35,6 +35,11 @@ func Test{{$tableNamePlural}}One(t *testing.T) {
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 eq $value.Type "[]byte"}}
if !byteSliceEqual(o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) {
t.Errorf("%d) Expected {{$value.Name}} columns to match, got:\nStruct: %#v\nResponse: %#v\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}})
}
{{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}})
@ -80,6 +85,11 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
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 eq $value.Type "[]byte"}}
if !byteSliceEqual(o[i].{{titleCase $value.Name}}, j[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}})
}
{{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}})

View file

@ -18,7 +18,7 @@ func Test{{$tableNamePlural}}InPrimaryKeyArgs(t *testing.T) {
t.Errorf("Expected args to be len %d, but got %d", len({{$varNameSingular}}PrimaryKeyColumns), len(args))
}
{{range $key, $value := .Table.PKey.Columns -}}
{{range $key, $value := .Table.PKey.Columns}}
if o.{{titleCase $value}} != args[{{$key}}] {
t.Errorf("Expected args[{{$key}}] to be value of o.{{titleCase $value}}, but got %#v", args[{{$key}}])
}
@ -40,10 +40,10 @@ func Test{{$tableNamePlural}}SliceInPrimaryKeyArgs(t *testing.T) {
}
for i := 0; i < len({{$varNameSingular}}PrimaryKeyColumns) * 3; i++ {
{{range $key, $value := .Table.PKey.Columns -}}
{{range $key, $value := .Table.PKey.Columns}}
if o[i].{{titleCase $value}} != args[i] {
t.Errorf("Expected args[%d] to be value of o.{{titleCase $value}}, but got %#v", i, args[i])
}
{{- end}}
}
{{- end}}
}

View file

@ -17,7 +17,6 @@ func initDBNameRand(input string) {
sumTmp = sumInt[i+1:]
continue
}
break
}
@ -51,3 +50,10 @@ func getDBNameHash(input string) string {
initDBNameRand(input)
return randStr(40)
}
// byteSliceEqual calls bytes.Equal to check that two
// byte slices are equal. bytes.Equal is not used directly
// to avoid an unecessary conditional type import.
func byteSliceEqual(a []byte, b []byte) bool {
return bytes.Equal(a, b)
}