session_delete_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/xormplus/core"
  10. )
  11. func TestDelete(t *testing.T) {
  12. assert.NoError(t, prepareEngine())
  13. type UserinfoDelete struct {
  14. Uid int64 `xorm:"id pk not null autoincr"`
  15. IsMan bool
  16. }
  17. assert.NoError(t, testEngine.Sync2(new(UserinfoDelete)))
  18. session := testEngine.NewSession()
  19. defer session.Close()
  20. var err error
  21. if testEngine.Dialect().DBType() == core.MSSQL {
  22. err = session.Begin()
  23. assert.NoError(t, err)
  24. _, err = session.Exec("SET IDENTITY_INSERT userinfo_delete ON")
  25. assert.NoError(t, err)
  26. }
  27. user := UserinfoDelete{Uid: 1}
  28. cnt, err := session.Insert(&user)
  29. assert.NoError(t, err)
  30. assert.EqualValues(t, 1, cnt)
  31. if testEngine.Dialect().DBType() == core.MSSQL {
  32. err = session.Commit()
  33. assert.NoError(t, err)
  34. }
  35. cnt, err = testEngine.Delete(&UserinfoDelete{Uid: user.Uid})
  36. assert.NoError(t, err)
  37. assert.EqualValues(t, 1, cnt)
  38. user.Uid = 0
  39. user.IsMan = true
  40. has, err := testEngine.ID(1).Get(&user)
  41. assert.NoError(t, err)
  42. assert.False(t, has)
  43. cnt, err = testEngine.Insert(&user)
  44. assert.NoError(t, err)
  45. assert.EqualValues(t, 1, cnt)
  46. cnt, err = testEngine.Where("`id`=?", user.Uid).Delete(&UserinfoDelete{})
  47. assert.NoError(t, err)
  48. assert.EqualValues(t, 1, cnt)
  49. user.Uid = 0
  50. user.IsMan = true
  51. has, err = testEngine.ID(2).Get(&user)
  52. assert.NoError(t, err)
  53. assert.False(t, has)
  54. }
  55. func TestDeleted(t *testing.T) {
  56. assert.NoError(t, prepareEngine())
  57. type Deleted struct {
  58. Id int64 `xorm:"pk"`
  59. Name string
  60. DeletedAt time.Time `xorm:"deleted"`
  61. }
  62. err := testEngine.DropTables(&Deleted{})
  63. assert.NoError(t, err)
  64. err = testEngine.CreateTables(&Deleted{})
  65. assert.NoError(t, err)
  66. _, err = testEngine.InsertOne(&Deleted{Id: 1, Name: "11111"})
  67. assert.NoError(t, err)
  68. _, err = testEngine.InsertOne(&Deleted{Id: 2, Name: "22222"})
  69. assert.NoError(t, err)
  70. _, err = testEngine.InsertOne(&Deleted{Id: 3, Name: "33333"})
  71. assert.NoError(t, err)
  72. // Test normal Find()
  73. var records1 []Deleted
  74. err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&records1, &Deleted{})
  75. assert.EqualValues(t, 3, len(records1))
  76. // Test normal Get()
  77. record1 := &Deleted{}
  78. has, err := testEngine.ID(1).Get(record1)
  79. assert.NoError(t, err)
  80. assert.True(t, has)
  81. // Test Delete() with deleted
  82. affected, err := testEngine.ID(1).Delete(&Deleted{})
  83. assert.NoError(t, err)
  84. assert.EqualValues(t, 1, affected)
  85. has, err = testEngine.ID(1).Get(&Deleted{})
  86. assert.NoError(t, err)
  87. assert.False(t, has)
  88. var records2 []Deleted
  89. err = testEngine.Where("`" + testEngine.GetColumnMapper().Obj2Table("Id") + "` > 0").Find(&records2)
  90. assert.NoError(t, err)
  91. assert.EqualValues(t, 2, len(records2))
  92. // Test no rows affected after Delete() again.
  93. affected, err = testEngine.ID(1).Delete(&Deleted{})
  94. assert.NoError(t, err)
  95. assert.EqualValues(t, 0, affected)
  96. // Deleted.DeletedAt must not be updated.
  97. affected, err = testEngine.ID(2).Update(&Deleted{Name: "2", DeletedAt: time.Now()})
  98. assert.NoError(t, err)
  99. assert.EqualValues(t, 1, affected)
  100. record2 := &Deleted{}
  101. has, err = testEngine.ID(2).Get(record2)
  102. assert.NoError(t, err)
  103. assert.True(t, record2.DeletedAt.IsZero())
  104. // Test find all records whatever `deleted`.
  105. var unscopedRecords1 []Deleted
  106. err = testEngine.Unscoped().Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&unscopedRecords1, &Deleted{})
  107. assert.NoError(t, err)
  108. assert.EqualValues(t, 3, len(unscopedRecords1))
  109. // Delete() must really delete a record with Unscoped()
  110. affected, err = testEngine.Unscoped().ID(1).Delete(&Deleted{})
  111. assert.NoError(t, err)
  112. assert.EqualValues(t, 1, affected)
  113. var unscopedRecords2 []Deleted
  114. err = testEngine.Unscoped().Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").Find(&unscopedRecords2, &Deleted{})
  115. assert.NoError(t, err)
  116. assert.EqualValues(t, 2, len(unscopedRecords2))
  117. var records3 []Deleted
  118. err = testEngine.Where("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` > 0").And("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"`> 1").
  119. Or("`"+testEngine.GetColumnMapper().Obj2Table("Id")+"` = ?", 3).Find(&records3)
  120. assert.NoError(t, err)
  121. assert.EqualValues(t, 2, len(records3))
  122. }
  123. func TestCacheDelete(t *testing.T) {
  124. assert.NoError(t, prepareEngine())
  125. oldCacher := testEngine.GetDefaultCacher()
  126. cacher := NewLRUCacher(NewMemoryStore(), 1000)
  127. testEngine.SetDefaultCacher(cacher)
  128. type CacheDeleteStruct struct {
  129. Id int64
  130. }
  131. err := testEngine.CreateTables(&CacheDeleteStruct{})
  132. assert.NoError(t, err)
  133. _, err = testEngine.Insert(&CacheDeleteStruct{})
  134. assert.NoError(t, err)
  135. aff, err := testEngine.Delete(&CacheDeleteStruct{
  136. Id: 1,
  137. })
  138. assert.NoError(t, err)
  139. assert.EqualValues(t, aff, 1)
  140. aff, err = testEngine.Unscoped().Delete(&CacheDeleteStruct{
  141. Id: 1,
  142. })
  143. assert.NoError(t, err)
  144. assert.EqualValues(t, aff, 0)
  145. testEngine.SetDefaultCacher(oldCacher)
  146. }
  147. func TestUnscopeDelete(t *testing.T) {
  148. assert.NoError(t, prepareEngine())
  149. type UnscopeDeleteStruct struct {
  150. Id int64
  151. Name string
  152. DeletedAt time.Time `xorm:"deleted"`
  153. }
  154. assertSync(t, new(UnscopeDeleteStruct))
  155. cnt, err := testEngine.Insert(&UnscopeDeleteStruct{
  156. Name: "test",
  157. })
  158. assert.NoError(t, err)
  159. assert.EqualValues(t, 1, cnt)
  160. var nowUnix = time.Now().Unix()
  161. var s UnscopeDeleteStruct
  162. cnt, err = testEngine.ID(1).Delete(&s)
  163. assert.NoError(t, err)
  164. assert.EqualValues(t, 1, cnt)
  165. assert.EqualValues(t, nowUnix, s.DeletedAt.Unix())
  166. var s1 UnscopeDeleteStruct
  167. has, err := testEngine.ID(1).Get(&s1)
  168. assert.NoError(t, err)
  169. assert.False(t, has)
  170. var s2 UnscopeDeleteStruct
  171. has, err = testEngine.ID(1).Unscoped().Get(&s2)
  172. assert.NoError(t, err)
  173. assert.True(t, has)
  174. assert.EqualValues(t, "test", s2.Name)
  175. assert.EqualValues(t, nowUnix, s2.DeletedAt.Unix())
  176. cnt, err = testEngine.ID(1).Unscoped().Delete(new(UnscopeDeleteStruct))
  177. assert.NoError(t, err)
  178. assert.EqualValues(t, 1, cnt)
  179. var s3 UnscopeDeleteStruct
  180. has, err = testEngine.ID(1).Get(&s3)
  181. assert.NoError(t, err)
  182. assert.False(t, has)
  183. var s4 UnscopeDeleteStruct
  184. has, err = testEngine.ID(1).Unscoped().Get(&s4)
  185. assert.NoError(t, err)
  186. assert.False(t, has)
  187. }