add processors tests
This commit is contained in:
parent
e9a5dddbef
commit
fe91775981
182
base_test.go
182
base_test.go
|
@ -1709,160 +1709,206 @@ func testPrefixTableName(engine *Engine, t *testing.T) {
|
|||
type ProcessorsStruct struct {
|
||||
Id int64
|
||||
|
||||
B4InsertFlag bool
|
||||
AfterInsertedFlag bool
|
||||
B4UpdateFlag bool
|
||||
AfterUpdatedFlag bool
|
||||
B4DeleteFlag bool `xorm:"-"`
|
||||
AfterDeletedFlag bool `xorm:"-"`
|
||||
B4InsertFlag int
|
||||
AfterInsertedFlag int
|
||||
B4UpdateFlag int
|
||||
AfterUpdatedFlag int
|
||||
B4DeleteFlag int `xorm:"-"`
|
||||
AfterDeletedFlag int `xorm:"-"`
|
||||
|
||||
B4InsertViaExt bool
|
||||
AfterInsertedViaExt bool
|
||||
B4UpdateViaExt bool
|
||||
AfterUpdatedViaExt bool
|
||||
B4DeleteViaExt bool `xorm:"-"`
|
||||
AfterDeletedViaExt bool `xorm:"-"`
|
||||
B4InsertViaExt int
|
||||
AfterInsertedViaExt int
|
||||
B4UpdateViaExt int
|
||||
AfterUpdatedViaExt int
|
||||
B4DeleteViaExt int `xorm:"-"`
|
||||
AfterDeletedViaExt int `xorm:"-"`
|
||||
}
|
||||
|
||||
func (p *ProcessorsStruct) BeforeInsert() {
|
||||
p.B4InsertFlag = true
|
||||
p.B4InsertFlag = 1
|
||||
}
|
||||
|
||||
func (p *ProcessorsStruct) BeforeUpdate() {
|
||||
p.B4UpdateFlag = true
|
||||
p.B4UpdateFlag = 1
|
||||
}
|
||||
|
||||
func (p *ProcessorsStruct) BeforeDelete() {
|
||||
p.B4DeleteFlag = true
|
||||
p.B4DeleteFlag = 1
|
||||
}
|
||||
|
||||
func (p *ProcessorsStruct) AfterInsert() {
|
||||
p.AfterInsertedFlag = true
|
||||
p.AfterInsertedFlag = 1
|
||||
}
|
||||
|
||||
func (p *ProcessorsStruct) AfterUpdate() {
|
||||
p.AfterUpdatedFlag = true
|
||||
p.AfterUpdatedFlag = 1
|
||||
}
|
||||
|
||||
func (p *ProcessorsStruct) AfterDelete() {
|
||||
p.AfterDeletedFlag = true
|
||||
p.AfterDeletedFlag = 1
|
||||
}
|
||||
|
||||
func testProcessors(engine *Engine, t *testing.T) {
|
||||
err := engine.Sync(&ProcessorsStruct{})
|
||||
tempEngine, err := NewEngine(engine.DriverName, engine.DataSourceName)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
tempEngine.ShowSQL = true
|
||||
err = tempEngine.DropTables(&ProcessorsStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = tempEngine.CreateTables(&ProcessorsStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := &ProcessorsStruct{}
|
||||
|
||||
b4InsertFunc := func(bean interface{}) {
|
||||
if v, ok := interface{}(bean).(ProcessorsStruct); ok {
|
||||
v.B4InsertViaExt = true
|
||||
if v, ok := interface{}(bean).(*ProcessorsStruct); ok {
|
||||
v.B4InsertViaExt = 1
|
||||
} else {
|
||||
t.Fail()
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
afterInsertFunc := func(bean interface{}) {
|
||||
if v, ok := interface{}(bean).(ProcessorsStruct); ok {
|
||||
v.AfterInsertedViaExt = true
|
||||
if v, ok := interface{}(bean).(*ProcessorsStruct); ok {
|
||||
v.AfterInsertedViaExt = 1
|
||||
} else {
|
||||
t.Fail()
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
_, err = engine.BeforeInsert(b4InsertFunc).AfterInsert(afterInsertFunc).Insert(p)
|
||||
_, err = tempEngine.Before(b4InsertFunc).After(afterInsertFunc).Insert(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if !p.B4InsertFlag { t.Fail() }
|
||||
if !p.AfterInsertedFlag { t.Fail() }
|
||||
if !p.B4InsertViaExt { t.Fail() }
|
||||
if !p.AfterInsertedViaExt { t.Fail() }
|
||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.AfterInsertedFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
||||
}
|
||||
|
||||
p2 := &ProcessorsStruct{}
|
||||
_, err = engine.Id(p.Id).Get(p2)
|
||||
_, err = tempEngine.Id(p.Id).Get(p2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if !p.B4InsertFlag { t.Fail() }
|
||||
if p.AfterInsertedFlag { t.Fail() }
|
||||
if !p.B4InsertViaExt { t.Fail() }
|
||||
if p.AfterInsertedViaExt { t.Fail() }
|
||||
if p2.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p2.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
||||
if p2.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
|
||||
b4UpdateFunc := func(bean interface{}) {
|
||||
if v, ok := interface{}(bean).(ProcessorsStruct); ok {
|
||||
v.B4UpdateViaExt = true
|
||||
if v, ok := interface{}(bean).(*ProcessorsStruct); ok {
|
||||
v.B4UpdateViaExt = 1
|
||||
} else {
|
||||
t.Fail()
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
afterUpdateFunc := func(bean interface{}) {
|
||||
if v, ok := interface{}(bean).(ProcessorsStruct); ok {
|
||||
v.AfterUpdatedViaExt = true
|
||||
if v, ok := interface{}(bean).(*ProcessorsStruct); ok {
|
||||
v.AfterUpdatedViaExt = 1
|
||||
} else {
|
||||
t.Fail()
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
p = p2 // reset
|
||||
|
||||
_, err = engine.BeforeUpdate(b4UpdateFunc).AfterUpdate(afterUpdateFunc).Update(p)
|
||||
_, err = tempEngine.Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if !p.B4UpdateFlag { t.Fail() }
|
||||
if !p.AfterUpdatedFlag { t.Fail() }
|
||||
if !p.B4UpdateViaExt { t.Fail() }
|
||||
if !p.AfterUpdatedViaExt { t.Fail() }
|
||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
||||
if p.AfterUpdatedFlag == 0 { t.Error(errors.New("AfterUpdatedFlag not set")) }
|
||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
||||
if p.AfterUpdatedViaExt == 0 { t.Error(errors.New("AfterUpdatedViaExt not set")) }
|
||||
}
|
||||
|
||||
_, err = engine.Id(p.Id).Get(p2)
|
||||
|
||||
p2 = &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(p.Id).Get(p2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if !p.B4UpdateFlag { t.Fail() }
|
||||
if p.AfterUpdatedFlag { t.Fail() }
|
||||
if !p.B4UpdateViaExt { t.Fail() }
|
||||
if p.AfterUpdatedViaExt { t.Fail() }
|
||||
if p2.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
||||
if p2.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set: " + string(p.AfterUpdatedFlag))) }
|
||||
if p2.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
||||
if p2.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set: " + string(p.AfterUpdatedViaExt))) }
|
||||
}
|
||||
|
||||
b4DeleteFunc := func(bean interface{}) {
|
||||
if v, ok := interface{}(bean).(ProcessorsStruct); ok {
|
||||
v.B4DeleteViaExt = true
|
||||
if v, ok := interface{}(bean).(*ProcessorsStruct); ok {
|
||||
v.B4DeleteViaExt = 1
|
||||
} else {
|
||||
t.Fail()
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
afterDeleteFunc := func(bean interface{}) {
|
||||
if v, ok := interface{}(bean).(ProcessorsStruct); ok {
|
||||
v.AfterDeletedViaExt = true
|
||||
if v, ok := interface{}(bean).(*ProcessorsStruct); ok {
|
||||
v.AfterDeletedViaExt = 1
|
||||
} else {
|
||||
t.Fail()
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_, err = engine.BeforeDelete(b4DeleteFunc).AfterDelete(afterDeleteFunc).Delete(p)
|
||||
|
||||
p = p2 // reset
|
||||
_, err = tempEngine.Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if !p.B4DeleteFlag { t.Fail() }
|
||||
if !p.AfterDeletedFlag { t.Fail() }
|
||||
if !p.B4DeleteViaExt { t.Fail() }
|
||||
if !p.AfterDeletedViaExt { t.Fail() }
|
||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
||||
if p.AfterDeletedFlag == 0 { t.Error(errors.New("AfterDeletedFlag not set")) }
|
||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
||||
if p.AfterDeletedViaExt == 0 { t.Error(errors.New("AfterDeletedViaExt not set")) }
|
||||
}
|
||||
|
||||
// !nashtsai! TODO insert many beans tests
|
||||
// test insert multi
|
||||
pslice := make([]*ProcessorsStruct, 0);
|
||||
pslice = append(pslice, &ProcessorsStruct{})
|
||||
pslice = append(pslice, &ProcessorsStruct{})
|
||||
cnt, err := tempEngine.Before(b4InsertFunc).After(afterInsertFunc).Insert(&pslice)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if cnt != 2 { t.Error(errors.New("incorrect insert count")) }
|
||||
for _, elem := range pslice {
|
||||
if elem.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if elem.AfterInsertedFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if elem.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if elem.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
||||
}
|
||||
}
|
||||
|
||||
for _, elem := range pslice {
|
||||
p = &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(elem.Id).Get(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p2.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p2.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
||||
if p2.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testAll(engine *Engine, t *testing.T) {
|
||||
|
@ -1959,8 +2005,8 @@ func testAll2(engine *Engine, t *testing.T) {
|
|||
testTime(engine, t)
|
||||
fmt.Println("-------------- testPrefixTableName --------------")
|
||||
testPrefixTableName(engine, t)
|
||||
fmt.Println("-------------- transaction --------------")
|
||||
transaction(engine, t)
|
||||
fmt.Println("-------------- processors --------------")
|
||||
testProcessors(engine, t)
|
||||
fmt.Println("-------------- transaction --------------")
|
||||
transaction(engine, t)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue