Move fakeDB to MockDriver in drivers package

This commit is contained in:
Patrick O'brien 2016-08-18 15:26:24 +10:00
parent 94d36d7bf7
commit 8d0cd2a53d
3 changed files with 154 additions and 137 deletions

81
bdb/drivers/mock.go Normal file
View file

@ -0,0 +1,81 @@
package drivers
import (
"github.com/vattle/sqlboiler/bdb"
"github.com/vattle/sqlboiler/strmangle"
)
// MockDriver is a mock implementation of the bdb driver Interface
type MockDriver int
// TableNames returns a list of mock table names
func (MockDriver) TableNames(exclude []string) ([]string, error) {
tables := []string{"pilots", "jets", "airports", "licenses", "pilots_jets_tags"}
return strmangle.SetComplement(tables, exclude), nil
}
// Columns returns a list of mock columns
func (MockDriver) Columns(tableName string) ([]bdb.Column, error) {
return map[string][]bdb.Column{
"pilots": {{Name: "id", Type: "int32"}},
"airports": {{Name: "id", Type: "int32", Nullable: true}},
"jets": {
{Name: "id", Type: "int32"},
{Name: "pilot_id", Type: "int32", Nullable: true, Unique: true},
{Name: "airport_id", Type: "int32"},
},
"licenses": {
{Name: "pilot_id", Type: "int32"},
{Name: "source_id", Type: "int32", Nullable: true},
},
"pilots_jets_tags": {
{Name: "pilot_id", Type: "int32"},
{Name: "jet_id", Type: "int32"},
},
}[tableName], nil
}
// ForeignKeyInfo returns a list of mock foreignkeys
func (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"},
{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id"},
},
"licenses": {
{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
{Name: "licenses_source_id_fk", Column: "source_id", ForeignTable: "pilots", ForeignColumn: "id"},
},
"pilots_jets_tags": {
{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id"},
{Name: "jet_id_fk", Column: "jet_id", ForeignTable: "jets", ForeignColumn: "id"},
},
}[tableName], nil
}
// 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
}
// PrimaryKeyInfo returns mock primary key info for the passed in table name
func (MockDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
return map[string]*bdb.PrimaryKey{
"pilots_jets_tags": {
Name: "pilot_jet_id_pkey",
Columns: []string{"pilot_id", "jet_id"},
},
}[tableName], nil
}
// UseLastInsertID returns a database mock LastInsertID compatability flag
func (MockDriver) UseLastInsertID() bool { return false }
// Open mimics a database open call and returns nil for no error
func (MockDriver) Open() error { return nil }
// Close mimics a database close call
func (MockDriver) Close() {}

View file

@ -1,65 +0,0 @@
package main
import (
"github.com/vattle/sqlboiler/bdb"
"github.com/vattle/sqlboiler/strmangle"
)
type fakeDB int
func (fakeDB) TableNames(exclude []string) ([]string, error) {
tables := []string{"users", "videos", "contests", "notifications", "users_videos_tags"}
return strmangle.SetComplement(tables, exclude), nil
}
func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
return map[string][]bdb.Column{
"users": {{Name: "id", Type: "int32"}},
"contests": {{Name: "id", Type: "int32", Nullable: true}},
"videos": {
{Name: "id", Type: "int32"},
{Name: "user_id", Type: "int32", Nullable: true, Unique: true},
{Name: "contest_id", Type: "int32"},
},
"notifications": {
{Name: "user_id", Type: "int32"},
{Name: "source_id", Type: "int32", Nullable: true},
},
"users_videos_tags": {
{Name: "user_id", Type: "int32"},
{Name: "video_id", Type: "int32"},
},
}[tableName], nil
}
func (fakeDB) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
return map[string][]bdb.ForeignKey{
"videos": {
{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
},
"notifications": {
{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
},
"users_videos_tags": {
{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
},
}[tableName], nil
}
func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
if c.Nullable {
c.Type = "null." + strmangle.TitleCase(c.Type)
}
return c
}
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
return map[string]*bdb.PrimaryKey{
"users_videos_tags": {
Name: "user_video_id_pkey",
Columns: []string{"user_id", "video_id"},
},
}[tableName], nil
}
func (fakeDB) UseLastInsertID() bool { return false }
func (fakeDB) Open() error { return nil }
func (fakeDB) Close() {}

View file

