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"
"unicode"
"github.com/pobri19/sqlboiler/strmangle"
"github.com/nullbio/sqlboiler/strmangle"
)
// SetComplement subtracts the elements in b from a

View file

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

View file

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

View file

@ -9,8 +9,8 @@ import (
"sort"
"time"
"github.com/pobri19/sqlboiler/strmangle"
"gopkg.in/BlackBaronsTux/null-extended.v1"
"github.com/nullbio/sqlboiler/strmangle"
"gopkg.in/nullbio/null.v4"
)
var (
@ -35,6 +35,35 @@ var (
// Bind executes the query and inserts the
// result into the passed in object pointer
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
}
@ -232,9 +261,11 @@ func randomizeField(field reflect.Value) error {
case typeNullString:
newVal = null.NewString(randStr(5+rand.Intn(25)), rand.Intn(2) == 1)
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:
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:
newVal = null.NewFloat32(rand.Float32(), rand.Intn(2) == 1)
case typeNullFloat64:

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -23,6 +23,7 @@ func (q {{$varNameSingular}}Query) All() ({{$varNameSingular}}Slice, error) {
if err != nil {
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)
if err != nil {

View file

@ -2,22 +2,24 @@
{{- $dbName := singular .Table.Name -}}
{{- $tableNamePlural := titleCasePlural .Table.Name -}}
{{- $varNamePlural := camelCasePlural .Table.Name -}}
// {{$tableNamePlural}}All retrieves all records.
func Test{{$tableNamePlural}}All(t *testing.T) {
var err error
// Start from a clean slate
{{$varNamePlural}}DeleteAllRows(t)
r := make([]{{$tableNameSingular}}, 2)
// insert two random columns to test DeleteAll
for i, v := range r {
err = boil.RandomizeStruct(&v)
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 = v.Insert()
err = r[i].Insert()
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)
for i, v := range o {
err = boil.RandomizeStruct(&v)
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 = v.Insert()
err = o[i].Insert()
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 (
"text/template"
"github.com/pobri19/sqlboiler/dbdrivers"
"github.com/nullbio/sqlboiler/dbdrivers"
"github.com/spf13/cobra"
)

View file

@ -9,7 +9,7 @@ import (
"fmt"
"os"
"github.com/pobri19/sqlboiler/cmds"
"github.com/nullbio/sqlboiler/cmds"
"github.com/spf13/cobra"
)
@ -29,7 +29,7 @@ func main() {
Use: "sqlboiler",
Short: "SQL Boiler generates boilerplate structs and statements",
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 {
return cmdData.SQLBoilerPreRun(cmd, args)
},

View file

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

View file

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

View file

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