From 3f289848ce55eedb414960365beebe252a4b8b78 Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Sun, 12 Jun 2016 16:41:04 +1000 Subject: [PATCH] Add singles imports to TestTemplates * Add byte slice comparison helper to test templates --- cmds/config.go | 1 + cmds/sqlboiler_test.go | 9 +++++++++ cmds/templates_test/finishers.tpl | 10 ++++++++++ cmds/templates_test/helpers.tpl | 6 +++--- cmds/templates_test/singles/helper_funcs.tpl | 8 +++++++- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmds/config.go b/cmds/config.go index 3d7fb91..f95aff0 100644 --- a/cmds/config.go +++ b/cmds/config.go @@ -104,6 +104,7 @@ var sqlBoilerSinglesTestImports = map[string]imports{ `"os"`, `"strconv"`, `"math/rand"`, + `"bytes"`, }, thirdparty: importList{}, }, diff --git a/cmds/sqlboiler_test.go b/cmds/sqlboiler_test.go index 8f42c44..a9b9db2 100644 --- a/cmds/sqlboiler_test.go +++ b/cmds/sqlboiler_test.go @@ -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) diff --git a/cmds/templates_test/finishers.tpl b/cmds/templates_test/finishers.tpl index 036d8ef..0261da7 100644 --- a/cmds/templates_test/finishers.tpl +++ b/cmds/templates_test/finishers.tpl @@ -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}}) diff --git a/cmds/templates_test/helpers.tpl b/cmds/templates_test/helpers.tpl index 9b36b9b..27d9bc9 100644 --- a/cmds/templates_test/helpers.tpl +++ b/cmds/templates_test/helpers.tpl @@ -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}} } diff --git a/cmds/templates_test/singles/helper_funcs.tpl b/cmds/templates_test/singles/helper_funcs.tpl index e879913..410d7db 100644 --- a/cmds/templates_test/singles/helper_funcs.tpl +++ b/cmds/templates_test/singles/helper_funcs.tpl @@ -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) +}