Fix null package imports, finish Bind

* Fix randomizeStruct time randomization
* Defer close sql.Rows
* Begin Delete test template
This commit is contained in:
Patrick O'brien 2016-06-08 15:45:34 +10:00
parent a20574110c
commit 7aba7104a5
18 changed files with 157 additions and 61 deletions

View file

@ -8,7 +8,7 @@ import (
"strings" "strings"
"unicode" "unicode"
"github.com/pobri19/sqlboiler/strmangle" "github.com/nullbio/sqlboiler/strmangle"
) )
// SetComplement subtracts the elements in b from a // SetComplement subtracts the elements in b from a

View file

@ -5,7 +5,7 @@ import (
"testing" "testing"
"time" "time"
"gopkg.in/BlackBaronsTux/null-extended.v1" "gopkg.in/nullbio/null.v4"
) )
type testObj struct { type testObj struct {

View file

@ -1,6 +1,6 @@
package qs package qs
import "github.com/pobri19/sqlboiler/boil" import "github.com/nullbio/sqlboiler/boil"
type QueryMod func(q *boil.Query) type QueryMod func(q *boil.Query)

View file

@ -9,8 +9,8 @@ import (
"sort" "sort"
"time" "time"
"github.com/pobri19/sqlboiler/strmangle" "github.com/nullbio/sqlboiler/strmangle"
"gopkg.in/BlackBaronsTux/null-extended.v1" "gopkg.in/nullbio/null.v4"
) )
var ( var (
@ -35,6 +35,35 @@ var (
// Bind executes the query and inserts the // Bind executes the query and inserts the
// result into the passed in object pointer // result into the passed in object pointer
func (q *Query) Bind(obj interface{}) error { func (q *Query) Bind(obj interface{}) error {
typ := reflect.TypeOf(obj)
kind := typ.Kind()
if kind != reflect.Ptr {
return fmt.Errorf("Bind not given a pointer to a slice or struct: %s", typ.String())
}
typ = typ.Elem()
kind = typ.Kind()
if kind == reflect.Struct {
row := ExecQueryOne(q)
err := BindOne(row, q.selectCols, obj)
if err != nil {
return fmt.Errorf("Failed to execute Bind query for %s: %s", q.table, err)
}
} else if kind == reflect.Slice {
rows, err := ExecQueryAll(q)
if err != nil {
return fmt.Errorf("Failed to execute Bind query for %s: %s", q.table, err)
}
err = BindAll(rows, q.selectCols, obj)
if err != nil {
return fmt.Errorf("Failed to Bind results to object provided for %s: %s", q.table, err)
}
} else {
return fmt.Errorf("Bind given a pointer to a non-slice or non-struct: %s", typ.String())
}
return nil return nil
} }
@ -232,9 +261,11 @@ func randomizeField(field reflect.Value) error {
case typeNullString: case typeNullString:
newVal = null.NewString(randStr(5+rand.Intn(25)), rand.Intn(2) == 1) newVal = null.NewString(randStr(5+rand.Intn(25)), rand.Intn(2) == 1)
case typeNullTime: case typeNullTime:
newVal = null.NewTime(time.Now().Add(time.Duration(rand.Intn((int(time.Hour * 24 * 10))))), rand.Intn(2) == 1) randTime := rand.Int63n(int64(time.Hour) * 24 * 365 * 65)
newVal = null.NewTime(time.Unix(0, 0).Add(time.Duration(randTime)), rand.Intn(2) == 1)
case typeTime: case typeTime:
newVal = time.Now().Add(time.Duration(rand.Intn((int(time.Hour * 24 * 10))))) randTime := rand.Int63n(int64(time.Hour) * 24 * 365 * 65)
newVal = time.Now().Add(time.Duration(randTime))
case typeNullFloat32: case typeNullFloat32:
newVal = null.NewFloat32(rand.Float32(), rand.Intn(2) == 1) newVal = null.NewFloat32(rand.Float32(), rand.Intn(2) == 1)
case typeNullFloat64: case typeNullFloat64:

View file

@ -4,19 +4,19 @@ import (
"testing" "testing"
"time" "time"
"gopkg.in/BlackBaronsTux/null-extended.v1" "gopkg.in/nullbio/null.v4"
) )
func TestBind(t *testing.T) { func TestBind(t *testing.T) {
t.Errorf("Not implemented")
} }
func TestBindOne(t *testing.T) { func TestBindOne(t *testing.T) {
t.Errorf("Not implemented")
} }
func TestBindAll(t *testing.T) { func TestBindAll(t *testing.T) {
t.Errorf("Not implemented")
} }
func TestGetStructValues(t *testing.T) { func TestGetStructValues(t *testing.T) {

View file

@ -6,56 +6,56 @@ import (
"text/template" "text/template"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/pobri19/sqlboiler/strmangle" "github.com/nullbio/sqlboiler/strmangle"
) )
// sqlBoilerTypeImports imports are only included in the template output if the database // sqlBoilerTypeImports imports are only included in the template output if the database
// requires one of the following special types. Check TranslateColumnType to see the type assignments. // requires one of the following special types. Check TranslateColumnType to see the type assignments.
var sqlBoilerTypeImports = map[string]imports{ var sqlBoilerTypeImports = map[string]imports{
"null.Float32": imports{ "null.Float32": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Float64": imports{ "null.Float64": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Int": imports{ "null.Int": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Int8": imports{ "null.Int8": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Int16": imports{ "null.Int16": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Int32": imports{ "null.Int32": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Int64": imports{ "null.Int64": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Uint": imports{ "null.Uint": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Uint8": imports{ "null.Uint8": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Uint16": imports{ "null.Uint16": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Uint32": imports{ "null.Uint32": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Uint64": imports{ "null.Uint64": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.String": imports{ "null.String": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Bool": imports{ "null.Bool": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"null.Time": imports{ "null.Time": imports{
thirdparty: importList{`"gopkg.in/BlackBaronsTux/null-extended.v1"`}, thirdparty: importList{`"gopkg.in/nullbio/null.v4"`},
}, },
"time.Time": imports{ "time.Time": imports{
standard: importList{`"time"`}, standard: importList{`"time"`},
@ -70,8 +70,8 @@ var sqlBoilerImports = imports{
`"strings"`, `"strings"`,
}, },
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
`"github.com/pobri19/sqlboiler/boil/qs"`, `"github.com/nullbio/sqlboiler/boil/qs"`,
}, },
} }
@ -79,8 +79,8 @@ var sqlBoilerSinglesImports = map[string]imports{
"helpers": imports{ "helpers": imports{
standard: importList{}, standard: importList{},
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
`"github.com/pobri19/sqlboiler/boil/qs"`, `"github.com/nullbio/sqlboiler/boil/qs"`,
}, },
}, },
} }
@ -91,7 +91,7 @@ var sqlBoilerTestImports = imports{
`"testing"`, `"testing"`,
}, },
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
}, },
} }
@ -122,7 +122,7 @@ var sqlBoilerTestMainImports = map[string]imports{
`"math/rand"`, `"math/rand"`,
}, },
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
`"github.com/BurntSushi/toml"`, `"github.com/BurntSushi/toml"`,
`_ "github.com/lib/pq"`, `_ "github.com/lib/pq"`,
}, },

