mirror of https://github.com/go-gorm/gorm.git
Improve test structure
This commit is contained in:
parent
3cbd233758
commit
8cb15cadde
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
module github.com/jinzhu/gorm/dialects/mysql
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
)
|
|
@ -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 "?"
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
module github.com/jinzhu/gorm/dialects/mysql
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
||||
)
|
|
@ -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 "?"
|
||||
}
|
|
@ -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"))
|
||||
}
|
|
@ -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
|
||||
func (db *DB) First(out interface{}, where ...interface{}) (tx *DB) {
|
||||
tx = db.getInstance()
|
||||
tx.callbacks.Create().Execute(tx.Limit(1).Order("id"))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
31
gorm.go
31
gorm.go
|
@ -13,7 +13,7 @@ import (
|
|||
type Config struct {
|
||||
// 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
|
||||
SkipDefaultTransaction bool
|
||||
SkipDefaultTransaction bool // TODO
|
||||
|
||||
// NamingStrategy tables, columns naming strategy
|
||||
NamingStrategy schema.Namer
|
||||
|
@ -27,6 +27,7 @@ type Config struct {
|
|||
|
||||
// Dialector GORM database dialector
|
||||
type Dialector interface {
|
||||
Initialize(*DB) error
|
||||
Migrator() Migrator
|
||||
BindVar(stmt Statement, v interface{}) string
|
||||
}
|
||||
|
@ -37,6 +38,7 @@ type DB struct {
|
|||
Dialector
|
||||
Instance
|
||||
clone bool
|
||||
callbacks *callbacks
|
||||
}
|
||||
|
||||
// Session session config when create new session
|
||||
|
@ -48,15 +50,33 @@ type Session struct {
|
|||
|
||||
// Open initialize db session based on dialector
|
||||
func Open(dialector Dialector, config *Config) (db *DB, err error) {
|
||||
if config == nil {
|
||||
config = &Config{}
|
||||
}
|
||||
|
||||
if config.NamingStrategy == nil {
|
||||
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,
|
||||
Dialector: dialector,
|
||||
clone: true,
|
||||
}, nil
|
||||
callbacks: InitializeCallbacks(),
|
||||
}
|
||||
|
||||
if dialector != nil {
|
||||
err = dialector.Initialize(db)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Session create new db session
|
||||
|
@ -112,6 +132,11 @@ func (db *DB) Get(key string) (interface{}, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
// Callback returns callback manager
|
||||
func (db *DB) Callback() *callbacks {
|
||||
return db.callbacks
|
||||
}
|
||||
|
||||
func (db *DB) getInstance() *DB {
|
||||
if db.clone {
|
||||
ctx := db.Instance.Context
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
)
|
||||
|
||||
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"}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
fc(f)
|
||||
}
|
||||
|
@ -89,6 +92,7 @@ func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*
|
|||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Relation struct {
|
||||
|
@ -123,6 +127,7 @@ type Reference struct {
|
|||
}
|
||||
|
||||
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.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 {
|
||||
t.Errorf("schema %v failed to find relations by name %v", s, relation.Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package tests
|
Loading…
Reference in New Issue