Improve test structure

This commit is contained in:
Jinzhu 2020-02-02 08:35:01 +08:00
parent 3cbd233758
commit 8cb15cadde
13 changed files with 304 additions and 126 deletions

12
callbacks/callbacks.go Normal file
View File

@ -0,0 +1,12 @@
package callbacks
import "github.com/jinzhu/gorm"
func RegisterDefaultCallbacks(db *gorm.DB) {
callback := db.Callback()
callback.Create().Register("gorm:before_create", BeforeCreate)
callback.Create().Register("gorm:save_before_associations", SaveBeforeAssociations)
callback.Create().Register("gorm:create", Create)
callback.Create().Register("gorm:save_after_associations", SaveAfterAssociations)
callback.Create().Register("gorm:after_create", AfterCreate)
}

24
callbacks/create.go Normal file
View File

@ -0,0 +1,24 @@
package callbacks
import "github.com/jinzhu/gorm"
func BeforeCreate(db *gorm.DB) {
// before save
// before create
// assign timestamp
}
func SaveBeforeAssociations(db *gorm.DB) {
}
func Create(db *gorm.DB) {
}
func SaveAfterAssociations(db *gorm.DB) {
}
func AfterCreate(db *gorm.DB) {
// after save
// after create
}

11
callbacks/interface.go Normal file
View File

@ -0,0 +1,11 @@
package callbacks
import "github.com/jinzhu/gorm"
type beforeSaveInterface interface {
BeforeSave(*gorm.DB) error
}
type beforeCreateInterface interface {
BeforeCreate(*gorm.DB) error
}

7
dialects/mysql/go.mod Normal file
View File

@ -0,0 +1,7 @@
module github.com/jinzhu/gorm/dialects/mysql
go 1.13
require (
github.com/go-sql-driver/mysql v1.5.0
)

29
dialects/mysql/mysql.go Normal file
View File

@ -0,0 +1,29 @@
package mysql
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/callbacks"
)
type Dialector struct {
}
func Open(dsn string) gorm.Dialector {
return &Dialector{}
}
func (Dialector) Initialize(db *gorm.DB) error {
// register callbacks
callbacks.RegisterDefaultCallbacks(db)
return nil
}
func (Dialector) Migrator() gorm.Migrator {
return nil
}
func (Dialector) BindVar(stmt gorm.Statement, v interface{}) string {
return "?"
}

View File

@ -0,0 +1,12 @@
package mysql_test
import (
"testing"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/mysql"
)
func TestOpen(t *testing.T) {
gorm.Open(mysql.Open("gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True"), nil)
}

7
dialects/sqlite/go.mod Normal file
View File

@ -0,0 +1,7 @@
module github.com/jinzhu/gorm/dialects/mysql
go 1.13
require (
github.com/mattn/go-sqlite3 v2.0.3+incompatible
)

28
dialects/sqlite/sqlite.go Normal file
View File

@ -0,0 +1,28 @@
package sqlite
import (
"github.com/jinzhu/gorm/callbacks"
_ "github.com/mattn/go-sqlite3"
)
type Dialector struct {
}
func Open(dsn string) gorm.Dialector {
return &Dialector{}
}
func (Dialector) Initialize(db *gorm.DB) error {
// register callbacks
callbacks.RegisterDefaultCallbacks(db)
return nil
}
func (Dialector) Migrator() gorm.Migrator {
return nil
}
func (Dialector) BindVar(stmt gorm.Statement, v interface{}) string {
return "?"
}

View File

@ -0,0 +1,15 @@
package sqlite_test
import (
"os"
"path/filepath"
"testing"
"github.com/jinzhu/gorm"
)
var DB *gorm.DB
func TestOpen(t *testing.T) {
db, err = gorm.Open("sqlite3", filepath.Join(os.TempDir(), "gorm.db"))
}

View File

