Merge pull request #28 from nashtsai/processors
add transactional tests for processors
This commit is contained in:
commit
d06120d7d1
341
base_test.go
341
base_test.go
|
@ -1770,6 +1770,7 @@ func testProcessors(engine *Engine, t *testing.T) {
|
|||
|
||||
p := &ProcessorsStruct{}
|
||||
|
||||
// test insert processors
|
||||
b4InsertFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.B4InsertViaExt = 1
|
||||
|
@ -1792,8 +1793,8 @@ func testProcessors(engine *Engine, t *testing.T) {
|
|||
panic(err)
|
||||
} else {
|
||||
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.AfterInsertedFlag == 0 { t.Error(errors.New("AfterInsertedFlag not set")) }
|
||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
||||
}
|
||||
|
||||
|
@ -1808,7 +1809,9 @@ func testProcessors(engine *Engine, t *testing.T) {
|
|||
if p2.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
// --
|
||||
|
||||
// test update processors
|
||||
b4UpdateFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.B4UpdateViaExt = 1
|
||||
|
@ -1845,11 +1848,13 @@ func testProcessors(engine *Engine, t *testing.T) {
|
|||
panic(err)
|
||||
} else {
|
||||
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.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
||||
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))) }
|
||||
if p2.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
||||
}
|
||||
// --
|
||||
|
||||
// test delete processors
|
||||
b4DeleteFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.B4DeleteViaExt = 1
|
||||
|
@ -1877,6 +1882,7 @@ func testProcessors(engine *Engine, t *testing.T) {
|
|||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
||||
if p.AfterDeletedViaExt == 0 { t.Error(errors.New("AfterDeletedViaExt not set")) }
|
||||
}
|
||||
// --
|
||||
|
||||
// test insert multi
|
||||
pslice := make([]*ProcessorsStruct, 0);
|
||||
|
@ -1909,6 +1915,331 @@ func testProcessors(engine *Engine, t *testing.T) {
|
|||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
}
|
||||
// --
|
||||
}
|
||||
|
||||
func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
// test insert processors with tx rollback
|
||||
session := tempEngine.NewSession()
|
||||
err = session.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := &ProcessorsStruct{}
|
||||
b4InsertFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.B4InsertViaExt = 1
|
||||
} else {
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
afterInsertFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.AfterInsertedViaExt = 1
|
||||
} else {
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
_, err = session.Before(b4InsertFunc).After(afterInsertFunc).Insert(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.AfterInsertedFlag != 0 { t.Error(errors.New("B4InsertFlag is set")) }
|
||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
|
||||
err = session.Rollback()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.AfterInsertedFlag != 0 { t.Error(errors.New("B4InsertFlag is set")) }
|
||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
session.Close()
|
||||
p2 := &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(p.Id).Get(p2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p2.Id > 0 { t.Error(errors.New("tx got committed upon insert!?")) }
|
||||
}
|
||||
// --
|
||||
|
||||
|
||||
// test insert processors with tx commit
|
||||
session = tempEngine.NewSession()
|
||||
err = session.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p = &ProcessorsStruct{}
|
||||
_, err = session.Before(b4InsertFunc).After(afterInsertFunc).Insert(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
||||
}
|
||||
|
||||
err = session.Commit()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
||||
if p.AfterInsertedFlag == 0 { t.Error(errors.New("AfterInsertedFlag not set")) }
|
||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
||||
if p.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
||||
}
|
||||
session.Close()
|
||||
p2 = &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(p.Id).Get(p2)
|
||||
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")) }
|
||||
}
|
||||
insertedId := p2.Id
|
||||
// --
|
||||
|
||||
|
||||
// test update processors with tx rollback
|
||||
session = tempEngine.NewSession()
|
||||
err = session.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
b4UpdateFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.B4UpdateViaExt = 1
|
||||
} else {
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
afterUpdateFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.AfterUpdatedViaExt = 1
|
||||
} else {
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
p = p2 // reset
|
||||
|
||||
_, err = session.Id(insertedId).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
||||
if p.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
||||
if p.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
||||
}
|
||||
err = session.Rollback()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
||||
if p.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
||||
if p.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
||||
}
|
||||
|
||||
session.Close()
|
||||
p2 = &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(insertedId).Get(p2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p2.B4UpdateFlag != 0 { t.Error(errors.New("B4UpdateFlag is set")) }
|
||||
if p2.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
||||
if p2.B4UpdateViaExt != 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
||||
if p2.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
||||
}
|
||||
// --
|
||||
|
||||
// test update processors with tx commit
|
||||
session = tempEngine.NewSession()
|
||||
err = session.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p = &ProcessorsStruct{}
|
||||
|
||||
_, err = session.Id(insertedId).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
||||
if p.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
||||
if p.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
||||
}
|
||||
err = session.Commit()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
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")) }
|
||||
}
|
||||
session.Close()
|
||||
p2 = &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(insertedId).Get(p2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
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")) }
|
||||
}
|
||||
// --
|
||||
|
||||
// test delete processors with tx rollback
|
||||
session = tempEngine.NewSession()
|
||||
err = session.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
b4DeleteFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.B4DeleteViaExt = 1
|
||||
} else {
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
afterDeleteFunc := func(bean interface{}) {
|
||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||
v.AfterDeletedViaExt = 1
|
||||
} else {
|
||||
t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
|
||||
}
|
||||
}
|
||||
|
||||
p = &ProcessorsStruct{} // reset
|
||||
|
||||
_, err = session.Id(insertedId).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
||||
if p.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
||||
if p.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
||||
}
|
||||
err = session.Rollback()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
||||
if p.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
||||
if p.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
||||
}
|
||||
session.Close()
|
||||
|
||||
p2 = &ProcessorsStruct{}
|
||||
_, err = tempEngine.Id(insertedId).Get(p2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p2.B4DeleteFlag != 0 { t.Error(errors.New("B4DeleteFlag is set")) }
|
||||
if p2.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
||||
if p2.B4DeleteViaExt != 0 { t.Error(errors.New("B4DeleteViaExt is set")) }
|
||||
if p2.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
||||
}
|
||||
// --
|
||||
|
||||
// test delete processors with tx commit
|
||||
session = tempEngine.NewSession()
|
||||
err = session.Begin()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p = &ProcessorsStruct{}
|
||||
|
||||
_, err = session.Id(insertedId).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
||||
if p.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
||||
if p.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
||||
}
|
||||
err = session.Commit()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
panic(err)
|
||||
} else {
|
||||
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")) }
|
||||
}
|
||||
session.Close()
|
||||
// --
|
||||
}
|
||||
|
||||
func testAll(engine *Engine, t *testing.T) {
|
||||
|
@ -2007,6 +2338,8 @@ func testAll2(engine *Engine, t *testing.T) {
|
|||
testPrefixTableName(engine, t)
|
||||
fmt.Println("-------------- processors --------------")
|
||||
testProcessors(engine, t)
|
||||
fmt.Println("-------------- processors TX --------------")
|
||||
testProcessorsTx(engine, t)
|
||||
fmt.Println("-------------- transaction --------------")
|
||||
transaction(engine, t)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue