Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
4.1KB

  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. "time"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. type VersionS struct {
  13. Id int64
  14. Name string
  15. Ver int `xorm:"version"`
  16. Created time.Time `xorm:"created"`
  17. }
  18. func TestVersion1(t *testing.T) {
  19. assert.NoError(t, prepareEngine())
  20. err := testEngine.DropTables(new(VersionS))
  21. if err != nil {
  22. t.Error(err)
  23. panic(err)
  24. }
  25. err = testEngine.CreateTables(new(VersionS))
  26. if err != nil {
  27. t.Error(err)
  28. panic(err)
  29. }
  30. ver := &VersionS{Name: "sfsfdsfds"}
  31. _, err = testEngine.Insert(ver)
  32. if err != nil {
  33. t.Error(err)
  34. panic(err)
  35. }
  36. fmt.Println(ver)
  37. if ver.Ver != 1 {
  38. err = errors.New("insert error")
  39. t.Error(err)
  40. panic(err)
  41. }
  42. newVer := new(VersionS)
  43. has, err := testEngine.ID(ver.Id).Get(newVer)
  44. if err != nil {
  45. t.Error(err)
  46. panic(err)
  47. }
  48. if !has {
  49. t.Error(errors.New(fmt.Sprintf("no version id is %v", ver.Id)))
  50. panic(err)
  51. }
  52. fmt.Println(newVer)
  53. if newVer.Ver != 1 {
  54. err = errors.New("insert error")
  55. t.Error(err)
  56. panic(err)
  57. }
  58. newVer.Name = "-------"
  59. _, err = testEngine.ID(ver.Id).Update(newVer)
  60. if err != nil {
  61. t.Error(err)
  62. panic(err)
  63. }
  64. if newVer.Ver != 2 {
  65. err = errors.New("update should set version back to struct")
  66. t.Error(err)
  67. }
  68. newVer = new(VersionS)
  69. has, err = testEngine.ID(ver.Id).Get(newVer)
  70. if err != nil {
  71. t.Error(err)
  72. panic(err)
  73. }
  74. fmt.Println(newVer)
  75. if newVer.Ver != 2 {
  76. err = errors.New("update error")
  77. t.Error(err)
  78. panic(err)
  79. }
  80. }
  81. func TestVersion2(t *testing.T) {
  82. assert.NoError(t, prepareEngine())
  83. err := testEngine.DropTables(new(VersionS))
  84. if err != nil {
  85. t.Error(err)
  86. panic(err)
  87. }
  88. err = testEngine.CreateTables(new(VersionS))
  89. if err != nil {
  90. t.Error(err)
  91. panic(err)
  92. }
  93. var vers = []VersionS{
  94. {Name: "sfsfdsfds"},
  95. {Name: "xxxxx"},
  96. }
  97. _, err = testEngine.Insert(vers)
  98. if err != nil {
  99. t.Error(err)
  100. panic(err)
  101. }
  102. fmt.Println(vers)
  103. for _, v := range vers {
  104. if v.Ver != 1 {
  105. err := errors.New("version should be 1")
  106. t.Error(err)
  107. panic(err)
  108. }
  109. }
  110. }
  111. type VersionUintS struct {
  112. Id int64
  113. Name string
  114. Ver uint `xorm:"version"`
  115. Created time.Time `xorm:"created"`
  116. }
  117. func TestVersion3(t *testing.T) {
  118. assert.NoError(t, prepareEngine())
  119. err := testEngine.DropTables(new(VersionUintS))
  120. if err != nil {
  121. t.Error(err)
  122. panic(err)
  123. }
  124. err = testEngine.CreateTables(new(VersionUintS))
  125. if err != nil {
  126. t.Error(err)
  127. panic(err)
  128. }
  129. ver := &VersionUintS{Name: "sfsfdsfds"}
  130. _, err = testEngine.Insert(ver)
  131. if err != nil {
  132. t.Error(err)
  133. panic(err)
  134. }
  135. fmt.Println(ver)
  136. if ver.Ver != 1 {
  137. err = errors.New("insert error")
  138. t.Error(err)
  139. panic(err)
  140. }
  141. newVer := new(VersionUintS)
  142. has, err := testEngine.ID(ver.Id).Get(newVer)
  143. if err != nil {
  144. t.Error(err)
  145. panic(err)
  146. }
  147. if !has {
  148. t.Error(errors.New(fmt.Sprintf("no version id is %v", ver.Id)))
  149. panic(err)
  150. }
  151. fmt.Println(newVer)
  152. if newVer.Ver != 1 {
  153. err = errors.New("insert error")
  154. t.Error(err)
  155. panic(err)
  156. }
  157. newVer.Name = "-------"
  158. _, err = testEngine.ID(ver.Id).Update(newVer)
  159. if err != nil {
  160. t.Error(err)
  161. panic(err)
  162. }
  163. if newVer.Ver != 2 {
  164. err = errors.New("update should set version back to struct")
  165. t.Error(err)
  166. }
  167. newVer = new(VersionUintS)
  168. has, err = testEngine.ID(ver.Id).Get(newVer)
  169. if err != nil {
  170. t.Error(err)
  171. panic(err)
  172. }
  173. fmt.Println(newVer)
  174. if newVer.Ver != 2 {
  175. err = errors.New("update error")
  176. t.Error(err)
  177. panic(err)
  178. }
  179. }
  180. func TestVersion4(t *testing.T) {
  181. assert.NoError(t, prepareEngine())
  182. err := testEngine.DropTables(new(VersionUintS))
  183. if err != nil {
  184. t.Error(err)
  185. panic(err)
  186. }
  187. err = testEngine.CreateTables(new(VersionUintS))
  188. if err != nil {
  189. t.Error(err)
  190. panic(err)
  191. }
  192. var vers = []VersionUintS{
  193. {Name: "sfsfdsfds"},
  194. {Name: "xxxxx"},
  195. }
  196. _, err = testEngine.Insert(vers)
  197. if err != nil {
  198. t.Error(err)
  199. panic(err)
  200. }
  201. fmt.Println(vers)
  202. for _, v := range vers {
  203. if v.Ver != 1 {
  204. err := errors.New("version should be 1")
  205. t.Error(err)
  206. panic(err)
  207. }
  208. }
  209. }