gorm/callback_test.go

113 lines
4.0 KiB
Go
Raw Normal View History

2014-01-25 15:35:21 +04:00
package gorm
import (
"reflect"
"runtime"
"strings"
"testing"
)
2014-01-26 08:41:37 +04:00
func equalFuncs(funcs []*func(s *Scope), fnames []string) bool {
2014-01-25 15:35:21 +04:00
var names []string
for _, f := range funcs {
fnames := strings.Split(runtime.FuncForPC(reflect.ValueOf(*f).Pointer()).Name(), ".")
names = append(names, fnames[len(fnames)-1])
}
return reflect.DeepEqual(names, fnames)
}
2015-02-17 03:34:01 +03:00
func create(s *Scope) {}
func beforeCreate1(s *Scope) {}
func beforeCreate2(s *Scope) {}
func afterCreate1(s *Scope) {}
func afterCreate2(s *Scope) {}
2014-01-25 15:35:21 +04:00
func TestRegisterCallback(t *testing.T) {
2016-01-16 16:55:00 +03:00
var callback = &Callback{}
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
callback.Create().Register("before_create1", beforeCreate1)
callback.Create().Register("before_create2", beforeCreate2)
callback.Create().Register("create", create)
callback.Create().Register("after_create1", afterCreate1)
callback.Create().Register("after_create2", afterCreate2)
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
if !equalFuncs(callback.creates, []string{"beforeCreate1", "beforeCreate2", "create", "afterCreate1", "afterCreate2"}) {
2014-01-25 15:35:21 +04:00
t.Errorf("register callback")
}
}
2014-08-27 12:10:33 +04:00
func TestRegisterCallbackWithOrder(t *testing.T) {
2016-01-16 16:55:00 +03:00
var callback1 = &Callback{}
callback1.Create().Register("before_create1", beforeCreate1)
callback1.Create().Register("create", create)
callback1.Create().Register("after_create1", afterCreate1)
callback1.Create().Before("after_create1").Register("after_create2", afterCreate2)
if !equalFuncs(callback1.creates, []string{"beforeCreate1", "create", "afterCreate2", "afterCreate1"}) {
2014-08-27 12:10:33 +04:00
t.Errorf("register callback with order")
}
2016-01-16 16:55:00 +03:00
var callback2 = &Callback{}
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
callback2.Update().Register("create", create)
callback2.Update().Before("create").Register("before_create1", beforeCreate1)
callback2.Update().After("after_create2").Register("after_create1", afterCreate1)
callback2.Update().Before("before_create1").Register("before_create2", beforeCreate2)
callback2.Update().Register("after_create2", afterCreate2)
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
if !equalFuncs(callback2.updates, []string{"beforeCreate2", "beforeCreate1", "create", "afterCreate2", "afterCreate1"}) {
2014-01-25 15:35:21 +04:00
t.Errorf("register callback with order")
}
}
2014-08-27 12:10:33 +04:00
func TestRegisterCallbackWithComplexOrder(t *testing.T) {
2016-01-16 16:55:00 +03:00
var callback1 = &Callback{}
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
callback1.Query().Before("after_create1").After("before_create1").Register("create", create)
callback1.Query().Register("before_create1", beforeCreate1)
callback1.Query().Register("after_create1", afterCreate1)
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
if !equalFuncs(callback1.queries, []string{"beforeCreate1", "create", "afterCreate1"}) {
2014-01-25 15:35:21 +04:00
t.Errorf("register callback with order")
}
2016-01-16 16:55:00 +03:00
var callback2 = &Callback{}
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
callback2.Delete().Before("after_create1").After("before_create1").Register("create", create)
callback2.Delete().Before("create").Register("before_create1", beforeCreate1)
callback2.Delete().After("before_create1").Register("before_create2", beforeCreate2)
callback2.Delete().Register("after_create1", afterCreate1)
callback2.Delete().After("after_create1").Register("after_create2", afterCreate2)
2014-01-25 15:35:21 +04:00
2016-01-16 16:55:00 +03:00
if !equalFuncs(callback2.deletes, []string{"beforeCreate1", "beforeCreate2", "create", "afterCreate1", "afterCreate2"}) {
2014-01-25 15:35:21 +04:00
t.Errorf("register callback with order")
}
}
2014-01-25 16:04:01 +04:00
2015-02-17 03:34:01 +03:00
func replaceCreate(s *Scope) {}
2014-01-25 16:04:01 +04:00
func TestReplaceCallback(t *testing.T) {
2016-01-16 16:55:00 +03:00
var callback = &Callback{}
2014-01-25 16:04:01 +04:00
2016-01-16 16:55:00 +03:00
callback.Create().Before("after_create1").After("before_create1").Register("create", create)
callback.Create().Register("before_create1", beforeCreate1)
callback.Create().Register("after_create1", afterCreate1)
callback.Create().Replace("create", replaceCreate)
2014-01-25 16:04:01 +04:00
2016-01-16 16:55:00 +03:00
if !equalFuncs(callback.creates, []string{"beforeCreate1", "replaceCreate", "afterCreate1"}) {
2014-01-25 16:04:01 +04:00
t.Errorf("replace callback")
}
}
func TestRemoveCallback(t *testing.T) {
2016-01-16 16:55:00 +03:00
var callback = &Callback{}
2014-01-25 16:04:01 +04:00
2016-01-16 16:55:00 +03:00
callback.Create().Before("after_create1").After("before_create1").Register("create", create)
callback.Create().Register("before_create1", beforeCreate1)
callback.Create().Register("after_create1", afterCreate1)
callback.Create().Remove("create")
2014-01-25 16:04:01 +04:00
2016-01-16 16:55:00 +03:00
if !equalFuncs(callback.creates, []string{"beforeCreate1", "afterCreate1"}) {
2014-01-25 16:04:01 +04:00
t.Errorf("remove callback")
}
}