Finish insert tests

* Add columnTypes helper
This commit is contained in:
Patrick O'brien 2016-07-15 22:26:45 +10:00
parent 05aa289167
commit 7e17008f8d
3 changed files with 50 additions and 1 deletions

View file

@ -28,6 +28,16 @@ func ColumnNames(cols []Column) []string {
return names
}
// ColumnTypes of the columns.
func ColumnTypes(cols []Column) []string {
types := make([]string, len(cols))
for i, c := range cols {
types[i] = c.Type
}
return types
}
// ColumnDBTypes of the columns.
func ColumnDBTypes(cols []Column) map[string]string {
types := map[string]string{}

View file

@ -150,6 +150,7 @@ var templateFunctions = template.FuncMap{
"sqlColDefinitions": bdb.SQLColDefinitions,
"sqlColDefStrings": bdb.SQLColDefStrings,
"columnNames": bdb.ColumnNames,
"columnTypes": bdb.ColumnTypes,
"columnDBTypes": bdb.ColumnDBTypes,
"toManyRelationships": bdb.ToManyRelationships,
"defaultValues": bdb.DefaultValues,

View file

@ -4,5 +4,43 @@
{{- $varNamePlural := .Table.Name | plural | camelCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
func Test{{$tableNamePlural}}Select(t *testing.T) {
t.Skip("Test select not implemented")
// Only run this test if there are ample cols to test on
if len({{$varNameSingular}}AutoIncrementColumns) == 0 {
return
}
var err error
x := &struct{
{{- $colNames := .Table.Columns | filterColumnsByAutoIncrement true | columnNames }}
{{ $colTypes := .Table.Columns | filterColumnsByAutoIncrement true | columnTypes }}
{{range $index, $element := $colNames}}
{{$element | titleCase}} {{index $colTypes $index}}
{{end}}
}{}
item := {{$tableNameSingular}}{}
blacklistCols := boil.SetMerge({{$varNameSingular}}AutoIncrementColumns, {{$varNameSingular}}PrimaryKeyColumns)
if err = boil.RandomizeStruct(&item, {{$varNameSingular}}DBTypes, false, blacklistCols...); err != nil {
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
}
if err = item.Insert(); err != nil {
t.Errorf("Unable to insert item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err)
}
err = {{$tableNamePlural}}(qm.Select({{$varNameSingular}}AutoIncrementColumns...), qm.Where(`{{whereClause .Table.PKey.Columns 1}}`, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "item." | join ", "}})).Bind(x)
if err != nil {
t.Errorf("Unable to select insert results with bind: %s", err)
}
{{range $index, $element := $colNames }}
{{$e := titleCase $element}}
if item.{{$e}} != x.{{$e}} || x.{{$e}} == {{index $colTypes $index}}(0) {
t.Errorf("Expected item.{{$e}} to match x.{{$e}}, but got: %v, %v", item.{{$e}}, x.{{$e}})
}
{{end}}
{{$varNamePlural}}DeleteAllRows(t)
}