Convert sqlboiler tests to MockDriver

* Improve impl of mock driver
This commit is contained in:
Patrick O'brien 2016-08-18 22:11:02 +10:00
parent c04e0d003a
commit 22a904d025
6 changed files with 82 additions and 148 deletions

View file

@ -6,37 +6,51 @@ import (
)
// MockDriver is a mock implementation of the bdb driver Interface
type MockDriver int
type MockDriver struct{}
// TableNames returns a list of mock table names
func (MockDriver) TableNames(exclude []string) ([]string, error) {
tables := []string{"pilots", "jets", "airports", "licenses", "pilots_jets_tags"}
func (m *MockDriver) TableNames(exclude []string) ([]string, error) {
tables := []string{"pilots", "jets", "airports", "licenses", "hangars", "pilots_jets_tags"}
return strmangle.SetComplement(tables, exclude), nil
}
// Columns returns a list of mock columns
func (MockDriver) Columns(tableName string) ([]bdb.Column, error) {
func (m *MockDriver) Columns(tableName string) ([]bdb.Column, error) {
return map[string][]bdb.Column{
"pilots": {{Name: "id", Type: "int32"}},
"airports": {{Name: "id", Type: "int32", Nullable: true}},
"pilots": {{Name: "id", Type: "int", DBType: "integer"}},
"airports": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "size", Type: "null.Int", DBType: "integer", Nullable: true},
},
"jets": {
{Name: "id", Type: "int32"},
{Name: "pilot_id", Type: "int32", Nullable: true, Unique: true},
{Name: "airport_id", Type: "int32"},
{Name: "id", Type: "int", DBType: "integer"},
{Name: "pilot_id", Type: "int", DBType: "integer", Nullable: true, Unique: true},
{Name: "airport_id", Type: "int", DBType: "integer"},
{Name: "name", Type: "string", DBType: "character", Nullable: false},
{Name: "color", Type: "null.String", DBType: "character", Nullable: true},
{Name: "uuid", Type: "string", DBType: "uuid", Nullable: true},
{Name: "identifier", Type: "string", DBType: "uuid", Nullable: false},
{Name: "cargo", Type: "[]byte", DBType: "bytea", Nullable: false},
{Name: "manifest", Type: "[]byte", DBType: "bytea", Nullable: true, Unique: true},
},
"licenses": {
{Name: "pilot_id", Type: "int32"},
{Name: "source_id", Type: "int32", Nullable: true},
{Name: "id", Type: "int", DBType: "integer"},
{Name: "pilot_id", Type: "int", DBType: "integer"},
{Name: "source_id", Type: "int", DBType: "integer", Nullable: true},
},
"hangars": {
{Name: "id", Type: "int", DBType: "integer"},
{Name: "name", Type: "string", DBType: "character", Nullable: true, Unique: true},
},
"pilots_jets_tags": {
{Name: "pilot_id", Type: "int32"},
{Name: "jet_id", Type: "int32"},
{Name: "pilot_id", Type: "int", DBType: "integer"},
{Name: "jet_id", Type: "int", DBType: "integer"},
},
}[tableName], nil
}
// ForeignKeyInfo returns a list of mock foreignkeys
func (MockDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
func (m *MockDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
return map[string][]bdb.ForeignKey{
"jets": {
{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
@ -54,16 +68,34 @@ func (MockDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
}
// TranslateColumnType converts a column to its "null." form if it is nullable
func (MockDriver) TranslateColumnType(c bdb.Column) bdb.Column {
if c.Nullable {
c.Type = "null." + strmangle.TitleCase(c.Type)
}
return c
func (m *MockDriver) TranslateColumnType(c bdb.Column) bdb.Column {
p := &PostgresDriver{}
return p.TranslateColumnType(c)
}
// PrimaryKeyInfo returns mock primary key info for the passed in table name
func (MockDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
func (m *MockDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
return map[string]*bdb.PrimaryKey{
"pilots": {
Name: "pilot_id_pkey",
Columns: []string{"id"},
},
"airports": {
Name: "airport_id_pkey",
Columns: []string{"id"},
},
"jets": {
Name: "jet_id_pkey",
Columns: []string{"id"},
},
"licenses": {
Name: "license_id_pkey",
Columns: []string{"id"},
},
"hangars": {
Name: "hangar_id_pkey",
Columns: []string{"id"},
},
"pilots_jets_tags": {
Name: "pilot_jet_id_pkey",
Columns: []string{"pilot_id", "jet_id"},
@ -72,10 +104,10 @@ func (MockDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
}
// UseLastInsertID returns a database mock LastInsertID compatability flag
func (MockDriver) UseLastInsertID() bool { return false }
func (m *MockDriver) UseLastInsertID() bool { return false }
// Open mimics a database open call and returns nil for no error
func (MockDriver) Open() error { return nil }
func (m *MockDriver) Open() error { return nil }
// Close mimics a database close call
func (MockDriver) Close() {}
func (m *MockDriver) Close() {}

View file

@ -6,7 +6,6 @@ type Config struct {
PkgName string `toml:"pkg_name"`
OutFolder string `toml:"out_folder"`
ExcludeTables []string `toml:"exclude"`
TableNames []string
Postgres PostgresConfig `toml:"postgres"`
}

View file

@ -180,6 +180,8 @@ func (s *State) initDriver(driverName string) error {
s.Config.Postgres.Port,
s.Config.Postgres.SSLMode,
)
case "mock":
s.Driver = &drivers.MockDriver{}
}
if s.Driver == nil {

View file

@ -11,144 +11,45 @@ import (
"regexp"
"strconv"
"testing"
"github.com/vattle/sqlboiler/bdb"
"github.com/vattle/sqlboiler/bdb/drivers"
)
var state *State
var rgxHasSpaces = regexp.MustCompile(`^\s+`)
func init() {
state = &State{
Driver: drivers.MockDriver(0),
Tables: []bdb.Table{
{
Name: "patrick_table",
Columns: []bdb.Column{
{Name: "patrick_column", Type: "string", Nullable: false},
{Name: "aaron_column", Type: "null.String", Nullable: true},
{Name: "id", Type: "null.Int", Nullable: true},
{Name: "fun_id", Type: "int64", Nullable: false},
{Name: "time", Type: "null.Time", Nullable: true},
{Name: "fun_time", Type: "time.Time", Nullable: false},
{Name: "cool_stuff_forever", Type: "[]byte", Nullable: false},
},
PKey: &bdb.PrimaryKey{
Name: "pkey_thing",
Columns: []string{"id", "fun_id"},
},
},
{
Name: "spiderman",
Columns: []bdb.Column{
{Name: "id", Type: "int64", Nullable: false},
},
PKey: &bdb.PrimaryKey{
Name: "pkey_id",
Columns: []string{"id"},
},
},
{
Name: "spiderman_table_two",
Columns: []bdb.Column{
{Name: "id", Type: "int64", Nullable: false},
{Name: "patrick", Type: "string", Nullable: false},
},
PKey: &bdb.PrimaryKey{
Name: "pkey_id",
Columns: []string{"id"},
},
},
},
Config: &Config{
PkgName: "patrick",
OutFolder: "",
DriverName: "postgres",
},
}
}
func TestLoadTemplate(t *testing.T) {
t.Parallel()
template, err := loadTemplate("templates_test/main_test", "postgres_main.tpl")
if err != nil {
t.Fatalf("Unable to loadTemplate: %s", err)
}
if template == nil {
t.Fatal("Unable to load template.")
}
}
func TestTemplates(t *testing.T) {
func TestNew(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
if err := checkPKeys(state.Tables); err != nil {
t.Fatalf("%s", err)
}
// Initialize the templates
var err error
state.Templates, err = loadTemplates("templates")
out, err := ioutil.TempDir("", "boil_templates")
if err != nil {
t.Fatalf("Unable to initialize templates: %s", err)
t.Fatalf("unable to create tempdir: %s", err)
}
if len(state.Templates.Templates()) == 0 {
t.Errorf("Templates is empty.")
}
state.SingletonTemplates, err = loadTemplates("templates/singleton")
if err != nil {
t.Fatalf("Unable to initialize singleton templates: %s", err)
}
if len(state.SingletonTemplates.Templates()) == 0 {
t.Errorf("SingletonTemplates is empty.")
}
state.TestTemplates, err = loadTemplates("templates_test")
if err != nil {
t.Fatalf("Unable to initialize templates: %s", err)
}
if len(state.Templates.Templates()) == 0 {
t.Errorf("Templates is empty.")
}
state.TestMainTemplate, err = loadTemplate("templates_test/main_test", "postgres_main.tpl")
if err != nil {
t.Fatalf("Unable to initialize templates: %s", err)
}
state.SingletonTestTemplates, err = loadTemplates("templates_test/singleton")
if err != nil {
t.Fatalf("Unable to initialize single test templates: %s", err)
}
if len(state.SingletonTestTemplates.Templates()) == 0 {
t.Errorf("SingleTestTemplates is empty.")
}
state.Config.OutFolder, err = ioutil.TempDir("", "templates")
if err != nil {
t.Fatalf("Unable to create tempdir: %s", err)
}
// Defer cleanup of the tmp folder
defer func() {
if t.Failed() {
t.Log("template test output:", state.Config.OutFolder)
return
}
os.RemoveAll(state.Config.OutFolder)
}()
if err = state.Run(true); err != nil {
t.Errorf("Unable to run SQLBoilerRun: %s", err)
config := &Config{
DriverName: "mock",
PkgName: "models",
OutFolder: out,
ExcludeTables: []string{"hangars"},
}
state, err = New(config)
if err != nil {
t.Fatalf("Unable to create State using config: %s", err)
}
if err = state.Run(false); err != nil {
t.Errorf("Unable to execute State.Run: %s", err)
}
buf := &bytes.Buffer{}

View file

View file

@ -12,7 +12,7 @@ import (
func TestTextsFromForeignKey(t *testing.T) {
t.Parallel()
tables, err := bdb.Tables(drivers.MockDriver(0))
tables, err := bdb.Tables(&drivers.MockDriver{})
if err != nil {
t.Fatal(err)
}
@ -38,7 +38,7 @@ func TestTextsFromForeignKey(t *testing.T) {
expect.Function.Receiver = "j"
expect.Function.ReverseInserts = false
expect.Function.LocalAssignment = "PilotID.Int32"
expect.Function.LocalAssignment = "PilotID.Int"
expect.Function.ForeignAssignment = "ID"
if !reflect.DeepEqual(expect, texts) {
@ -49,7 +49,7 @@ func TestTextsFromForeignKey(t *testing.T) {
func TestTextsFromOneToOneRelationship(t *testing.T) {
t.Parallel()
tables, err := bdb.Tables(drivers.MockDriver(0))
tables, err := bdb.Tables(&drivers.MockDriver{})
if err != nil {
t.Fatal(err)
}
@ -86,7 +86,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
expect.Function.ReverseInserts = true
expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID.Int32"
expect.Function.ForeignAssignment = "PilotID.Int"
if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
@ -96,7 +96,7 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
func TestTextsFromRelationship(t *testing.T) {
t.Parallel()
tables, err := bdb.Tables(drivers.MockDriver(0))
tables, err := bdb.Tables(&drivers.MockDriver{})
if err != nil {
t.Fatal(err)
}
@ -116,7 +116,7 @@ func TestTextsFromRelationship(t *testing.T) {
expect.Function.Name = "Jets"
expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "PilotID.Int32"
expect.Function.ForeignAssignment = "PilotID.Int"
if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
@ -156,7 +156,7 @@ func TestTextsFromRelationship(t *testing.T) {
expect.Function.Name = "SourceLicenses"
expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "SourceID.Int32"
expect.Function.ForeignAssignment = "SourceID.Int"
if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))