View file

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
) )
func combineImports(a, b imports) imports { func combineImports(a, b imports) imports {

View file

@ -5,7 +5,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
) )
func TestCombineTypeImports(t *testing.T) { func TestCombineTypeImports(t *testing.T) {
@ -15,7 +15,7 @@ func TestCombineTypeImports(t *testing.T) {
`"fmt"`, `"fmt"`,
}, },
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
}, },
} }
@ -26,8 +26,8 @@ func TestCombineTypeImports(t *testing.T) {
`"time"`, `"time"`,
}, },
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
`"gopkg.in/guregu/null.v3"`, `"gopkg.in/nullbio/null.v4"`,
}, },
} }
@ -59,8 +59,8 @@ func TestCombineTypeImports(t *testing.T) {
`"time"`, `"time"`,
}, },
thirdparty: importList{ thirdparty: importList{
`"github.com/pobri19/sqlboiler/boil"`, `"github.com/nullbio/sqlboiler/boil"`,
`"gopkg.in/guregu/null.v3"`, `"gopkg.in/nullbio/null.v4"`,
}, },
} }
@ -76,11 +76,11 @@ func TestCombineImports(t *testing.T) {
a := imports{ a := imports{
standard: importList{"fmt"}, standard: importList{"fmt"},
thirdparty: importList{"github.com/pobri19/sqlboiler", "gopkg.in/guregu/null.v3"}, thirdparty: importList{"github.com/nullbio/sqlboiler", "gopkg.in/nullbio/null.v4"},
} }
b := imports{ b := imports{
standard: importList{"os"}, standard: importList{"os"},
thirdparty: importList{"github.com/pobri19/sqlboiler"}, thirdparty: importList{"github.com/nullbio/sqlboiler"},
} }
c := combineImports(a, b) c := combineImports(a, b)
@ -88,8 +88,8 @@ func TestCombineImports(t *testing.T) {
if c.standard[0] != "fmt" && c.standard[1] != "os" { if c.standard[0] != "fmt" && c.standard[1] != "os" {
t.Errorf("Wanted: fmt, os got: %#v", c.standard) t.Errorf("Wanted: fmt, os got: %#v", c.standard)
} }
if c.thirdparty[0] != "github.com/pobri19/sqlboiler" && c.thirdparty[1] != "gopkg.in/guregu/null.v3" { if c.thirdparty[0] != "github.com/nullbio/sqlboiler" && c.thirdparty[1] != "gopkg.in/nullbio/null.v4" {
t.Errorf("Wanted: github.com/pobri19/sqlboiler, gopkg.in/guregu/null.v3 got: %#v", c.thirdparty) t.Errorf("Wanted: github.com/nullbio/sqlboiler, gopkg.in/nullbio/null.v4 got: %#v", c.thirdparty)
} }
} }