@ -6,38 +6,39 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/vattle/sqlboiler/bdb" "github.com/vattle/sqlboiler/bdb"
"github.com/vattle/sqlboiler/bdb/drivers"
) )
func TestTextsFromForeignKey(t *testing.T) { func TestTextsFromForeignKey(t *testing.T) {
t.Parallel() t.Parallel()
tables, err := bdb.Tables(fakeDB(0)) tables, err := bdb.Tables(drivers.MockDriver(0))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
videos := bdb.GetTable(tables, "videos") jets := bdb.GetTable(tables, "jets")
texts := textsFromForeignKey("models", tables, videos, videos.FKeys[0]) texts := textsFromForeignKey("models", tables, jets, jets.FKeys[0])
expect := RelationshipToOneTexts{} expect := RelationshipToOneTexts{}
expect.ForeignKey = videos.FKeys[0] expect.ForeignKey = jets.FKeys[0]
expect.LocalTable.NameGo = "Video" expect.LocalTable.NameGo = "Jet"
expect.LocalTable.ColumnNameGo = "UserID" expect.LocalTable.ColumnNameGo = "PilotID"
expect.ForeignTable.Name = "users" expect.ForeignTable.Name = "pilots"
expect.ForeignTable.NameGo = "User" expect.ForeignTable.NameGo = "Pilot"
expect.ForeignTable.NamePluralGo = "Users" expect.ForeignTable.NamePluralGo = "Pilots"
expect.ForeignTable.ColumnName = "id" expect.ForeignTable.ColumnName = "id"
expect.ForeignTable.ColumnNameGo = "ID" expect.ForeignTable.ColumnNameGo = "ID"
expect.Function.PackageName = "models" expect.Function.PackageName = "models"
expect.Function.Name = "User" expect.Function.Name = "Pilot"
expect.Function.Varname = "user" expect.Function.Varname = "pilot"
expect.Function.Receiver = "v" expect.Function.Receiver = "j"
expect.Function.ReverseInserts = false expect.Function.ReverseInserts = false
expect.Function.LocalAssignment = "UserID.Int32" expect.Function.LocalAssignment = "PilotID.Int32"
expect.Function.ForeignAssignment = "ID" expect.Function.ForeignAssignment = "ID"
if !reflect.DeepEqual(expect, texts) { if !reflect.DeepEqual(expect, texts) {
@ -48,13 +49,13 @@ func TestTextsFromForeignKey(t *testing.T) {
func TestTextsFromOneToOneRelationship(t *testing.T) { func TestTextsFromOneToOneRelationship(t *testing.T) {
t.Parallel() t.Parallel()
tables, err := bdb.Tables(fakeDB(0)) tables, err := bdb.Tables(drivers.MockDriver(0))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
users := bdb.GetTable(tables, "users") pilots := bdb.GetTable(tables, "pilots")
texts := textsFromOneToOneRelationship("models", tables, users, users.ToManyRelationships[0]) texts := textsFromOneToOneRelationship("models", tables, pilots, pilots.ToManyRelationships[0])
expect := RelationshipToOneTexts{} expect := RelationshipToOneTexts{}
expect.ForeignKey = bdb.ForeignKey{ expect.ForeignKey = bdb.ForeignKey{
@ -63,29 +64,29 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
Nullable: false, Nullable: false,
Unique: false, Unique: false,
ForeignTable: "videos", ForeignTable: "jets",
ForeignColumn: "user_id", ForeignColumn: "pilot_id",
ForeignColumnNullable: true, ForeignColumnNullable: true,
ForeignColumnUnique: true, ForeignColumnUnique: true,
} }
expect.LocalTable.NameGo = "User" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.ColumnNameGo = "ID" expect.LocalTable.ColumnNameGo = "ID"
expect.ForeignTable.Name = "videos" expect.ForeignTable.Name = "jets"
expect.ForeignTable.NameGo = "Video" expect.ForeignTable.NameGo = "Jet"
expect.ForeignTable.NamePluralGo = "Videos" expect.ForeignTable.NamePluralGo = "Jets"
expect.ForeignTable.ColumnName = "user_id" expect.ForeignTable.ColumnName = "pilot_id"
expect.ForeignTable.ColumnNameGo = "UserID" expect.ForeignTable.ColumnNameGo = "PilotID"
expect.Function.PackageName = "models" expect.Function.PackageName = "models"
expect.Function.Name = "Video" expect.Function.Name = "Jet"
expect.Function.Varname = "video" expect.Function.Varname = "jet"
expect.Function.Receiver = "u" expect.Function.Receiver = "p"
expect.Function.ReverseInserts = true expect.Function.ReverseInserts = true
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "UserID.Int32" expect.Function.ForeignAssignment = "PilotID.Int32"
if !reflect.DeepEqual(expect, texts) { if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
@ -95,65 +96,65 @@ func TestTextsFromOneToOneRelationship(t *testing.T) {
func TestTextsFromRelationship(t *testing.T) { func TestTextsFromRelationship(t *testing.T) {
t.Parallel() t.Parallel()
tables, err := bdb.Tables(fakeDB(0)) tables, err := bdb.Tables(drivers.MockDriver(0))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
users := bdb.GetTable(tables, "users") pilots := bdb.GetTable(tables, "pilots")
texts := textsFromRelationship(tables, users, users.ToManyRelationships[0]) texts := textsFromRelationship(tables, pilots, pilots.ToManyRelationships[0])
expect := RelationshipToManyTexts{} expect := RelationshipToManyTexts{}
expect.LocalTable.NameGo = "User" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "user" expect.LocalTable.NameSingular = "pilot"
expect.ForeignTable.NameGo = "Video" expect.ForeignTable.NameGo = "Jet"
expect.ForeignTable.NameSingular = "video" expect.ForeignTable.NameSingular = "jet"
expect.ForeignTable.NamePluralGo = "Videos" expect.ForeignTable.NamePluralGo = "Jets"
expect.ForeignTable.NameHumanReadable = "videos" expect.ForeignTable.NameHumanReadable = "jets"
expect.ForeignTable.Slice = "VideoSlice" expect.ForeignTable.Slice = "JetSlice"
expect.Function.Name = "Videos" expect.Function.Name = "Jets"
expect.Function.Receiver = "u" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "UserID.Int32" expect.Function.ForeignAssignment = "PilotID.Int32"
if !reflect.DeepEqual(expect, texts) { if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
} }
texts = textsFromRelationship(tables, users, users.ToManyRelationships[1]) texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[1])
expect = RelationshipToManyTexts{} expect = RelationshipToManyTexts{}
expect.LocalTable.NameGo = "User" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "user" expect.LocalTable.NameSingular = "pilot"
expect.ForeignTable.NameGo = "Notification" expect.ForeignTable.NameGo = "License"
expect.ForeignTable.NameSingular = "notification" expect.ForeignTable.NameSingular = "license"
expect.ForeignTable.NamePluralGo = "Notifications" expect.ForeignTable.NamePluralGo = "Licenses"
expect.ForeignTable.NameHumanReadable = "notifications" expect.ForeignTable.NameHumanReadable = "licenses"
expect.ForeignTable.Slice = "NotificationSlice" expect.ForeignTable.Slice = "LicenseSlice"
expect.Function.Name = "Notifications" expect.Function.Name = "Licenses"
expect.Function.Receiver = "u" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "UserID" expect.Function.ForeignAssignment = "PilotID"
if !reflect.DeepEqual(expect, texts) { if !reflect.DeepEqual(expect, texts) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
} }
texts = textsFromRelationship(tables, users, users.ToManyRelationships[2]) texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[2])
expect = RelationshipToManyTexts{} expect = RelationshipToManyTexts{}
expect.LocalTable.NameGo = "User" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "user" expect.LocalTable.NameSingular = "pilot"
expect.ForeignTable.NameGo = "Notification" expect.ForeignTable.NameGo = "License"
expect.ForeignTable.NameSingular = "notification" expect.ForeignTable.NameSingular = "license"
expect.ForeignTable.NamePluralGo = "Notifications" expect.ForeignTable.NamePluralGo = "Licenses"
expect.ForeignTable.NameHumanReadable = "notifications" expect.ForeignTable.NameHumanReadable = "licenses"
expect.ForeignTable.Slice = "NotificationSlice" expect.ForeignTable.Slice = "LicenseSlice"
expect.Function.Name = "SourceNotifications" expect.Function.Name = "SourceLicenses"
expect.Function.Receiver = "u" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "SourceID.Int32" expect.Function.ForeignAssignment = "SourceID.Int32"
@ -161,19 +162,19 @@ func TestTextsFromRelationship(t *testing.T) {
t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts)) t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
} }
texts = textsFromRelationship(tables, users, users.ToManyRelationships[3]) texts = textsFromRelationship(tables, pilots, pilots.ToManyRelationships[3])
expect = RelationshipToManyTexts{} expect = RelationshipToManyTexts{}
expect.LocalTable.NameGo = "User" expect.LocalTable.NameGo = "Pilot"
expect.LocalTable.NameSingular = "user" expect.LocalTable.NameSingular = "pilot"
expect.ForeignTable.NameGo = "Video" expect.ForeignTable.NameGo = "Jet"
expect.ForeignTable.NameSingular = "video" expect.ForeignTable.NameSingular = "jet"
expect.ForeignTable.NamePluralGo = "Videos" expect.ForeignTable.NamePluralGo = "Jets"
expect.ForeignTable.NameHumanReadable = "videos" expect.ForeignTable.NameHumanReadable = "jets"
expect.ForeignTable.Slice = "VideoSlice" expect.ForeignTable.Slice = "JetSlice"
expect.Function.Name = "Videos" expect.Function.Name = "Jets"
expect.Function.Receiver = "u" expect.Function.Receiver = "p"
expect.Function.LocalAssignment = "ID" expect.Function.LocalAssignment = "ID"
expect.Function.ForeignAssignment = "ID" expect.Function.ForeignAssignment = "ID"