session_tx_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "fmt"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/xormplus/core"
  11. )
  12. func TestTransaction(t *testing.T) {
  13. assert.NoError(t, prepareEngine())
  14. assertSync(t, new(Userinfo))
  15. counter := func() {
  16. total, err := testEngine.Count(&Userinfo{})
  17. if err != nil {
  18. t.Error(err)
  19. }
  20. fmt.Printf("----now total %v records\n", total)
  21. }
  22. counter()
  23. //defer counter()
  24. session := testEngine.NewSession()
  25. defer session.Close()
  26. err := session.Begin()
  27. assert.NoError(t, err)
  28. user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
  29. _, err = session.Insert(&user1)
  30. assert.NoError(t, err)
  31. user2 := Userinfo{Username: "yyy"}
  32. _, err = session.Where("(id) = ?", 0).Update(&user2)
  33. assert.NoError(t, err)
  34. _, err = session.Delete(&user2)
  35. assert.NoError(t, err)
  36. err = session.Commit()
  37. assert.NoError(t, err)
  38. }
  39. func TestCombineTransaction(t *testing.T) {
  40. assert.NoError(t, prepareEngine())
  41. assertSync(t, new(Userinfo))
  42. counter := func() {
  43. total, err := testEngine.Count(&Userinfo{})
  44. if err != nil {
  45. t.Error(err)
  46. }
  47. fmt.Printf("----now total %v records\n", total)
  48. }
  49. counter()
  50. //defer counter()
  51. session := testEngine.NewSession()
  52. defer session.Close()
  53. err := session.Begin()
  54. assert.NoError(t, err)
  55. user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
  56. _, err = session.Insert(&user1)
  57. assert.NoError(t, err)
  58. user2 := Userinfo{Username: "zzz"}
  59. _, err = session.Where("id = ?", 0).Update(&user2)
  60. assert.NoError(t, err)
  61. _, err = session.Exec("delete from "+testEngine.TableName("userinfo", true)+" where username = ?", user2.Username)
  62. assert.NoError(t, err)
  63. err = session.Commit()
  64. assert.NoError(t, err)
  65. }
  66. func TestCombineTransactionSameMapper(t *testing.T) {
  67. assert.NoError(t, prepareEngine())
  68. oldMapper := testEngine.GetColumnMapper()
  69. testEngine.UnMapType(rValue(new(Userinfo)).Type())
  70. testEngine.SetMapper(core.SameMapper{})
  71. defer func() {
  72. testEngine.UnMapType(rValue(new(Userinfo)).Type())
  73. testEngine.SetMapper(oldMapper)
  74. }()
  75. assertSync(t, new(Userinfo))
  76. counter := func() {
  77. total, err := testEngine.Count(&Userinfo{})
  78. if err != nil {
  79. t.Error(err)
  80. }
  81. fmt.Printf("----now total %v records\n", total)
  82. }
  83. counter()
  84. defer counter()
  85. session := testEngine.NewSession()
  86. defer session.Close()
  87. err := session.Begin()
  88. assert.NoError(t, err)
  89. user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
  90. _, err = session.Insert(&user1)
  91. assert.NoError(t, err)
  92. user2 := Userinfo{Username: "zzz"}
  93. _, err = session.Where("(id) = ?", 0).Update(&user2)
  94. assert.NoError(t, err)
  95. _, err = session.Exec("delete from "+testEngine.TableName("`Userinfo`", true)+" where `Username` = ?", user2.Username)
  96. assert.NoError(t, err)
  97. err = session.Commit()
  98. assert.NoError(t, err)
  99. }
  100. func TestMultipleTransaction(t *testing.T) {
  101. assert.NoError(t, prepareEngine())
  102. type MultipleTransaction struct {
  103. Id int64
  104. Name string
  105. }
  106. assertSync(t, new(MultipleTransaction))
  107. session := testEngine.NewSession()
  108. defer session.Close()
  109. err := session.Begin()
  110. assert.NoError(t, err)
  111. m1 := MultipleTransaction{Name: "xiaoxiao2"}
  112. _, err = session.Insert(&m1)
  113. assert.NoError(t, err)
  114. user2 := MultipleTransaction{Name: "zzz"}
  115. _, err = session.Where("id = ?", 0).Update(&user2)
  116. assert.NoError(t, err)
  117. err = session.Commit()
  118. assert.NoError(t, err)
  119. var ms []MultipleTransaction
  120. err = session.Find(&ms)
  121. assert.NoError(t, err)
  122. assert.EqualValues(t, 1, len(ms))
  123. err = session.Begin()
  124. assert.NoError(t, err)
  125. _, err = session.Where("id=?", m1.Id).Delete(new(MultipleTransaction))
  126. assert.NoError(t, err)
  127. err = session.Commit()
  128. assert.NoError(t, err)
  129. ms = make([]MultipleTransaction, 0)
  130. err = session.Find(&ms)
  131. assert.NoError(t, err)
  132. assert.EqualValues(t, 0, len(ms))
  133. err = session.Begin()
  134. assert.NoError(t, err)
  135. _, err = session.Insert(&MultipleTransaction{
  136. Name: "ssss",
  137. })
  138. assert.NoError(t, err)
  139. err = session.Rollback()
  140. assert.NoError(t, err)
  141. ms = make([]MultipleTransaction, 0)
  142. err = session.Find(&ms)
  143. assert.NoError(t, err)
  144. assert.EqualValues(t, 0, len(ms))
  145. }