View file

@ -9,7 +9,7 @@ import (
"strings" "strings"
"text/template" "text/template"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -12,7 +12,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
) )
var cmdData *CmdData var cmdData *CmdData

View file

@ -23,6 +23,7 @@ func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("{{.PkgName}}: failed to execute an all query for {{.Table.Name}}: %s", err) return nil, fmt.Errorf("{{.PkgName}}: failed to execute an all query for {{.Table.Name}}: %s", err)
} }
defer res.Close()
err = boil.BindAll(res, boil.Select(q.Query), &o) err = boil.BindAll(res, boil.Select(q.Query), &o)
if err != nil { if err != nil {

View file

@ -2,22 +2,24 @@
{{- $dbName := singular .Table.Name -}} {{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := titleCasePlural .Table.Name -}} {{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}} {{- $varNamePlural := camelCasePlural .Table.Name -}}
// {{$tableNamePlural}}All retrieves all records.
func Test{{$tableNamePlural}}All(t *testing.T) { func Test{{$tableNamePlural}}All(t *testing.T) {
var err error var err error
// Start from a clean slate
{{$varNamePlural}}DeleteAllRows(t)
r := make([]{{$tableNameSingular}}, 2) r := make([]{{$tableNameSingular}}, 2)
// insert two random columns to test DeleteAll // insert two random columns to test DeleteAll
for i, v := range r { for i := 0; i < len(r); i++ {
err = boil.RandomizeStruct(&v) err = boil.RandomizeStruct(&r[i])
if err != nil { if err != nil {
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err) t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
} }
err = v.Insert() err = r[i].Insert()
if err != nil { if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", v, err) t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", r[i], err)
} }
} }
@ -37,15 +39,15 @@ func Test{{$tableNamePlural}}All(t *testing.T) {
o := make([]{{$tableNameSingular}}, 3) o := make([]{{$tableNameSingular}}, 3)
for i, v := range o { for i := 0; i < len(o); i++ {
err = boil.RandomizeStruct(&v) err = boil.RandomizeStruct(&o[i])
if err != nil { if err != nil {
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err) t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
} }
err = v.Insert() err = o[i].Insert()
if err != nil { if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", v, err) t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
} }
} }

