processors_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "errors"
  7. "fmt"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestBefore_Get(t *testing.T) {
  12. assert.NoError(t, prepareEngine())
  13. type BeforeTable struct {
  14. Id int64
  15. Name string
  16. Val string `xorm:"-"`
  17. }
  18. assert.NoError(t, testEngine.Sync2(new(BeforeTable)))
  19. cnt, err := testEngine.Insert(&BeforeTable{
  20. Name: "test",
  21. })
  22. assert.NoError(t, err)
  23. assert.EqualValues(t, 1, cnt)
  24. var be BeforeTable
  25. has, err := testEngine.Before(func(bean interface{}) {
  26. bean.(*BeforeTable).Val = "val"
  27. }).Get(&be)
  28. assert.NoError(t, err)
  29. assert.Equal(t, true, has)
  30. assert.Equal(t, "val", be.Val)
  31. assert.Equal(t, "test", be.Name)
  32. }
  33. func TestBefore_Find(t *testing.T) {
  34. assert.NoError(t, prepareEngine())
  35. type BeforeTable2 struct {
  36. Id int64
  37. Name string
  38. Val string `xorm:"-"`
  39. }
  40. assert.NoError(t, testEngine.Sync2(new(BeforeTable2)))
  41. cnt, err := testEngine.Insert([]BeforeTable2{
  42. {Name: "test1"},
  43. {Name: "test2"},
  44. })
  45. assert.NoError(t, err)
  46. assert.EqualValues(t, 2, cnt)
  47. var be []BeforeTable2
  48. err = testEngine.Before(func(bean interface{}) {
  49. bean.(*BeforeTable2).Val = "val"
  50. }).Find(&be)
  51. assert.NoError(t, err)
  52. assert.Equal(t, 2, len(be))
  53. assert.Equal(t, "val", be[0].Val)
  54. assert.Equal(t, "test1", be[0].Name)
  55. assert.Equal(t, "val", be[1].Val)
  56. assert.Equal(t, "test2", be[1].Name)
  57. }
  58. type ProcessorsStruct struct {
  59. Id int64
  60. B4InsertFlag int
  61. AfterInsertedFlag int
  62. B4UpdateFlag int
  63. AfterUpdatedFlag int
  64. B4DeleteFlag int `xorm:"-"`
  65. AfterDeletedFlag int `xorm:"-"`
  66. BeforeSetFlag int `xorm:"-"`
  67. B4InsertViaExt int
  68. AfterInsertedViaExt int
  69. B4UpdateViaExt int
  70. AfterUpdatedViaExt int
  71. B4DeleteViaExt int `xorm:"-"`
  72. AfterDeletedViaExt int `xorm:"-"`
  73. AfterSetFlag int `xorm:"-"`
  74. }
  75. func (p *ProcessorsStruct) BeforeInsert() {
  76. p.B4InsertFlag = 1
  77. }
  78. func (p *ProcessorsStruct) BeforeUpdate() {
  79. p.B4UpdateFlag = 1
  80. }
  81. func (p *ProcessorsStruct) BeforeDelete() {
  82. p.B4DeleteFlag = 1
  83. }
  84. func (p *ProcessorsStruct) BeforeSet(col string, cell Cell) {
  85. p.BeforeSetFlag = p.BeforeSetFlag + 1
  86. }
  87. func (p *ProcessorsStruct) AfterInsert() {
  88. p.AfterInsertedFlag = 1
  89. }
  90. func (p *ProcessorsStruct) AfterUpdate() {
  91. p.AfterUpdatedFlag = 1
  92. }
  93. func (p *ProcessorsStruct) AfterDelete() {
  94. p.AfterDeletedFlag = 1
  95. }
  96. func (p *ProcessorsStruct) AfterSet(col string, cell Cell) {
  97. p.AfterSetFlag = p.AfterSetFlag + 1
  98. }
  99. func TestProcessors(t *testing.T) {
  100. assert.NoError(t, prepareEngine())
  101. err := testEngine.DropTables(&ProcessorsStruct{})
  102. if err != nil {
  103. t.Error(err)
  104. panic(err)
  105. }
  106. p := &ProcessorsStruct{}
  107. err = testEngine.CreateTables(&ProcessorsStruct{})
  108. if err != nil {
  109. t.Error(err)
  110. panic(err)
  111. }
  112. b4InsertFunc := func(bean interface{}) {
  113. if v, ok := (bean).(*ProcessorsStruct); ok {
  114. v.B4InsertViaExt = 1
  115. } else {
  116. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  117. }
  118. }
  119. afterInsertFunc := func(bean interface{}) {
  120. if v, ok := (bean).(*ProcessorsStruct); ok {
  121. v.AfterInsertedViaExt = 1
  122. } else {
  123. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  124. }
  125. }
  126. _, err = testEngine.Before(b4InsertFunc).After(afterInsertFunc).Insert(p)
  127. assert.NoError(t, err)
  128. assert.True(t, p.Id > 0, "Inserted ID not set")
  129. assert.True(t, p.B4InsertFlag > 0, "B4InsertFlag not set")
  130. assert.True(t, p.AfterInsertedFlag > 0, "B4InsertFlag not set")
  131. assert.True(t, p.B4InsertViaExt > 0, "B4InsertFlag not set")
  132. assert.True(t, p.AfterInsertedViaExt > 0, "AfterInsertedViaExt not set")
  133. p2 := &ProcessorsStruct{}
  134. has, err := testEngine.ID(p.Id).Get(p2)
  135. assert.NoError(t, err)
  136. assert.True(t, has)
  137. assert.True(t, p2.B4InsertFlag > 0, "B4InsertFlag not set")
  138. assert.True(t, p2.AfterInsertedFlag == 0, "AfterInsertedFlag is set")
  139. assert.True(t, p2.B4InsertViaExt > 0, "B4InsertViaExt not set")
  140. assert.True(t, p2.AfterInsertedViaExt == 0, "AfterInsertedViaExt is set")
  141. assert.True(t, p2.BeforeSetFlag == 9, fmt.Sprintf("BeforeSetFlag is %d not 9", p2.BeforeSetFlag))
  142. assert.True(t, p2.AfterSetFlag == 9, fmt.Sprintf("AfterSetFlag is %d not 9", p2.BeforeSetFlag))
  143. // --
  144. // test find processors
  145. var p2Find []*ProcessorsStruct
  146. err = testEngine.Find(&p2Find)
  147. assert.NoError(t, err)
  148. if len(p2Find) != 1 {
  149. err = errors.New("Should get 1")
  150. t.Error(err)
  151. }
  152. p21 := p2Find[0]
  153. if p21.B4InsertFlag == 0 {
  154. t.Error(errors.New("B4InsertFlag not set"))
  155. }
  156. if p21.AfterInsertedFlag != 0 {
  157. t.Error(errors.New("AfterInsertedFlag is set"))
  158. }
  159. if p21.B4InsertViaExt == 0 {
  160. t.Error(errors.New("B4InsertViaExt not set"))
  161. }
  162. if p21.AfterInsertedViaExt != 0 {
  163. t.Error(errors.New("AfterInsertedViaExt is set"))
  164. }
  165. if p21.BeforeSetFlag != 9 {
  166. t.Error(fmt.Errorf("BeforeSetFlag is %d not 9", p21.BeforeSetFlag))
  167. }
  168. if p21.AfterSetFlag != 9 {
  169. t.Error(fmt.Errorf("AfterSetFlag is %d not 9", p21.BeforeSetFlag))
  170. }
  171. // --
  172. // test find map processors
  173. var p2FindMap = make(map[int64]*ProcessorsStruct)
  174. err = testEngine.Find(&p2FindMap)
  175. assert.NoError(t, err)
  176. if len(p2FindMap) != 1 {
  177. err = errors.New("Should get 1")
  178. t.Error(err)
  179. }
  180. var p22 *ProcessorsStruct
  181. for _, v := range p2FindMap {
  182. p22 = v
  183. }
  184. if p22.B4InsertFlag == 0 {
  185. t.Error(errors.New("B4InsertFlag not set"))
  186. }
  187. if p22.AfterInsertedFlag != 0 {
  188. t.Error(errors.New("AfterInsertedFlag is set"))
  189. }
  190. if p22.B4InsertViaExt == 0 {
  191. t.Error(errors.New("B4InsertViaExt not set"))
  192. }
  193. if p22.AfterInsertedViaExt != 0 {
  194. t.Error(errors.New("AfterInsertedViaExt is set"))
  195. }
  196. if p22.BeforeSetFlag != 9 {
  197. t.Error(fmt.Errorf("BeforeSetFlag is %d not 9", p22.BeforeSetFlag))
  198. }
  199. if p22.AfterSetFlag != 9 {
  200. t.Error(fmt.Errorf("AfterSetFlag is %d not 9", p22.BeforeSetFlag))
  201. }
  202. // --
  203. // test update processors
  204. b4UpdateFunc := func(bean interface{}) {
  205. if v, ok := (bean).(*ProcessorsStruct); ok {
  206. v.B4UpdateViaExt = 1
  207. } else {
  208. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  209. }
  210. }
  211. afterUpdateFunc := func(bean interface{}) {
  212. if v, ok := (bean).(*ProcessorsStruct); ok {
  213. v.AfterUpdatedViaExt = 1
  214. } else {
  215. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  216. }
  217. }
  218. p = p2 // reset
  219. _, err = testEngine.Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
  220. assert.NoError(t, err)
  221. if p.B4UpdateFlag == 0 {
  222. t.Error(errors.New("B4UpdateFlag not set"))
  223. }
  224. if p.AfterUpdatedFlag == 0 {
  225. t.Error(errors.New("AfterUpdatedFlag not set"))
  226. }
  227. if p.B4UpdateViaExt == 0 {
  228. t.Error(errors.New("B4UpdateViaExt not set"))
  229. }
  230. if p.AfterUpdatedViaExt == 0 {
  231. t.Error(errors.New("AfterUpdatedViaExt not set"))
  232. }
  233. p2 = &ProcessorsStruct{}
  234. has, err = testEngine.ID(p.Id).Get(p2)
  235. assert.NoError(t, err)
  236. assert.True(t, has)
  237. if p2.B4UpdateFlag == 0 {
  238. t.Error(errors.New("B4UpdateFlag not set"))
  239. }
  240. if p2.AfterUpdatedFlag != 0 {
  241. t.Error(errors.New("AfterUpdatedFlag is set: " + string(p.AfterUpdatedFlag)))
  242. }
  243. if p2.B4UpdateViaExt == 0 {
  244. t.Error(errors.New("B4UpdateViaExt not set"))
  245. }
  246. if p2.AfterUpdatedViaExt != 0 {
  247. t.Error(errors.New("AfterUpdatedViaExt is set: " + string(p.AfterUpdatedViaExt)))
  248. }
  249. if p2.BeforeSetFlag != 9 {
  250. t.Error(fmt.Errorf("BeforeSetFlag is %d not 9", p2.BeforeSetFlag))
  251. }
  252. if p2.AfterSetFlag != 9 {
  253. t.Error(fmt.Errorf("AfterSetFlag is %d not 9", p2.BeforeSetFlag))
  254. }
  255. // --
  256. // test delete processors
  257. b4DeleteFunc := func(bean interface{}) {
  258. if v, ok := (bean).(*ProcessorsStruct); ok {
  259. v.B4DeleteViaExt = 1
  260. } else {
  261. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  262. }
  263. }
  264. afterDeleteFunc := func(bean interface{}) {
  265. if v, ok := (bean).(*ProcessorsStruct); ok {
  266. v.AfterDeletedViaExt = 1
  267. } else {
  268. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  269. }
  270. }
  271. p = p2 // reset
  272. _, err = testEngine.Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
  273. assert.NoError(t, err)
  274. if p.B4DeleteFlag == 0 {
  275. t.Error(errors.New("B4DeleteFlag not set"))
  276. }
  277. if p.AfterDeletedFlag == 0 {
  278. t.Error(errors.New("AfterDeletedFlag not set"))
  279. }
  280. if p.B4DeleteViaExt == 0 {
  281. t.Error(errors.New("B4DeleteViaExt not set"))
  282. }
  283. if p.AfterDeletedViaExt == 0 {
  284. t.Error(errors.New("AfterDeletedViaExt not set"))
  285. }
  286. // --
  287. // test insert multi
  288. pslice := make([]*ProcessorsStruct, 0)
  289. pslice = append(pslice, &ProcessorsStruct{})
  290. pslice = append(pslice, &ProcessorsStruct{})
  291. cnt, err := testEngine.Before(b4InsertFunc).After(afterInsertFunc).Insert(&pslice)
  292. assert.NoError(t, err)
  293. assert.EqualValues(t, 2, cnt, "incorrect insert count")
  294. for _, elem := range pslice {
  295. if elem.B4InsertFlag == 0 {
  296. t.Error(errors.New("B4InsertFlag not set"))
  297. }
  298. if elem.AfterInsertedFlag == 0 {
  299. t.Error(errors.New("B4InsertFlag not set"))
  300. }
  301. if elem.B4InsertViaExt == 0 {
  302. t.Error(errors.New("B4InsertFlag not set"))
  303. }
  304. if elem.AfterInsertedViaExt == 0 {
  305. t.Error(errors.New("AfterInsertedViaExt not set"))
  306. }
  307. }
  308. for _, elem := range pslice {
  309. p = &ProcessorsStruct{}
  310. _, err = testEngine.ID(elem.Id).Get(p)
  311. assert.NoError(t, err)
  312. if p2.B4InsertFlag == 0 {
  313. t.Error(errors.New("B4InsertFlag not set"))
  314. }
  315. if p2.AfterInsertedFlag != 0 {
  316. t.Error(errors.New("AfterInsertedFlag is set"))
  317. }
  318. if p2.B4InsertViaExt == 0 {
  319. t.Error(errors.New("B4InsertViaExt not set"))
  320. }
  321. if p2.AfterInsertedViaExt != 0 {
  322. t.Error(errors.New("AfterInsertedViaExt is set"))
  323. }
  324. if p2.BeforeSetFlag != 9 {
  325. t.Error(fmt.Errorf("BeforeSetFlag is %d not 9", p2.BeforeSetFlag))
  326. }
  327. if p2.AfterSetFlag != 9 {
  328. t.Error(fmt.Errorf("AfterSetFlag is %d not 9", p2.BeforeSetFlag))
  329. }
  330. }
  331. // --
  332. }
  333. func TestProcessorsTx(t *testing.T) {
  334. assert.NoError(t, prepareEngine())
  335. err := testEngine.DropTables(&ProcessorsStruct{})
  336. assert.NoError(t, err)
  337. err = testEngine.CreateTables(&ProcessorsStruct{})
  338. assert.NoError(t, err)
  339. // test insert processors with tx rollback
  340. session := testEngine.NewSession()
  341. defer session.Close()
  342. err = session.Begin()
  343. assert.NoError(t, err)
  344. p := &ProcessorsStruct{}
  345. b4InsertFunc := func(bean interface{}) {
  346. if v, ok := (bean).(*ProcessorsStruct); ok {
  347. v.B4InsertViaExt = 1
  348. } else {
  349. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  350. }
  351. }
  352. afterInsertFunc := func(bean interface{}) {
  353. if v, ok := (bean).(*ProcessorsStruct); ok {
  354. v.AfterInsertedViaExt = 1
  355. } else {
  356. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  357. }
  358. }
  359. _, err = session.Before(b4InsertFunc).After(afterInsertFunc).Insert(p)
  360. assert.NoError(t, err)
  361. if p.B4InsertFlag == 0 {
  362. t.Error(errors.New("B4InsertFlag not set"))
  363. }
  364. if p.AfterInsertedFlag != 0 {
  365. t.Error(errors.New("B4InsertFlag is set"))
  366. }
  367. if p.B4InsertViaExt == 0 {
  368. t.Error(errors.New("B4InsertViaExt not set"))
  369. }
  370. if p.AfterInsertedViaExt != 0 {
  371. t.Error(errors.New("AfterInsertedViaExt is set"))
  372. }
  373. err = session.Rollback()
  374. assert.NoError(t, err)
  375. if p.B4InsertFlag == 0 {
  376. t.Error(errors.New("B4InsertFlag not set"))
  377. }
  378. if p.AfterInsertedFlag != 0 {
  379. t.Error(errors.New("B4InsertFlag is set"))
  380. }
  381. if p.B4InsertViaExt == 0 {
  382. t.Error(errors.New("B4InsertViaExt not set"))
  383. }
  384. if p.AfterInsertedViaExt != 0 {
  385. t.Error(errors.New("AfterInsertedViaExt is set"))
  386. }
  387. session.Close()
  388. p2 := &ProcessorsStruct{}
  389. _, err = testEngine.ID(p.Id).Get(p2)
  390. assert.NoError(t, err)
  391. if p2.Id > 0 {
  392. err = errors.New("tx got committed upon insert!?")
  393. t.Error(err)
  394. panic(err)
  395. }
  396. // --
  397. // test insert processors with tx commit
  398. session = testEngine.NewSession()
  399. defer session.Close()
  400. err = session.Begin()
  401. assert.NoError(t, err)
  402. p = &ProcessorsStruct{}
  403. _, err = session.Before(b4InsertFunc).After(afterInsertFunc).Insert(p)
  404. assert.NoError(t, err)
  405. if p.B4InsertFlag == 0 {
  406. t.Error(errors.New("B4InsertFlag not set"))
  407. }
  408. if p.AfterInsertedFlag != 0 {
  409. t.Error(errors.New("AfterInsertedFlag is set"))
  410. }
  411. if p.B4InsertViaExt == 0 {
  412. t.Error(errors.New("B4InsertViaExt not set"))
  413. }
  414. if p.AfterInsertedViaExt != 0 {
  415. t.Error(errors.New("AfterInsertedViaExt is set"))
  416. }
  417. err = session.Commit()
  418. assert.NoError(t, err)
  419. if p.B4InsertFlag == 0 {
  420. t.Error(errors.New("B4InsertFlag not set"))
  421. }
  422. if p.AfterInsertedFlag == 0 {
  423. t.Error(errors.New("AfterInsertedFlag not set"))
  424. }
  425. if p.B4InsertViaExt == 0 {
  426. t.Error(errors.New("B4InsertViaExt not set"))
  427. }
  428. if p.AfterInsertedViaExt == 0 {
  429. t.Error(errors.New("AfterInsertedViaExt not set"))
  430. }
  431. session.Close()
  432. p2 = &ProcessorsStruct{}
  433. _, err = testEngine.ID(p.Id).Get(p2)
  434. assert.NoError(t, err)
  435. if p2.B4InsertFlag == 0 {
  436. t.Error(errors.New("B4InsertFlag not set"))
  437. }
  438. if p2.AfterInsertedFlag != 0 {
  439. t.Error(errors.New("AfterInsertedFlag is set"))
  440. }
  441. if p2.B4InsertViaExt == 0 {
  442. t.Error(errors.New("B4InsertViaExt not set"))
  443. }
  444. if p2.AfterInsertedViaExt != 0 {
  445. t.Error(errors.New("AfterInsertedViaExt is set"))
  446. }
  447. insertedId := p2.Id
  448. // --
  449. // test update processors with tx rollback
  450. session = testEngine.NewSession()
  451. defer session.Close()
  452. err = session.Begin()
  453. assert.NoError(t, err)
  454. b4UpdateFunc := func(bean interface{}) {
  455. if v, ok := (bean).(*ProcessorsStruct); ok {
  456. v.B4UpdateViaExt = 1
  457. } else {
  458. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  459. }
  460. }
  461. afterUpdateFunc := func(bean interface{}) {
  462. if v, ok := (bean).(*ProcessorsStruct); ok {
  463. v.AfterUpdatedViaExt = 1
  464. } else {
  465. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  466. }
  467. }
  468. p = p2 // reset
  469. _, err = session.ID(insertedId).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
  470. assert.NoError(t, err)
  471. if p.B4UpdateFlag == 0 {
  472. t.Error(errors.New("B4UpdateFlag not set"))
  473. }
  474. if p.AfterUpdatedFlag != 0 {
  475. t.Error(errors.New("AfterUpdatedFlag is set"))
  476. }
  477. if p.B4UpdateViaExt == 0 {
  478. t.Error(errors.New("B4UpdateViaExt not set"))
  479. }
  480. if p.AfterUpdatedViaExt != 0 {
  481. t.Error(errors.New("AfterUpdatedViaExt is set"))
  482. }
  483. err = session.Rollback()
  484. assert.NoError(t, err)
  485. if p.B4UpdateFlag == 0 {
  486. t.Error(errors.New("B4UpdateFlag not set"))
  487. }
  488. if p.AfterUpdatedFlag != 0 {
  489. t.Error(errors.New("AfterUpdatedFlag is set"))
  490. }
  491. if p.B4UpdateViaExt == 0 {
  492. t.Error(errors.New("B4UpdateViaExt not set"))
  493. }
  494. if p.AfterUpdatedViaExt != 0 {
  495. t.Error(errors.New("AfterUpdatedViaExt is set"))
  496. }
  497. session.Close()
  498. p2 = &ProcessorsStruct{}
  499. _, err = testEngine.ID(insertedId).Get(p2)
  500. assert.NoError(t, err)
  501. if p2.B4UpdateFlag != 0 {
  502. t.Error(errors.New("B4UpdateFlag is set"))
  503. }
  504. if p2.AfterUpdatedFlag != 0 {
  505. t.Error(errors.New("AfterUpdatedFlag is set"))
  506. }
  507. if p2.B4UpdateViaExt != 0 {
  508. t.Error(errors.New("B4UpdateViaExt not set"))
  509. }
  510. if p2.AfterUpdatedViaExt != 0 {
  511. t.Error(errors.New("AfterUpdatedViaExt is set"))
  512. }
  513. // --
  514. // test update processors with tx rollback
  515. session = testEngine.NewSession()
  516. defer session.Close()
  517. err = session.Begin()
  518. assert.NoError(t, err)
  519. p = &ProcessorsStruct{Id: insertedId}
  520. _, err = session.Update(p)
  521. assert.NoError(t, err)
  522. if p.B4UpdateFlag == 0 {
  523. t.Error(errors.New("B4UpdateFlag not set"))
  524. }
  525. if p.AfterUpdatedFlag != 0 {
  526. t.Error(errors.New("AfterUpdatedFlag is set"))
  527. }
  528. err = session.Commit()
  529. assert.NoError(t, err)
  530. if p.B4UpdateFlag == 0 {
  531. t.Error(errors.New("B4UpdateFlag not set"))
  532. }
  533. if p.AfterUpdatedFlag == 0 {
  534. t.Error(errors.New("AfterUpdatedFlag not set"))
  535. }
  536. if p.AfterDeletedFlag != 0 {
  537. t.Error(errors.New("AfterDeletedFlag set"))
  538. }
  539. if p.AfterInsertedFlag != 0 {
  540. t.Error(errors.New("AfterInsertedFlag set"))
  541. }
  542. session.Close()
  543. // test update processors with tx commit
  544. session = testEngine.NewSession()
  545. defer session.Close()
  546. err = session.Begin()
  547. assert.NoError(t, err)
  548. p = &ProcessorsStruct{}
  549. _, err = session.ID(insertedId).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
  550. assert.NoError(t, err)
  551. if p.B4UpdateFlag == 0 {
  552. t.Error(errors.New("B4UpdateFlag not set"))
  553. }
  554. if p.AfterUpdatedFlag != 0 {
  555. t.Error(errors.New("AfterUpdatedFlag is set"))
  556. }
  557. if p.B4UpdateViaExt == 0 {
  558. t.Error(errors.New("B4UpdateViaExt not set"))
  559. }
  560. if p.AfterUpdatedViaExt != 0 {
  561. t.Error(errors.New("AfterUpdatedViaExt is set"))
  562. }
  563. err = session.Commit()
  564. assert.NoError(t, err)
  565. if p.B4UpdateFlag == 0 {
  566. t.Error(errors.New("B4UpdateFlag not set"))
  567. }
  568. if p.AfterUpdatedFlag == 0 {
  569. t.Error(errors.New("AfterUpdatedFlag not set"))
  570. }
  571. if p.B4UpdateViaExt == 0 {
  572. t.Error(errors.New("B4UpdateViaExt not set"))
  573. }
  574. if p.AfterUpdatedViaExt == 0 {
  575. t.Error(errors.New("AfterUpdatedViaExt not set"))
  576. }
  577. session.Close()
  578. p2 = &ProcessorsStruct{}
  579. _, err = testEngine.ID(insertedId).Get(p2)
  580. assert.NoError(t, err)
  581. if p.B4UpdateFlag == 0 {
  582. t.Error(errors.New("B4UpdateFlag not set"))
  583. }
  584. if p.AfterUpdatedFlag == 0 {
  585. t.Error(errors.New("AfterUpdatedFlag not set"))
  586. }
  587. if p.B4UpdateViaExt == 0 {
  588. t.Error(errors.New("B4UpdateViaExt not set"))
  589. }
  590. if p.AfterUpdatedViaExt == 0 {
  591. t.Error(errors.New("AfterUpdatedViaExt not set"))
  592. }
  593. // --
  594. // test delete processors with tx rollback
  595. session = testEngine.NewSession()
  596. defer session.Close()
  597. err = session.Begin()
  598. assert.NoError(t, err)
  599. b4DeleteFunc := func(bean interface{}) {
  600. if v, ok := (bean).(*ProcessorsStruct); ok {
  601. v.B4DeleteViaExt = 1
  602. } else {
  603. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  604. }
  605. }
  606. afterDeleteFunc := func(bean interface{}) {
  607. if v, ok := (bean).(*ProcessorsStruct); ok {
  608. v.AfterDeletedViaExt = 1
  609. } else {
  610. t.Error(errors.New("cast to ProcessorsStruct failed, how can this be!?"))
  611. }
  612. }
  613. p = &ProcessorsStruct{} // reset
  614. _, err = session.ID(insertedId).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
  615. assert.NoError(t, err)
  616. if p.B4DeleteFlag == 0 {
  617. t.Error(errors.New("B4DeleteFlag not set"))
  618. }
  619. if p.AfterDeletedFlag != 0 {
  620. t.Error(errors.New("AfterDeletedFlag is set"))
  621. }
  622. if p.B4DeleteViaExt == 0 {
  623. t.Error(errors.New("B4DeleteViaExt not set"))
  624. }
  625. if p.AfterDeletedViaExt != 0 {
  626. t.Error(errors.New("AfterDeletedViaExt is set"))
  627. }
  628. err = session.Rollback()
  629. assert.NoError(t, err)
  630. if p.B4DeleteFlag == 0 {
  631. t.Error(errors.New("B4DeleteFlag not set"))
  632. }
  633. if p.AfterDeletedFlag != 0 {
  634. t.Error(errors.New("AfterDeletedFlag is set"))
  635. }
  636. if p.B4DeleteViaExt == 0 {
  637. t.Error(errors.New("B4DeleteViaExt not set"))
  638. }
  639. if p.AfterDeletedViaExt != 0 {
  640. t.Error(errors.New("AfterDeletedViaExt is set"))
  641. }
  642. session.Close()
  643. p2 = &ProcessorsStruct{}
  644. _, err = testEngine.ID(insertedId).Get(p2)
  645. assert.NoError(t, err)
  646. if p2.B4DeleteFlag != 0 {
  647. t.Error(errors.New("B4DeleteFlag is set"))
  648. }
  649. if p2.AfterDeletedFlag != 0 {
  650. t.Error(errors.New("AfterDeletedFlag is set"))
  651. }
  652. if p2.B4DeleteViaExt != 0 {
  653. t.Error(errors.New("B4DeleteViaExt is set"))
  654. }
  655. if p2.AfterDeletedViaExt != 0 {
  656. t.Error(errors.New("AfterDeletedViaExt is set"))
  657. }
  658. // --
  659. // test delete processors with tx commit
  660. session = testEngine.NewSession()
  661. defer session.Close()
  662. err = session.Begin()
  663. assert.NoError(t, err)
  664. p = &ProcessorsStruct{}
  665. _, err = session.ID(insertedId).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
  666. assert.NoError(t, err)
  667. if p.B4DeleteFlag == 0 {
  668. t.Error(errors.New("B4DeleteFlag not set"))
  669. }
  670. if p.AfterDeletedFlag != 0 {
  671. t.Error(errors.New("AfterDeletedFlag is set"))
  672. }
  673. if p.B4DeleteViaExt == 0 {
  674. t.Error(errors.New("B4DeleteViaExt not set"))
  675. }
  676. if p.AfterDeletedViaExt != 0 {
  677. t.Error(errors.New("AfterDeletedViaExt is set"))
  678. }
  679. err = session.Commit()
  680. assert.NoError(t, err)
  681. if p.B4DeleteFlag == 0 {
  682. t.Error(errors.New("B4DeleteFlag not set"))
  683. }
  684. if p.AfterDeletedFlag == 0 {
  685. t.Error(errors.New("AfterDeletedFlag not set"))
  686. }
  687. if p.B4DeleteViaExt == 0 {
  688. t.Error(errors.New("B4DeleteViaExt not set"))
  689. }
  690. if p.AfterDeletedViaExt == 0 {
  691. t.Error(errors.New("AfterDeletedViaExt not set"))
  692. }
  693. session.Close()
  694. // test delete processors with tx commit
  695. session = testEngine.NewSession()
  696. defer session.Close()
  697. err = session.Begin()
  698. assert.NoError(t, err)
  699. p = &ProcessorsStruct{Id: insertedId}
  700. _, err = session.Delete(p)
  701. assert.NoError(t, err)
  702. if p.B4DeleteFlag == 0 {
  703. t.Error(errors.New("B4DeleteFlag not set"))
  704. }
  705. if p.AfterDeletedFlag != 0 {
  706. t.Error(errors.New("AfterDeletedFlag is set"))
  707. }
  708. err = session.Commit()
  709. assert.NoError(t, err)
  710. if p.B4DeleteFlag == 0 {
  711. t.Error(errors.New("B4DeleteFlag not set"))
  712. }
  713. if p.AfterDeletedFlag == 0 {
  714. t.Error(errors.New("AfterDeletedFlag not set"))
  715. }
  716. if p.AfterInsertedFlag != 0 {
  717. t.Error(errors.New("AfterInsertedFlag set"))
  718. }
  719. if p.AfterUpdatedFlag != 0 {
  720. t.Error(errors.New("AfterUpdatedFlag set"))
  721. }
  722. session.Close()
  723. // --
  724. }
  725. type AfterLoadStructA struct {
  726. Id int64
  727. Content string
  728. }
  729. type AfterLoadStructB struct {
  730. Id int64
  731. Content string
  732. AId int64
  733. A AfterLoadStructA `xorm:"-"`
  734. Err error `xorm:"-"`
  735. }
  736. func (s *AfterLoadStructB) AfterLoad(session *Session) {
  737. has, err := session.ID(s.AId).NoAutoCondition().Get(&s.A)
  738. if err != nil {
  739. s.Err = err
  740. return
  741. }
  742. if !has {
  743. s.Err = ErrNotExist
  744. }
  745. }
  746. func TestAfterLoadProcessor(t *testing.T) {
  747. assert.NoError(t, prepareEngine())
  748. assertSync(t, new(AfterLoadStructA), new(AfterLoadStructB))
  749. var a = AfterLoadStructA{
  750. Content: "testa",
  751. }
  752. _, err := testEngine.Insert(&a)
  753. assert.NoError(t, err)
  754. var b = AfterLoadStructB{
  755. Content: "testb",
  756. AId: a.Id,
  757. }
  758. _, err = testEngine.Insert(&b)
  759. assert.NoError(t, err)
  760. var b2 AfterLoadStructB
  761. has, err := testEngine.ID(b.Id).Get(&b2)
  762. assert.NoError(t, err)
  763. assert.True(t, has)
  764. assert.EqualValues(t, a.Id, b2.A.Id)
  765. assert.EqualValues(t, a.Content, b2.A.Content)
  766. assert.NoError(t, b2.Err)
  767. b.Id = 0
  768. _, err = testEngine.Insert(&b)
  769. assert.NoError(t, err)
  770. var bs []AfterLoadStructB
  771. err = testEngine.Find(&bs)
  772. assert.NoError(t, err)
  773. assert.EqualValues(t, 2, len(bs))
  774. for i := 0; i < len(bs); i++ {
  775. assert.EqualValues(t, a.Id, bs[i].A.Id)
  776. assert.EqualValues(t, a.Content, bs[i].A.Content)
  777. assert.NoError(t, bs[i].Err)
  778. }
  779. }
  780. type AfterInsertStruct struct {
  781. Id int64
  782. }
  783. func (a *AfterInsertStruct) AfterInsert() {
  784. if a.Id == 0 {
  785. panic("a.Id")
  786. }
  787. }
  788. func TestAfterInsert(t *testing.T) {
  789. assert.NoError(t, prepareEngine())
  790. assertSync(t, new(AfterInsertStruct))
  791. _, err := testEngine.Insert(&AfterInsertStruct{})
  792. assert.NoError(t, err)
  793. }