Added main template test file, fixed errors

* Fixed TestTemplates bug (now shows compile errors properly)
* Fixed all compile errors for templates (except test templates)
* Added conditional imports for column types
This commit is contained in:
Patrick O'brien 2016-03-23 13:03:35 +10:00
parent 3b0a5e80a1
commit 48a9ba8d29
13 changed files with 242 additions and 57 deletions

View file

@ -18,6 +18,10 @@ func boilRun(cmd *cobra.Command, args []string) {
// Prepend "struct" command to templateNames slice so it sits at top of sort
commandNames = append([]string{"struct"}, commandNames...)
// Create a testCommandNames with "main" prepended to the front for the test templates
// the main template initializes all of the testing assets
testCommandNames := append([]string{"main"}, commandNames...)
for i := 0; i < len(cmdData.Columns); i++ {
data := tplData{
Table: cmdData.Tables[i],
@ -34,6 +38,7 @@ func boilRun(cmd *cobra.Command, args []string) {
// Loop through and generate every command template (excluding skipTemplates)
for _, command := range commandNames {
imps = combineImports(imps, sqlBoilerCustomImports[command])
imps = combineConditionalTypeImports(imps, sqlBoilerConditionalTypeImports, data.Columns)
out = append(out, generateTemplate(command, &data))
}
@ -51,7 +56,7 @@ func boilRun(cmd *cobra.Command, args []string) {
testImps.thirdparty = sqlBoilerDefaultTestImports.thirdparty
// Loop through and generate every command test template (excluding skipTemplates)
for _, command := range commandNames {
for _, command := range testCommandNames {
testImps = combineImports(testImps, sqlBoilerCustomTestImports[command])
testOut = append(testOut, generateTestTemplate(command, &data))
}
@ -66,8 +71,9 @@ func boilRun(cmd *cobra.Command, args []string) {
func buildCommandList() []string {
// Exclude these commands from the output
skipTemplates := []string{
skipCommands := []string{
"boil",
"struct",
}
var commandNames []string
@ -75,11 +81,9 @@ func buildCommandList() []string {
// Build a list of template names
for _, c := range sqlBoilerCommands {
skip := false
for _, s := range skipTemplates {
for _, s := range skipCommands {
// Skip name if it's in the exclude list.
// Also skip "struct" so that it can be added manually at the beginning
// of the slice. Structs should always go to the top of the file.
if s == c.Name() || c.Name() == "struct" {
if s == c.Name() {
skip = true
break
}

View file

@ -6,32 +6,57 @@ import (
"github.com/spf13/cobra"
)
type importList []string
// imports defines the optional standard imports and
// thirdparty imports (from github for example)
type imports struct {
standard []string
thirdparty []string
standard importList
thirdparty importList
}
// sqlBoilerDefaultImports defines the list of default template imports.
var sqlBoilerDefaultImports = imports{
standard: []string{
standard: importList{
`"errors"`,
`"fmt"`,
},
thirdparty: []string{
thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`,
`"gopkg.in/guregu/null.v3"`,
},
}
// sqlBoilerDefaultTestImports defines the list of default test template imports.
var sqlBoilerDefaultTestImports = imports{
standard: []string{
standard: importList{
`"testing"`,
},
}
// sqlBoilerConditionalTypeImports imports are only included in the template output
// if the database requires one of the following special types. Check ParseTableInfo
// to see the type assignments.
var sqlBoilerConditionalTypeImports = map[string]imports{
"null.Int": imports{
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
},
"null.String": imports{
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
},
"null.Bool": imports{
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
},
"null.Float": imports{
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
},
"null.Time": imports{
thirdparty: importList{`"gopkg.in/guregu/null.v3"`},
},
"time.Time": imports{
standard: importList{`"time"`},
},
}
var sqlBoilerCustomImports map[string]imports
var sqlBoilerCustomTestImports map[string]imports

View file

@ -5,19 +5,21 @@ import (
"fmt"
"sort"
"strings"
"github.com/pobri19/sqlboiler/dbdrivers"
)
type ImportSorter []string
func (i ImportSorter) Len() int {
func (i importList) Len() int {
return len(i)
}
func (i ImportSorter) Swap(k, j int) {
func (i importList) Swap(k, j int) {
i[k], i[j] = i[j], i[k]
}
func (i ImportSorter) Less(k, j int) bool {
func (i importList) Less(k, j int) bool {
res := strings.Compare(strings.TrimLeft(i[k], "_ "), strings.TrimLeft(i[j], "_ "))
if res <= 0 {
return true
@ -32,12 +34,39 @@ func combineImports(a, b imports) imports {
c.standard = removeDuplicates(combineStringSlices(a.standard, b.standard))
c.thirdparty = removeDuplicates(combineStringSlices(a.thirdparty, b.thirdparty))
sort.Sort(ImportSorter(c.standard))
sort.Sort(ImportSorter(c.thirdparty))
sort.Sort(c.standard)
sort.Sort(c.thirdparty)
return c
}
func combineConditionalTypeImports(a imports, b map[string]imports, columns []dbdrivers.DBColumn) imports {
tmpImp := imports{
standard: make(importList, len(a.standard)),
thirdparty: make(importList, len(a.thirdparty)),
}
copy(tmpImp.standard, a.standard)
copy(tmpImp.thirdparty, a.thirdparty)
for _, col := range columns {
for key, imp := range b {
if col.Type == key {
tmpImp.standard = append(tmpImp.standard, imp.standard...)
tmpImp.thirdparty = append(tmpImp.thirdparty, imp.thirdparty...)
}
}
}
tmpImp.standard = removeDuplicates(tmpImp.standard)
tmpImp.thirdparty = removeDuplicates(tmpImp.thirdparty)
sort.Sort(tmpImp.standard)
sort.Sort(tmpImp.thirdparty)
return tmpImp
}
func buildImportString(imps *imports) []byte {
stdlen, thirdlen := len(imps.standard), len(imps.thirdparty)
if stdlen+thirdlen < 1 {

71
cmds/imports_test.go Normal file
View file

@ -0,0 +1,71 @@
package cmds
import (
"reflect"
"testing"
"github.com/pobri19/sqlboiler/dbdrivers"
)
func TestCombineConditionalTypeImports(t *testing.T) {
imports1 := imports{
standard: importList{
`"errors"`,
`"fmt"`,
},
thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`,
},
}
importsExpected := imports{
standard: importList{
`"errors"`,
`"fmt"`,
`"time"`,
},
thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`,
`"gopkg.in/guregu/null.v3"`,
},
}
cols := []dbdrivers.DBColumn{
dbdrivers.DBColumn{
Type: "null.Time",
},
dbdrivers.DBColumn{
Type: "null.Time",
},
dbdrivers.DBColumn{
Type: "time.Time",
},
dbdrivers.DBColumn{
Type: "null.Float",
},
}
res1 := combineConditionalTypeImports(imports1, sqlBoilerConditionalTypeImports, cols)
if !reflect.DeepEqual(res1, importsExpected) {
t.Errorf("Expected res1 to match importsExpected, got:\n\n%#v\n", res1)
}
imports2 := imports{
standard: importList{
`"errors"`,
`"fmt"`,
`"time"`,
},
thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`,
`"gopkg.in/guregu/null.v3"`,
},
}
res2 := combineConditionalTypeImports(imports2, sqlBoilerConditionalTypeImports, cols)
if !reflect.DeepEqual(res2, importsExpected) {
t.Errorf("Expected res2 to match importsExpected, got:\n\n%#v\n", res1)
}
}

View file

@ -61,6 +61,8 @@ func templater(cmd *cobra.Command, data *tplData) {
out := [][]byte{generateTemplate(cmd.Name(), data)}
imps := combineImports(sqlBoilerDefaultImports, sqlBoilerCustomImports[cmd.Name()])
imps = combineConditionalTypeImports(imps, sqlBoilerConditionalTypeImports, data.Columns)
err := outHandler(cmdData.OutFolder, out, data, &imps, false)
if err != nil {
errorQuit(fmt.Errorf("Unable to generate the template for command %s: %s", cmd.Name(), err))

View file

@ -70,7 +70,7 @@ func TestOutHandlerFiles(t *testing.T) {
}
a1 := imports{
standard: []string{
standard: importList{
`"fmt"`,
},
}
@ -98,11 +98,11 @@ func TestOutHandlerFiles(t *testing.T) {
}
a3 := imports{
standard: []string{
standard: importList{
`"fmt"`,
`"errors"`,
},
thirdparty: []string{
thirdparty: importList{
`_ "github.com/lib/pq"`,
`_ "github.com/gorilla/n"`,
`"github.com/gorilla/mux"`,
@ -111,8 +111,8 @@ func TestOutHandlerFiles(t *testing.T) {
}
file = &bytes.Buffer{}
sort.Sort(ImportSorter(a3.standard))
sort.Sort(ImportSorter(a3.thirdparty))
sort.Sort(a3.standard)
sort.Sort(a3.thirdparty)
if err := outHandler("folder", templateOutputs, &data, &a3, false); err != nil {
t.Error(err)
@ -142,44 +142,44 @@ patrick's dreams
func TestSortImports(t *testing.T) {
t.Parallel()
a1 := []string{
a1 := importList{
`"fmt"`,
`"errors"`,
}
a2 := []string{
a2 := importList{
`_ "github.com/lib/pq"`,
`_ "github.com/gorilla/n"`,
`"github.com/gorilla/mux"`,
`"github.com/gorilla/websocket"`,
}
a1Expected := []string{"errors", "fmt"}
a2Expected := []string{
a1Expected := importList{`"errors"`, `"fmt"`}
a2Expected := importList{
`"github.com/gorilla/mux"`,
`_ "github.com/gorilla/n"`,
`"github.com/gorilla/websocket"`,
`_ "github.com/lib/pq"`,
}
sort.Sort(ImportSorter(a1))
sort.Sort(a1)
if !reflect.DeepEqual(a1, a1Expected) {
fmt.Errorf("Expected a1 to match a1Expected, got: %v", a1)
t.Errorf("Expected a1 to match a1Expected, got: %v", a1)
}
for i, v := range a1 {
if v != a1Expected[i] {
fmt.Errorf("Expected a1[%d] to match a1Expected[%d]:\n%s\n%s\n", i, i, v, a1Expected[i])
t.Errorf("Expected a1[%d] to match a1Expected[%d]:\n%s\n%s\n", i, i, v, a1Expected[i])
}
}
sort.Sort(ImportSorter(a2))
sort.Sort(a2)
if !reflect.DeepEqual(a2, a2Expected) {
fmt.Errorf("Expected a2 to match a2expected, got: %v", a2)
t.Errorf("Expected a2 to match a2expected, got: %v", a2)
}
for i, v := range a2 {
if v != a2Expected[i] {
fmt.Errorf("Expected a2[%d] to match a2Expected[%d]:\n%s\n%s\n", i, i, v, a1Expected[i])
t.Errorf("Expected a2[%d] to match a2Expected[%d]:\n%s\n%s\n", i, i, v, a1Expected[i])
}
}
}
@ -188,12 +188,12 @@ func TestCombineImports(t *testing.T) {
t.Parallel()
a := imports{
standard: []string{"fmt"},
thirdparty: []string{"github.com/pobri19/sqlboiler", "gopkg.in/guregu/null.v3"},
standard: importList{"fmt"},
thirdparty: importList{"github.com/pobri19/sqlboiler", "gopkg.in/guregu/null.v3"},
}
b := imports{
standard: []string{"os"},
thirdparty: []string{"github.com/pobri19/sqlboiler"},
standard: importList{"os"},
thirdparty: importList{"github.com/pobri19/sqlboiler"},
}
c := combineImports(a, b)

View file

@ -94,10 +94,12 @@ func sqlBoilerPreRun(cmd *cobra.Command, args []string) {
errorQuit(fmt.Errorf("Unable to initialize templates: %s", err))
}
// Initialize the test templates
testTemplates, err = initTemplates(templatesTestDirectory)
if err != nil {
errorQuit(fmt.Errorf("Unable to initialize test templates: %s", err))
// Initialize the test templates if the OutFolder
if cmdData.OutFolder != "" && cfg.TestPostgres != nil {
testTemplates, err = initTemplates(templatesTestDirectory)
if err != nil {
errorQuit(fmt.Errorf("Unable to initialize test templates: %s", err))
}
}
}

View file

@ -61,12 +61,15 @@ func TestTemplates(t *testing.T) {
}
defer tplTestHandle.Close()
fmt.Fprintf(tplTestHandle, "package %s", cmdData.PkgName)
fmt.Fprintf(tplTestHandle, "package %s\n", cmdData.PkgName)
buf := bytes.Buffer{}
cmd := exec.Command("go", "test", tplFile)
buf2 := bytes.Buffer{}
cmd := exec.Command("go", "test")
cmd.Dir = cmdData.OutFolder
cmd.Stderr = &buf
cmd.Stdout = &buf2
if err = cmd.Run(); err != nil {
t.Errorf("go test cmd execution failed: %s\n\n%s", err, buf.String())

View file

@ -19,7 +19,7 @@ func {{$tableNamePlural}}All(db boil.DB) ([]*{{$tableNameSingular}}, error) {
return nil, fmt.Errorf("{{.PkgName}}: failed to scan row: %v", err)
}
{{$varNamePlural}} = append({{$varNamePlural}}, {{$varNamePlural}}Tmp)
{{$varNamePlural}} = append({{$varNamePlural}}, &{{$varNamePlural}}Tmp)
}
if err := rows.Err(); err != nil {

View file

@ -1,13 +1,13 @@
{{- $tableNameSingular := titleCaseSingular .Table -}}
// {{$tableNameSingular}}Delete deletes a single record.
func {{$tableNameSingular}}Delete(db boil.DB, id int) error {
if id == nil {
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table}} delete")
if id == 0 {
return errors.New("{{.PkgName}}: no id provided for {{.Table}} delete")
}
err := db.Exec("DELETE FROM {{.Table}} WHERE id=$1", id)
_, err := db.Exec("DELETE FROM {{.Table}} WHERE id=$1", id)
if err != nil {
return errors.New("{{.PkgName}}: unable to delete from {{.Table}}: %s", err)
return fmt.Errorf("{{.PkgName}}: unable to delete from {{.Table}}: %s", err)
}
return nil
@ -16,11 +16,11 @@ func {{$tableNameSingular}}Delete(db boil.DB, id int) error {
{{if hasPrimaryKey .Columns -}}
// Delete deletes a single {{$tableNameSingular}} record.
// Delete will match against the primary key column to find the record to delete.
func (o *{{$tableNameSingular}}) Delete(db *sqlx.DB) error {
func (o *{{$tableNameSingular}}) Delete(db boil.DB) error {
{{- $pkeyName := getPrimaryKey .Columns -}}
err := db.Exec("DELETE FROM {{.Table}} WHERE {{$pkeyName}}=$1", o.{{titleCase $pkeyName}})
_, err := db.Exec("DELETE FROM {{.Table}} WHERE {{$pkeyName}}=$1", o.{{titleCase $pkeyName}})
if err != nil {
return errors.New("{{.PkgName}}: unable to delete from {{.Table}}: %s", err)
return fmt.Errorf("{{.PkgName}}: unable to delete from {{.Table}}: %s", err)
}
return nil

View file

@ -4,7 +4,7 @@
// For example: friendName string `db:"friend_name"`
func {{$tableNameSingular}}FindSelect(db boil.DB, id int, results interface{}) error {
if id == 0 {
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table}} select")
return errors.New("{{.PkgName}}: no id provided for {{.Table}} select")
}
query := fmt.Sprintf(`SELECT %s FROM {{.Table}} WHERE id=$1`, boil.SelectNames(results))

View file

@ -2,14 +2,14 @@
// {{$tableNameSingular}}Update updates a single record.
func {{$tableNameSingular}}Update(db boil.DB, id int, columns map[string]interface{}) error {
if id == 0 {
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table}} update")
return errors.New("{{.PkgName}}: no id provided for {{.Table}} update")
}
query := fmt.Sprintf(`UPDATE {{.Table}} SET %s WHERE id=$%d`, boil.Update(columns), len(columns))
err := db.Exec(query, id, boil.WhereParams(columns))
_, err := db.Exec(query, id, boil.WhereParams(columns))
if err != nil {
return errors.New("{{.PkgName}}: unable to update row with ID %d in {{.Table}}: %s", id, err)
return fmt.Errorf("{{.PkgName}}: unable to update row with ID %d in {{.Table}}: %s", id, err)
}
return nil
@ -20,11 +20,11 @@ func {{$tableNameSingular}}Update(db boil.DB, id int, columns map[string]interfa
// Update will match against the primary key column to find the record to update.
// WARNING: This Update method will NOT ignore nil members.
// If you pass in nil members, those columnns will be set to null.
func (o *{{$tableNameSingular}}) Update(db *sqlx.DB) error {
func (o *{{$tableNameSingular}}) Update(db boil.DB) error {
{{- $pkeyName := getPrimaryKey .Columns -}}
err := db.Exec("UPDATE {{.Table}} SET {{updateParamNames .Columns}} WHERE {{$pkeyName}}=${{len .Columns}}", {{updateParamVariables "o." .Columns}}, o.{{titleCase $pkeyName}})
_, err := db.Exec("UPDATE {{.Table}} SET {{updateParamNames .Columns}} WHERE {{$pkeyName}}=${{len .Columns}}", {{updateParamVariables "o." .Columns}}, o.{{titleCase $pkeyName}})
if err != nil {
return errors.New("{{.PkgName}}: unable to update {{.Table}} row using primary key {{$pkeyName}}: %s", err)
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table}} row using primary key {{$pkeyName}}: %s", err)
}
return nil

View file

@ -0,0 +1,49 @@
type PostgresCfg struct {
User string `toml:"user"`
Pass string `toml:"pass"`
Host string `toml:"host"`
Port int `toml:"port"`
DBName string `toml:"dbname"`
}
type Config struct {
Postgres PostgresCfg `toml:"postgres"`
TestPostgres *PostgresCfg `toml:"postgres_test"`
}
var cfg *Config
func LoadConfigFile(filename string) {
_, err := toml.DecodeFile(filename, &cfg)
if os.IsNotExist(err) {
fmt.Fatalf("Failed to find the toml configuration file %s: %s", filename, err)
}
if err != nil {
fmt.Fatalf("Failed to decode toml configuration file:", err)
}
if cfg.TestPostgres != nil {
if cfg.TestPostgres.User == "" || cfg.TestPostgres.Pass == "" ||
cfg.TestPostgres.Host == "" || cfg.TestPostgres.Port == 0 ||
cfg.TestPostgres.DBName == "" || cfg.Postgres.DBName == cfg.TestPostgres.DBName {
cfg.TestPostgres = nil
}
}
if cfg.TestPostgres == nil {
fmt.Fatalf("Failed to load config.toml postgres_test config")
}
}
func TestMain(m *testing.M) {
setup()
code := m.Run()
// shutdown
os.Exit(code)
}
func setup() {
LoadConfigFile("../config.toml")
}