Fix a bunch of test errors.
This commit is contained in:
parent
e348767e8a
commit
8aac8dc121
13 changed files with 82 additions and 73 deletions
|
@ -7,6 +7,8 @@ import (
|
|||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/pobri19/sqlboiler/dbdrivers"
|
||||
)
|
||||
|
||||
func TestOutHandler(t *testing.T) {
|
||||
|
@ -19,12 +21,14 @@ func TestOutHandler(t *testing.T) {
|
|||
}()
|
||||
|
||||
data := tplData{
|
||||
Table: "patrick",
|
||||
Table: dbdrivers.Table{
|
||||
Name: "patrick",
|
||||
},
|
||||
}
|
||||
|
||||
templateOutputs := [][]byte{[]byte("hello world"), []byte("patrick's dreams")}
|
||||
|
||||
if err := outHandler("", templateOutputs, &data, &imports{}, false); err != nil {
|
||||
if err := outHandler("", templateOutputs, &data, imports{}, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
|
@ -57,12 +61,12 @@ func TestOutHandlerFiles(t *testing.T) {
|
|||
}
|
||||
|
||||
data := tplData{
|
||||
Table: "patrick",
|
||||
Table: dbdrivers.Table{Name: "patrick"},
|
||||
}
|
||||
|
||||
templateOutputs := [][]byte{[]byte("hello world"), []byte("patrick's dreams")}
|
||||
|
||||
if err := outHandler("folder", templateOutputs, &data, &imports{}, false); err != nil {
|
||||
if err := outHandler("folder", templateOutputs, &data, imports{}, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if out := file.String(); out != "package patrick\n\nhello world\npatrick's dreams\n" {
|
||||
|
@ -76,7 +80,7 @@ func TestOutHandlerFiles(t *testing.T) {
|
|||
}
|
||||
file = &bytes.Buffer{}
|
||||
|
||||
if err := outHandler("folder", templateOutputs, &data, &a1, false); err != nil {
|
||||
if err := outHandler("folder", templateOutputs, &data, a1, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if out := file.String(); out != "package patrick\n\nimport \"fmt\"\nhello world\npatrick's dreams\n" {
|
||||
|
@ -90,7 +94,7 @@ func TestOutHandlerFiles(t *testing.T) {
|
|||
}
|
||||
file = &bytes.Buffer{}
|
||||
|
||||
if err := outHandler("folder", templateOutputs, &data, &a2, false); err != nil {
|
||||
if err := outHandler("folder", templateOutputs, &data, a2, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if out := file.String(); out != "package patrick\n\nimport \"github.com/spf13/cobra\"\nhello world\npatrick's dreams\n" {
|
||||
|
@ -114,7 +118,7 @@ func TestOutHandlerFiles(t *testing.T) {
|
|||
sort.Sort(a3.standard)
|
||||
sort.Sort(a3.thirdparty)
|
||||
|
||||
if err := outHandler("folder", templateOutputs, &data, &a3, false); err != nil {
|
||||
if err := outHandler("folder", templateOutputs, &data, a3, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
|
|
|
@ -13,24 +13,29 @@ import (
|
|||
|
||||
func init() {
|
||||
cmdData = &CmdData{
|
||||
Tables: []string{"patrick_table", "spiderman"},
|
||||
Columns: [][]dbdrivers.Column{
|
||||
[]dbdrivers.Column{
|
||||
{Name: "patrick_column", Type: "string", IsNullable: false},
|
||||
{Name: "aaron_column", Type: "null.String", IsNullable: true},
|
||||
{Name: "id", Type: "null.Int", IsNullable: true},
|
||||
{Name: "fun_id", Type: "int64", IsNullable: false},
|
||||
{Name: "time", Type: "null.Time", IsNullable: true},
|
||||
{Name: "fun_time", Type: "time.Time", IsNullable: false},
|
||||
{Name: "cool_stuff_forever", Type: "[]byte", IsNullable: false},
|
||||
Tables: []dbdrivers.Table{
|
||||
{
|
||||
Name: "patrick_table",
|
||||
Columns: []dbdrivers.Column{
|
||||
{Name: "patrick_column", Type: "string", IsNullable: false},
|
||||
{Name: "aaron_column", Type: "null.String", IsNullable: true},
|
||||
{Name: "id", Type: "null.Int", IsNullable: true},
|
||||
{Name: "fun_id", Type: "int64", IsNullable: false},
|
||||
{Name: "time", Type: "null.Time", IsNullable: true},
|
||||
{Name: "fun_time", Type: "time.Time", IsNullable: false},
|
||||
{Name: "cool_stuff_forever", Type: "[]byte", IsNullable: false},
|
||||
},
|
||||
},
|
||||
[]dbdrivers.Column{
|
||||
{Name: "patrick", Type: "string", IsNullable: false},
|
||||
{
|
||||
Name: "spiderman",
|
||||
Columns: []dbdrivers.Column{
|
||||
{Name: "patrick", Type: "string", IsNullable: false},
|
||||
},
|
||||
},
|
||||
},
|
||||
PkgName: "patrick",
|
||||
OutFolder: "",
|
||||
Interface: nil,
|
||||
Interface: nil,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ func generateTemplate(commandName string, data *tplData) []byte {
|
|||
|
||||
output, err := processTemplate(template, data)
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to process the template %s for table %s: %s", template.Name(), data.Table, err))
|
||||
errorQuit(fmt.Errorf("Unable to process the template %s for table %s: %s", template.Name(), data.Table.Name, err))
|
||||
}
|
||||
|
||||
return output
|
||||
|
@ -37,7 +37,7 @@ func generateTestTemplate(commandName string, data *tplData) []byte {
|
|||
|
||||
output, err := processTemplate(template, data)
|
||||
if err != nil {
|
||||
errorQuit(fmt.Errorf("Unable to process the test template %s for table %s: %s", template.Name(), data.Table, err))
|
||||
errorQuit(fmt.Errorf("Unable to process the test template %s for table %s: %s", template.Name(), data.Table.Name, err))
|
||||
}
|
||||
|
||||
return output
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $dbName := singular .Table -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table -}}
|
||||
{{- $varNamePlural := camelCasePlural .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
||||
{{- $varNamePlural := camelCasePlural .Table.Name -}}
|
||||
// {{$tableNamePlural}}All retrieves all records.
|
||||
func {{$tableNamePlural}}All(db boil.DB) ([]*{{$tableNameSingular}}, error) {
|
||||
var {{$varNamePlural}} []*{{$tableNameSingular}}
|
||||
|
||||
rows, err := db.Query(`SELECT {{selectParamNames $dbName .Columns}} FROM {{.Table}}`)
|
||||
rows, err := db.Query(`SELECT {{selectParamNames $dbName .Table.Columns}} FROM {{.Table.Name}}`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: failed to query: %v", err)
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ func {{$tableNamePlural}}All(db boil.DB) ([]*{{$tableNameSingular}}, error) {
|
|||
{{- $tmpVarName := (print $varNamePlural "Tmp") -}}
|
||||
{{$varNamePlural}}Tmp := {{$tableNameSingular}}{}
|
||||
|
||||
if err := rows.Scan({{scanParamNames $tmpVarName .Columns}}); err != nil {
|
||||
if err := rows.Scan({{scanParamNames $tmpVarName .Table.Columns}}); err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: failed to scan row: %v", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}Delete deletes a single record.
|
||||
func {{$tableNameSingular}}Delete(db boil.DB, id int) error {
|
||||
if id == 0 {
|
||||
return errors.New("{{.PkgName}}: no id provided for {{.Table}} delete")
|
||||
return errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} delete")
|
||||
}
|
||||
|
||||
_, err := db.Exec("DELETE FROM {{.Table}} WHERE id=$1", id)
|
||||
_, err := db.Exec("DELETE FROM {{.Table.Name}} WHERE id=$1", id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to delete from {{.Table}}: %s", err)
|
||||
return fmt.Errorf("{{.PkgName}}: unable to delete from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
{{if hasPrimaryKey .Columns -}}
|
||||
{{if hasPrimaryKey .Table.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 boil.DB) error {
|
||||
{{- $pkeyName := getPrimaryKey .Columns -}}
|
||||
_, err := db.Exec("DELETE FROM {{.Table}} WHERE {{$pkeyName}}=$1", o.{{titleCase $pkeyName}})
|
||||
{{- $pkeyName := getPrimaryKey .Table.Columns -}}
|
||||
_, err := db.Exec("DELETE FROM {{.Table.Name}} WHERE {{$pkeyName}}=$1", o.{{titleCase $pkeyName}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to delete from {{.Table}}: %s", err)
|
||||
return fmt.Errorf("{{.PkgName}}: unable to delete from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $dbName := singular .Table -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $varNameSingular := camelCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}Find retrieves a single record by ID.
|
||||
func {{$tableNameSingular}}Find(db boil.DB, id int) (*{{$tableNameSingular}}, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table}} select")
|
||||
return nil, errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} select")
|
||||
}
|
||||
var {{$varNameSingular}} *{{$tableNameSingular}}
|
||||
err := db.Select(&{{$varNameSingular}}, `SELECT {{selectParamNames $dbName .Columns}} WHERE id=$1`, id)
|
||||
err := db.Select(&{{$varNameSingular}}, `SELECT {{selectParamNames $dbName .Table.Columns}} WHERE id=$1`, id)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table}}: %s", err)
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varNameSingular}}, nil
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}FindSelect retrieves the specified columns for a single record by ID.
|
||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{$tableNameSingular}}FindSelect(db boil.DB, id int, results interface{}) error {
|
||||
if id == 0 {
|
||||
return errors.New("{{.PkgName}}: no id provided for {{.Table}} select")
|
||||
return errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} select")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table}} WHERE id=$1`, boil.SelectNames(results))
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE id=$1`, boil.SelectNames(results))
|
||||
err := db.Select(results, query, id)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table}}: %s", err)
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}Insert inserts a single record.
|
||||
func {{$tableNameSingular}}Insert(db boil.DB, o *{{$tableNameSingular}}) (int, error) {
|
||||
if o == nil {
|
||||
return 0, errors.New("{{.PkgName}}: no {{.Table}} provided for insertion")
|
||||
return 0, errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
|
||||
}
|
||||
|
||||
var rowID int
|
||||
err := db.QueryRow(`INSERT INTO {{.Table}} ({{insertParamNames .Columns}}) VALUES({{insertParamFlags .Columns}}) RETURNING id`, {{insertParamVariables "o." .Columns}}).Scan(&rowID)
|
||||
err := db.QueryRow(`INSERT INTO {{.Table.Name}} ({{insertParamNames .Table.Columns}}) VALUES({{insertParamFlags .Table.Columns}}) RETURNING id`, {{insertParamVariables "o." .Table.Columns}}).Scan(&rowID)
|
||||
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("{{.PkgName}}: unable to insert {{.Table}}: %s", err)
|
||||
return 0, fmt.Errorf("{{.PkgName}}: unable to insert {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return rowID, nil
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
{{- $tableNamePlural := titleCasePlural .Table -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
||||
// {{$tableNamePlural}}Select retrieves the specified columns for all records.
|
||||
// Pass in a pointer to an object with `db` tags that match the column names you wish to retrieve.
|
||||
// For example: friendName string `db:"friend_name"`
|
||||
func {{$tableNamePlural}}Select(db boil.DB, results interface{}) error {
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table}}`, boil.SelectNames(results))
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}}`, boil.SelectNames(results))
|
||||
err := db.Select(results, query)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table}}: %s", err)
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
{{- $tableNamePlural := titleCasePlural .Table -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
||||
// {{$tableNamePlural}}SelectWhere retrieves all records with the specified column values.
|
||||
func {{$tableNamePlural}}SelectWhere(db boil.DB, results interface{}, columns map[string]interface{}) error {
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table}} WHERE %s`, boil.SelectNames(results), boil.Where(columns))
|
||||
query := fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, boil.SelectNames(results), boil.Where(columns))
|
||||
err := db.Select(results, query, boil.WhereParams(columns)...)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table}}: %s", err)
|
||||
return fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $dbName := singular .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
{{- $dbName := singular .Table.Name -}}
|
||||
// {{$tableNameSingular}} is an object representing the database table.
|
||||
type {{$tableNameSingular}} struct {
|
||||
{{range $key, $value := .Columns -}}
|
||||
{{range $key, $value := .Table.Columns -}}
|
||||
{{titleCase $value.Name}} {{$value.Type}} `db:"{{makeDBName $dbName $value.Name}}" json:"{{$value.Name}}"`
|
||||
{{end -}}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
// {{$tableNameSingular}}Update updates a single record.
|
||||
func {{$tableNameSingular}}Update(db boil.DB, id int, columns map[string]interface{}) error {
|
||||
if id == 0 {
|
||||
return errors.New("{{.PkgName}}: no id provided for {{.Table}} update")
|
||||
return errors.New("{{.PkgName}}: no id provided for {{.Table.Name}} update")
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`UPDATE {{.Table}} SET %s WHERE id=$%d`, boil.Update(columns), len(columns))
|
||||
query := fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE id=$%d`, boil.Update(columns), len(columns))
|
||||
|
||||
_, err := db.Exec(query, id, boil.WhereParams(columns))
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.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.Name}}: %s", id, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
{{if hasPrimaryKey .Columns -}}
|
||||
{{if hasPrimaryKey .Table.Columns -}}
|
||||
// Update updates a single {{$tableNameSingular}} record.
|
||||
// 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 boil.DB) error {
|
||||
{{- $pkeyName := getPrimaryKey .Columns -}}
|
||||
_, err := db.Exec("UPDATE {{.Table}} SET {{updateParamNames .Columns}} WHERE {{$pkeyName}}=${{len .Columns}}", {{updateParamVariables "o." .Columns}}, o.{{titleCase $pkeyName}})
|
||||
{{- $pkeyName := getPrimaryKey .Table.Columns -}}
|
||||
_, err := db.Exec("UPDATE {{.Table.Name}} SET {{updateParamNames .Table.Columns}} WHERE {{$pkeyName}}=${{len .Table.Columns}}", {{updateParamVariables "o." .Table.Columns}}, o.{{titleCase $pkeyName}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table}} row using primary key {{$pkeyName}}: %s", err)
|
||||
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}} row using primary key {{$pkeyName}}: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
{{- $tableNameSingular := titleCaseSingular .Table -}}
|
||||
{{- $dbName := singular .Table -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table -}}
|
||||
{{- $varNamePlural := camelCasePlural .Table -}}
|
||||
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
|
||||
{{- $dbName := singular .Table.Name -}}
|
||||
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
|
||||
{{- $varNamePlural := camelCasePlural .Table.Name -}}
|
||||
// {{$tableNamePlural}}Where retrieves all records with the specified column values.
|
||||
func {{$tableNamePlural}}Where(db boil.DB, columns map[string]interface{}) ([]*{{$tableNameSingular}}, error) {
|
||||
var {{$varNamePlural}} []*{{$tableNameSingular}}
|
||||
query := fmt.Sprintf(`SELECT {{selectParamNames $dbName .Columns}} FROM {{.Table}} WHERE %s`, boil.Where(columns))
|
||||
query := fmt.Sprintf(`SELECT {{selectParamNames $dbName .Table.Columns}} FROM {{.Table.Name}} WHERE %s`, boil.Where(columns))
|
||||
err := db.Select(&{{$varNamePlural}}, query, boil.WhereParams(columns)...)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table}}: %s", err)
|
||||
return nil, fmt.Errorf("{{.PkgName}}: unable to select from {{.Table.Name}}: %s", err)
|
||||
}
|
||||
|
||||
return {{$varNamePlural}}, nil
|
||||
|
|
Loading…
Add table
Reference in a new issue