View file

@ -0,0 +1,62 @@
{{- $tableNameSingular := titleCaseSingular .Table.Name -}}
{{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
func {{$varNamePlural}}DeleteAllRows(t *testing.T) {
// Delete all rows to give a clean slate
err := {{$tableNamePlural}}().DeleteAll()
if err != nil {
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
}
}
func Test{{$tableNamePlural}}Delete(t *testing.T) {
var err error
// Start from a clean slate
{{$varNamePlural}}DeleteAllRows(t)
r := make([]{{$tableNameSingular}}, 3)
// insert random columns to test DeleteAll
for i := 0; i < len(r); i++ {
err = boil.RandomizeStruct(&r[i])
if err != nil {
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
}
err = r[i].Insert()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", r[i], err)
}
}
// Test DeleteAll()
err = {{$tableNamePlural}}().DeleteAll()
if err != nil {
t.Errorf("Unable to delete all from {{$tableNamePlural}}: %s", err)
}
// Check number of rows in table to ensure DeleteAll was successful
var c int64
c, err = {{$tableNamePlural}}().Count()
if c != 0 {
t.Errorf("Expected {{.Table.Name}} table to be empty, but got %d rows", c)
}
// insert random columns to test Delete
o := make([]{{$tableNameSingular}}, 3)
for i := 0; i < len(o); i++ {
err = boil.RandomizeStruct(&o[i])
if err != nil {
t.Errorf("%d: Unable to randomize {{$tableNameSingular}} struct: %s", i, err)
}
err = o[i].Insert()
if err != nil {
t.Errorf("Unable to insert {{$tableNameSingular}}:\n%#v\nErr: %s", o[i], err)
}
}
}

View file

@ -3,7 +3,7 @@ package cmds
import ( import (
"text/template" "text/template"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View file

@ -9,7 +9,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/pobri19/sqlboiler/cmds" "github.com/nullbio/sqlboiler/cmds"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -29,7 +29,7 @@ func main() {
Use: "sqlboiler", Use: "sqlboiler",
Short: "SQL Boiler generates boilerplate structs and statements", Short: "SQL Boiler generates boilerplate structs and statements",
Long: "SQL Boiler generates boilerplate structs and statements from the template files.\n" + Long: "SQL Boiler generates boilerplate structs and statements from the template files.\n" +
`Complete documentation is available at http://github.com/pobri19/sqlboiler`, `Complete documentation is available at http://github.com/nullbio/sqlboiler`,
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
return cmdData.SQLBoilerPreRun(cmd, args) return cmdData.SQLBoilerPreRun(cmd, args)
}, },

View file

@ -6,7 +6,7 @@ import (
"strings" "strings"
"github.com/jinzhu/inflection" "github.com/jinzhu/inflection"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
) )
var rgxAutoIncColumn = regexp.MustCompile(`^nextval\(.*\)`) var rgxAutoIncColumn = regexp.MustCompile(`^nextval\(.*\)`)

View file

@ -3,7 +3,7 @@ package strmangle
import ( import (
"testing" "testing"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
) )
var testColumns = []dbdrivers.Column{ var testColumns = []dbdrivers.Column{

View file

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/pobri19/sqlboiler/dbdrivers" "github.com/nullbio/sqlboiler/dbdrivers"
) )
func RandDBStruct(varName string, table dbdrivers.Table) string { func RandDBStruct(varName string, table dbdrivers.Table) string {