@ -12,6 +12,7 @@ func (db *DB) Count(sql string, values ...interface{}) (tx *DB) {
// First find first record that match given conditions, order by primary key // First find first record that match given conditions, order by primary key
func (db *DB) First(out interface{}, where ...interface{}) (tx *DB) { func (db *DB) First(out interface{}, where ...interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.callbacks.Create().Execute(tx.Limit(1).Order("id"))
return return
} }

31
gorm.go
View File

@ -13,7 +13,7 @@ import (
type Config struct { type Config struct {
// GORM perform single create, update, delete operations in transactions by default to ensure database data integrity // GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
// You can cancel it by setting `SkipDefaultTransaction` to true // You can cancel it by setting `SkipDefaultTransaction` to true
SkipDefaultTransaction bool SkipDefaultTransaction bool // TODO
// NamingStrategy tables, columns naming strategy // NamingStrategy tables, columns naming strategy
NamingStrategy schema.Namer NamingStrategy schema.Namer
@ -27,6 +27,7 @@ type Config struct {
// Dialector GORM database dialector // Dialector GORM database dialector
type Dialector interface { type Dialector interface {
Initialize(*DB) error
Migrator() Migrator Migrator() Migrator
BindVar(stmt Statement, v interface{}) string BindVar(stmt Statement, v interface{}) string
} }
@ -37,6 +38,7 @@ type DB struct {
Dialector Dialector
Instance Instance
clone bool clone bool
callbacks *callbacks
} }
// Session session config when create new session // Session session config when create new session
@ -48,15 +50,33 @@ type Session struct {
// Open initialize db session based on dialector // Open initialize db session based on dialector
func Open(dialector Dialector, config *Config) (db *DB, err error) { func Open(dialector Dialector, config *Config) (db *DB, err error) {
if config == nil {
config = &Config{}
}
if config.NamingStrategy == nil { if config.NamingStrategy == nil {
config.NamingStrategy = schema.NamingStrategy{} config.NamingStrategy = schema.NamingStrategy{}
} }
return &DB{ if config.Logger == nil {
config.Logger = logger.Default
}
if config.NowFunc == nil {
config.NowFunc = func() time.Time { return time.Now().Local() }
}
db = &DB{
Config: config, Config: config,
Dialector: dialector, Dialector: dialector,
clone: true, clone: true,
}, nil callbacks: InitializeCallbacks(),
}
if dialector != nil {
err = dialector.Initialize(db)
}
return
} }
// Session create new db session // Session create new db session
@ -112,6 +132,11 @@ func (db *DB) Get(key string) (interface{}, bool) {
return nil, false return nil, false
} }
// Callback returns callback manager
func (db *DB) Callback() *callbacks {
return db.callbacks
}
func (db *DB) getInstance() *DB { func (db *DB) getInstance() *DB {
if db.clone { if db.clone {
ctx := db.Instance.Context ctx := db.Instance.Context

View File

@ -10,6 +10,7 @@ import (
) )
func checkSchema(t *testing.T, s *schema.Schema, v schema.Schema, primaryFields []string) { func checkSchema(t *testing.T, s *schema.Schema, v schema.Schema, primaryFields []string) {
t.Run("CheckSchema/"+s.Name, func(t *testing.T) {
equalFieldNames := []string{"Name", "Table"} equalFieldNames := []string{"Name", "Table"}
for _, name := range equalFieldNames { for _, name := range equalFieldNames {
@ -38,9 +39,11 @@ func checkSchema(t *testing.T, s *schema.Schema, v schema.Schema, primaryFields
t.Errorf("schema %v failed to found priamry key: %v", s, field) t.Errorf("schema %v failed to found priamry key: %v", s, field)
} }
} }
})
} }
func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*schema.Field)) { func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*schema.Field)) {
t.Run("CheckField/"+f.Name, func(t *testing.T) {
if fc != nil { if fc != nil {
fc(f) fc(f)
} }
@ -89,6 +92,7 @@ func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*
} }
} }
} }
})
} }
type Relation struct { type Relation struct {
@ -123,6 +127,7 @@ type Reference struct {
} }
func checkSchemaRelation(t *testing.T, s *schema.Schema, relation Relation) { func checkSchemaRelation(t *testing.T, s *schema.Schema, relation Relation) {
t.Run("CheckRelation/"+relation.Name, func(t *testing.T) {
if r, ok := s.Relationships.Relations[relation.Name]; ok { if r, ok := s.Relationships.Relations[relation.Name]; ok {
if r.Name != relation.Name { if r.Name != relation.Name {
t.Errorf("schema %v relation name expects %v, but got %v", s, r.Name, relation.Name) t.Errorf("schema %v relation name expects %v, but got %v", s, r.Name, relation.Name)
@ -198,4 +203,5 @@ func checkSchemaRelation(t *testing.T, s *schema.Schema, relation Relation) {
} else { } else {
t.Errorf("schema %v failed to find relations by name %v", s, relation.Name) t.Errorf("schema %v failed to find relations by name %v", s, relation.Name)
} }
})
} }

1
tests/create_test.go Normal file
View File

@ -0,0 +1 @@
package tests