Finish Bind test, renamed querymods package

* Rename querymods qs package to qm
* Refactor comparing values in template tests to remove redundancy
This commit is contained in:
Patrick O'brien 2016-06-15 00:01:28 +10:00
parent e49df72e84
commit 1508d85cb6
12 changed files with 72 additions and 80 deletions

View file

@ -1,4 +1,4 @@
package qs
package qm
import "github.com/nullbio/sqlboiler/boil"

View file

@ -0,0 +1 @@
package qm

View file

@ -1 +0,0 @@
package qs

View file

@ -149,7 +149,7 @@ var defaultTemplateImports = imports{
},
thirdParty: importList{
`"github.com/nullbio/sqlboiler/boil"`,
`"github.com/nullbio/sqlboiler/boil/qs"`,
`"github.com/nullbio/sqlboiler/boil/qm"`,
},
}
@ -158,7 +158,7 @@ var defaultSingletonTemplateImports = map[string]imports{
standard: importList{},
thirdParty: importList{
`"github.com/nullbio/sqlboiler/boil"`,
`"github.com/nullbio/sqlboiler/boil/qs"`,
`"github.com/nullbio/sqlboiler/boil/qm"`,
},
},
}
@ -170,6 +170,7 @@ var defaultTestTemplateImports = imports{
},
thirdParty: importList{
`"github.com/nullbio/sqlboiler/boil"`,
`"github.com/nullbio/sqlboiler/boil/qm"`,
},
}

View file

@ -275,6 +275,11 @@ func GenerateParamFlags(colCount int, startAt int) string {
func WherePrimaryKey(pkeyCols []string, start int) string {
var output string
// 0 is not a valid start number
if start == 0 {
start = 1
}
cols := make([]string, len(pkeyCols))
copy(cols, pkeyCols)

View file

@ -6,11 +6,11 @@ type {{$varNameSingular}}Query struct {
}
// {{$tableNamePlural}}All retrieves all records.
func {{$tableNamePlural}}(mods ...qs.QueryMod) {{$varNameSingular}}Query {
func {{$tableNamePlural}}(mods ...qm.QueryMod) {{$varNameSingular}}Query {
return {{$tableNamePlural}}X(boil.GetDB(), mods...)
}
func {{$tableNamePlural}}X(exec boil.Executor, mods ...qs.QueryMod) {{$varNameSingular}}Query {
mods = append(mods, qs.Table("{{.Table.Name}}"))
func {{$tableNamePlural}}X(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query {
mods = append(mods, qm.Table("{{.Table.Name}}"))
return {{$varNameSingular}}Query{NewQueryX(exec, mods...)}
}

View file

@ -16,11 +16,11 @@ func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} provided for deletion")
}
var mods []qs.QueryMod
var mods []qm.QueryMod
mods = append(mods,
qs.Table("{{.Table.Name}}"),
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{paramsPrimaryKey "o." .Table.PKey.Columns true}}),
qm.Table("{{.Table.Name}}"),
qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{paramsPrimaryKey "o." .Table.PKey.Columns true}}),
)
query := NewQueryX(exec, mods...)
@ -61,14 +61,14 @@ func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error {
return errors.New("{{.PkgName}}: no {{$tableNameSingular}} slice provided for delete all")
}
var mods []qs.QueryMod
var mods []qm.QueryMod
args := o.inPrimaryKeyArgs()
in := boil.WherePrimaryKeyIn(len(o), {{commaList .Table.PKey.Columns}})
mods = append(mods,
qs.Table("{{.Table.Name}}"),
qs.Where(in, args...),
qm.Table("{{.Table.Name}}"),
qm.Where(in, args...),
)
query := NewQueryX(exec, mods...)

View file

@ -9,11 +9,11 @@ func {{$tableNameSingular}}Find({{primaryKeyFuncSig .Table.Columns .Table.PKey.C
func {{$tableNameSingular}}FindX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, selectCols ...string) (*{{$tableNameSingular}}, error) {
{{$varNameSingular}} := &{{$tableNameSingular}}{}
mods := []qs.QueryMod{
qs.Select(selectCols...),
qs.Table("{{.Table.Name}}"),
qs.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList "" .Table.PKey.Columns}}),
mods := []qm.QueryMod{
qm.Select(selectCols...),
qm.Table("{{.Table.Name}}"),
qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{camelCaseCommaList "" .Table.PKey.Columns}}),
}
q := NewQueryX(exec, mods...)

View file

@ -2,15 +2,15 @@
type M map[string]interface{}
// NewQuery initializes a new Query using the passed in QueryMods
func NewQuery(mods ...qs.QueryMod) *boil.Query {
func NewQuery(mods ...qm.QueryMod) *boil.Query {
return NewQueryX(boil.GetDB(), mods...)
}
// NewQueryX initializes a new Query using the passed in QueryMods
func NewQueryX(exec boil.Executor, mods ...qs.QueryMod) *boil.Query {
func NewQueryX(exec boil.Executor, mods ...qm.QueryMod) *boil.Query {
q := &boil.Query{}
boil.SetExecutor(q, exec)
qs.Apply(q, mods...)
qm.Apply(q, mods...)
return q
}

View file

@ -3,6 +3,28 @@
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
func {{$varNameSingular}}CompareVals(o *{{$tableNameSingular}}, j *{{$tableNameSingular}}, t *testing.T) {
{{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 eq $value.Type "[]byte"}}
if !byteSliceEqual(o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}}) {
t.Errorf("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\n\n", o.{{titleCase $value.Name}}, j.{{titleCase $value.Name}})
}
{{end}}
{{end}}
}
func Test{{$tableNamePlural}}(t *testing.T) {
var err error

View file

@ -23,26 +23,7 @@ func Test{{$tableNamePlural}}Find(t *testing.T) {
// Perform all Find queries and assign result objects to slice for comparison
for i := 0; i < len(j); i++ {
j[i], err = {{$tableNameSingular}}Find({{titleCaseCommaList "o[i]." .Table.PKey.Columns}})
{{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 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}})
}
{{end}}
{{end}}
{{$varNameSingular}}CompareVals(o[i], j[i], t)
}
{{if hasPrimaryKey .Table.PKey}}

View file

@ -4,7 +4,27 @@
{{- $varNamePlural := camelCasePlural .Table.Name -}}
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
func Test{{$tableNamePlural}}Bind(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 := {{$tableNameSingular}}{}
err = {{$tableNamePlural}}(qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{titleCaseCommaList "o." .Table.PKey.Columns}})).Bind(&j)
if err != nil {
t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err)
}
{{$varNameSingular}}CompareVals(&o, &j, t)
}
func Test{{$tableNamePlural}}One(t *testing.T) {
@ -26,26 +46,7 @@ func Test{{$tableNamePlural}}One(t *testing.T) {
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 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}})
}
{{end}}
{{end}}
{{$varNameSingular}}CompareVals(&o, j, t)
}
func Test{{$tableNamePlural}}All(t *testing.T) {
@ -76,25 +77,7 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
}
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 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}})
}
{{end}}
{{end}}
{{$varNameSingular}}CompareVals(o[i], j[i], t)
}
}