time_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. "strings"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestTimeUserTime(t *testing.T) {
  13. assert.NoError(t, prepareEngine())
  14. type TimeUser struct {
  15. Id string
  16. OperTime time.Time
  17. }
  18. assertSync(t, new(TimeUser))
  19. var user = TimeUser{
  20. Id: "lunny",
  21. OperTime: time.Now(),
  22. }
  23. fmt.Println("user", user.OperTime)
  24. cnt, err := testEngine.Insert(&user)
  25. assert.NoError(t, err)
  26. assert.EqualValues(t, 1, cnt)
  27. var user2 TimeUser
  28. has, err := testEngine.Get(&user2)
  29. assert.NoError(t, err)
  30. assert.True(t, has)
  31. assert.EqualValues(t, user.OperTime.Unix(), user2.OperTime.Unix())
  32. assert.EqualValues(t, formatTime(user.OperTime), formatTime(user2.OperTime))
  33. fmt.Println("user2", user2.OperTime)
  34. }
  35. func TestTimeUserTimeDiffLoc(t *testing.T) {
  36. assert.NoError(t, prepareEngine())
  37. loc, err := time.LoadLocation("Asia/Shanghai")
  38. assert.NoError(t, err)
  39. testEngine.SetTZLocation(loc)
  40. dbLoc, err := time.LoadLocation("America/New_York")
  41. assert.NoError(t, err)
  42. testEngine.SetTZDatabase(dbLoc)
  43. type TimeUser2 struct {
  44. Id string
  45. OperTime time.Time
  46. }
  47. assertSync(t, new(TimeUser2))
  48. var user = TimeUser2{
  49. Id: "lunny",
  50. OperTime: time.Now(),
  51. }
  52. fmt.Println("user", user.OperTime)
  53. cnt, err := testEngine.Insert(&user)
  54. assert.NoError(t, err)
  55. assert.EqualValues(t, 1, cnt)
  56. var user2 TimeUser2
  57. has, err := testEngine.Get(&user2)
  58. assert.NoError(t, err)
  59. assert.True(t, has)
  60. assert.EqualValues(t, user.OperTime.Unix(), user2.OperTime.Unix())
  61. assert.EqualValues(t, formatTime(user.OperTime.In(loc)), formatTime(user2.OperTime))
  62. fmt.Println("user2", user2.OperTime)
  63. }
  64. func TestTimeUserCreated(t *testing.T) {
  65. assert.NoError(t, prepareEngine())
  66. type UserCreated struct {
  67. Id string
  68. CreatedAt time.Time `xorm:"created"`
  69. }
  70. assertSync(t, new(UserCreated))
  71. var user = UserCreated{
  72. Id: "lunny",
  73. }
  74. fmt.Println("user", user.CreatedAt)
  75. cnt, err := testEngine.Insert(&user)
  76. assert.NoError(t, err)
  77. assert.EqualValues(t, 1, cnt)
  78. var user2 UserCreated
  79. has, err := testEngine.Get(&user2)
  80. assert.NoError(t, err)
  81. assert.True(t, has)
  82. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  83. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user2.CreatedAt))
  84. fmt.Println("user2", user2.CreatedAt)
  85. }
  86. func TestTimeUserCreatedDiffLoc(t *testing.T) {
  87. assert.NoError(t, prepareEngine())
  88. loc, err := time.LoadLocation("Asia/Shanghai")
  89. assert.NoError(t, err)
  90. testEngine.SetTZLocation(loc)
  91. dbLoc, err := time.LoadLocation("America/New_York")
  92. assert.NoError(t, err)
  93. testEngine.SetTZDatabase(dbLoc)
  94. type UserCreated2 struct {
  95. Id string
  96. CreatedAt time.Time `xorm:"created"`
  97. }
  98. assertSync(t, new(UserCreated2))
  99. var user = UserCreated2{
  100. Id: "lunny",
  101. }
  102. fmt.Println("user", user.CreatedAt)
  103. cnt, err := testEngine.Insert(&user)
  104. assert.NoError(t, err)
  105. assert.EqualValues(t, 1, cnt)
  106. var user2 UserCreated2
  107. has, err := testEngine.Get(&user2)
  108. assert.NoError(t, err)
  109. assert.True(t, has)
  110. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  111. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user2.CreatedAt))
  112. fmt.Println("user2", user2.CreatedAt)
  113. }
  114. func TestTimeUserUpdated(t *testing.T) {
  115. assert.NoError(t, prepareEngine())
  116. type UserUpdated struct {
  117. Id string
  118. CreatedAt time.Time `xorm:"created"`
  119. UpdatedAt time.Time `xorm:"updated"`
  120. }
  121. assertSync(t, new(UserUpdated))
  122. var user = UserUpdated{
  123. Id: "lunny",
  124. }
  125. fmt.Println("user", user.CreatedAt, user.UpdatedAt)
  126. cnt, err := testEngine.Insert(&user)
  127. assert.NoError(t, err)
  128. assert.EqualValues(t, 1, cnt)
  129. var user2 UserUpdated
  130. has, err := testEngine.Get(&user2)
  131. assert.NoError(t, err)
  132. assert.True(t, has)
  133. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  134. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user2.CreatedAt))
  135. assert.EqualValues(t, user.UpdatedAt.Unix(), user2.UpdatedAt.Unix())
  136. assert.EqualValues(t, formatTime(user.UpdatedAt), formatTime(user2.UpdatedAt))
  137. fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt)
  138. var user3 = UserUpdated{
  139. Id: "lunny2",
  140. }
  141. cnt, err = testEngine.Update(&user3)
  142. assert.NoError(t, err)
  143. assert.EqualValues(t, 1, cnt)
  144. assert.True(t, user.UpdatedAt.Unix() <= user3.UpdatedAt.Unix())
  145. var user4 UserUpdated
  146. has, err = testEngine.Get(&user4)
  147. assert.NoError(t, err)
  148. assert.True(t, has)
  149. assert.EqualValues(t, user.CreatedAt.Unix(), user4.CreatedAt.Unix())
  150. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user4.CreatedAt))
  151. assert.EqualValues(t, user3.UpdatedAt.Unix(), user4.UpdatedAt.Unix())
  152. assert.EqualValues(t, formatTime(user3.UpdatedAt), formatTime(user4.UpdatedAt))
  153. fmt.Println("user3", user.CreatedAt, user4.UpdatedAt)
  154. }
  155. func TestTimeUserUpdatedDiffLoc(t *testing.T) {
  156. assert.NoError(t, prepareEngine())
  157. loc, err := time.LoadLocation("Asia/Shanghai")
  158. assert.NoError(t, err)
  159. testEngine.SetTZLocation(loc)
  160. dbLoc, err := time.LoadLocation("America/New_York")
  161. assert.NoError(t, err)
  162. testEngine.SetTZDatabase(dbLoc)
  163. type UserUpdated2 struct {
  164. Id string
  165. CreatedAt time.Time `xorm:"created"`
  166. UpdatedAt time.Time `xorm:"updated"`
  167. }
  168. assertSync(t, new(UserUpdated2))
  169. var user = UserUpdated2{
  170. Id: "lunny",
  171. }
  172. fmt.Println("user", user.CreatedAt, user.UpdatedAt)
  173. cnt, err := testEngine.Insert(&user)
  174. assert.NoError(t, err)
  175. assert.EqualValues(t, 1, cnt)
  176. var user2 UserUpdated2
  177. has, err := testEngine.Get(&user2)
  178. assert.NoError(t, err)
  179. assert.True(t, has)
  180. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  181. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user2.CreatedAt))
  182. assert.EqualValues(t, user.UpdatedAt.Unix(), user2.UpdatedAt.Unix())
  183. assert.EqualValues(t, formatTime(user.UpdatedAt), formatTime(user2.UpdatedAt))
  184. fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt)
  185. var user3 = UserUpdated2{
  186. Id: "lunny2",
  187. }
  188. cnt, err = testEngine.Update(&user3)
  189. assert.NoError(t, err)
  190. assert.EqualValues(t, 1, cnt)
  191. assert.True(t, user.UpdatedAt.Unix() <= user3.UpdatedAt.Unix())
  192. var user4 UserUpdated2
  193. has, err = testEngine.Get(&user4)
  194. assert.NoError(t, err)
  195. assert.True(t, has)
  196. assert.EqualValues(t, user.CreatedAt.Unix(), user4.CreatedAt.Unix())
  197. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user4.CreatedAt))
  198. assert.EqualValues(t, user3.UpdatedAt.Unix(), user4.UpdatedAt.Unix())
  199. assert.EqualValues(t, formatTime(user3.UpdatedAt), formatTime(user4.UpdatedAt))
  200. fmt.Println("user3", user.CreatedAt, user4.UpdatedAt)
  201. }
  202. func TestTimeUserDeleted(t *testing.T) {
  203. assert.NoError(t, prepareEngine())
  204. type UserDeleted struct {
  205. Id string
  206. CreatedAt time.Time `xorm:"created"`
  207. UpdatedAt time.Time `xorm:"updated"`
  208. DeletedAt time.Time `xorm:"deleted"`
  209. }
  210. assertSync(t, new(UserDeleted))
  211. var user = UserDeleted{
  212. Id: "lunny",
  213. }
  214. cnt, err := testEngine.Insert(&user)
  215. assert.NoError(t, err)
  216. assert.EqualValues(t, 1, cnt)
  217. fmt.Println("user", user.CreatedAt, user.UpdatedAt, user.DeletedAt)
  218. var user2 UserDeleted
  219. has, err := testEngine.Get(&user2)
  220. assert.NoError(t, err)
  221. assert.True(t, has)
  222. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  223. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user2.CreatedAt))
  224. assert.EqualValues(t, user.UpdatedAt.Unix(), user2.UpdatedAt.Unix())
  225. assert.EqualValues(t, formatTime(user.UpdatedAt), formatTime(user2.UpdatedAt))
  226. assert.True(t, isTimeZero(user2.DeletedAt))
  227. fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt, user2.DeletedAt)
  228. var user3 UserDeleted
  229. cnt, err = testEngine.Where("id = ?", "lunny").Delete(&user3)
  230. assert.NoError(t, err)
  231. assert.EqualValues(t, 1, cnt)
  232. assert.True(t, !isTimeZero(user3.DeletedAt))
  233. var user4 UserDeleted
  234. has, err = testEngine.Unscoped().Get(&user4)
  235. assert.NoError(t, err)
  236. assert.True(t, has)
  237. assert.EqualValues(t, user3.DeletedAt.Unix(), user4.DeletedAt.Unix())
  238. assert.EqualValues(t, formatTime(user3.DeletedAt), formatTime(user4.DeletedAt))
  239. fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
  240. }
  241. func TestTimeUserDeletedDiffLoc(t *testing.T) {
  242. assert.NoError(t, prepareEngine())
  243. loc, err := time.LoadLocation("Asia/Shanghai")
  244. assert.NoError(t, err)
  245. testEngine.SetTZLocation(loc)
  246. dbLoc, err := time.LoadLocation("America/New_York")
  247. assert.NoError(t, err)
  248. testEngine.SetTZDatabase(dbLoc)
  249. type UserDeleted2 struct {
  250. Id string
  251. CreatedAt time.Time `xorm:"created"`
  252. UpdatedAt time.Time `xorm:"updated"`
  253. DeletedAt time.Time `xorm:"deleted"`
  254. }
  255. assertSync(t, new(UserDeleted2))
  256. var user = UserDeleted2{
  257. Id: "lunny",
  258. }
  259. cnt, err := testEngine.Insert(&user)
  260. assert.NoError(t, err)
  261. assert.EqualValues(t, 1, cnt)
  262. fmt.Println("user", user.CreatedAt, user.UpdatedAt, user.DeletedAt)
  263. var user2 UserDeleted2
  264. has, err := testEngine.Get(&user2)
  265. assert.NoError(t, err)
  266. assert.True(t, has)
  267. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  268. assert.EqualValues(t, formatTime(user.CreatedAt), formatTime(user2.CreatedAt))
  269. assert.EqualValues(t, user.UpdatedAt.Unix(), user2.UpdatedAt.Unix())
  270. assert.EqualValues(t, formatTime(user.UpdatedAt), formatTime(user2.UpdatedAt))
  271. assert.True(t, isTimeZero(user2.DeletedAt))
  272. fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt, user2.DeletedAt)
  273. var user3 UserDeleted2
  274. cnt, err = testEngine.Where("id = ?", "lunny").Delete(&user3)
  275. assert.NoError(t, err)
  276. assert.EqualValues(t, 1, cnt)
  277. assert.True(t, !isTimeZero(user3.DeletedAt))
  278. var user4 UserDeleted2
  279. has, err = testEngine.Unscoped().Get(&user4)
  280. assert.NoError(t, err)
  281. assert.True(t, has)
  282. assert.EqualValues(t, user3.DeletedAt.Unix(), user4.DeletedAt.Unix())
  283. assert.EqualValues(t, formatTime(user3.DeletedAt), formatTime(user4.DeletedAt))
  284. fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
  285. }
  286. type JsonDate time.Time
  287. func (j JsonDate) MarshalJSON() ([]byte, error) {
  288. if time.Time(j).IsZero() {
  289. return []byte(`""`), nil
  290. }
  291. return []byte(`"` + time.Time(j).Format("2006-01-02 15:04:05") + `"`), nil
  292. }
  293. func (j *JsonDate) UnmarshalJSON(value []byte) error {
  294. var v = strings.TrimSpace(strings.Trim(string(value), "\""))
  295. t, err := time.ParseInLocation("2006-01-02 15:04:05", v, time.Local)
  296. if err != nil {
  297. return err
  298. }
  299. *j = JsonDate(t)
  300. return nil
  301. }
  302. func (j *JsonDate) Unix() int64 {
  303. return (*time.Time)(j).Unix()
  304. }
  305. func TestCustomTimeUserDeleted(t *testing.T) {
  306. assert.NoError(t, prepareEngine())
  307. type UserDeleted3 struct {
  308. Id string
  309. CreatedAt JsonDate `xorm:"created"`
  310. UpdatedAt JsonDate `xorm:"updated"`
  311. DeletedAt JsonDate `xorm:"deleted"`
  312. }
  313. assertSync(t, new(UserDeleted3))
  314. var user = UserDeleted3{
  315. Id: "lunny",
  316. }
  317. cnt, err := testEngine.Insert(&user)
  318. assert.NoError(t, err)
  319. assert.EqualValues(t, 1, cnt)
  320. fmt.Println("user", user.CreatedAt, user.UpdatedAt, user.DeletedAt)
  321. var user2 UserDeleted3
  322. has, err := testEngine.Get(&user2)
  323. assert.NoError(t, err)
  324. assert.True(t, has)
  325. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  326. assert.EqualValues(t, formatTime(time.Time(user.CreatedAt)), formatTime(time.Time(user2.CreatedAt)))
  327. assert.EqualValues(t, user.UpdatedAt.Unix(), user2.UpdatedAt.Unix())
  328. assert.EqualValues(t, formatTime(time.Time(user.UpdatedAt)), formatTime(time.Time(user2.UpdatedAt)))
  329. assert.True(t, isTimeZero(time.Time(user2.DeletedAt)))
  330. fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt, user2.DeletedAt)
  331. var user3 UserDeleted3
  332. cnt, err = testEngine.Where("id = ?", "lunny").Delete(&user3)
  333. assert.NoError(t, err)
  334. assert.EqualValues(t, 1, cnt)
  335. assert.True(t, !isTimeZero(time.Time(user3.DeletedAt)))
  336. var user4 UserDeleted3
  337. has, err = testEngine.Unscoped().Get(&user4)
  338. assert.NoError(t, err)
  339. assert.True(t, has)
  340. assert.EqualValues(t, user3.DeletedAt.Unix(), user4.DeletedAt.Unix())
  341. assert.EqualValues(t, formatTime(time.Time(user3.DeletedAt)), formatTime(time.Time(user4.DeletedAt)))
  342. fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
  343. }
  344. func TestCustomTimeUserDeletedDiffLoc(t *testing.T) {
  345. assert.NoError(t, prepareEngine())
  346. loc, err := time.LoadLocation("Asia/Shanghai")
  347. assert.NoError(t, err)
  348. testEngine.SetTZLocation(loc)
  349. dbLoc, err := time.LoadLocation("America/New_York")
  350. assert.NoError(t, err)
  351. testEngine.SetTZDatabase(dbLoc)
  352. type UserDeleted4 struct {
  353. Id string
  354. CreatedAt JsonDate `xorm:"created"`
  355. UpdatedAt JsonDate `xorm:"updated"`
  356. DeletedAt JsonDate `xorm:"deleted"`
  357. }
  358. assertSync(t, new(UserDeleted4))
  359. var user = UserDeleted4{
  360. Id: "lunny",
  361. }
  362. cnt, err := testEngine.Insert(&user)
  363. assert.NoError(t, err)
  364. assert.EqualValues(t, 1, cnt)
  365. fmt.Println("user", user.CreatedAt, user.UpdatedAt, user.DeletedAt)
  366. var user2 UserDeleted4
  367. has, err := testEngine.Get(&user2)
  368. assert.NoError(t, err)
  369. assert.True(t, has)
  370. assert.EqualValues(t, user.CreatedAt.Unix(), user2.CreatedAt.Unix())
  371. assert.EqualValues(t, formatTime(time.Time(user.CreatedAt)), formatTime(time.Time(user2.CreatedAt)))
  372. assert.EqualValues(t, user.UpdatedAt.Unix(), user2.UpdatedAt.Unix())
  373. assert.EqualValues(t, formatTime(time.Time(user.UpdatedAt)), formatTime(time.Time(user2.UpdatedAt)))
  374. assert.True(t, isTimeZero(time.Time(user2.DeletedAt)))
  375. fmt.Println("user2", user2.CreatedAt, user2.UpdatedAt, user2.DeletedAt)
  376. var user3 UserDeleted4
  377. cnt, err = testEngine.Where("id = ?", "lunny").Delete(&user3)
  378. assert.NoError(t, err)
  379. assert.EqualValues(t, 1, cnt)
  380. assert.True(t, !isTimeZero(time.Time(user3.DeletedAt)))
  381. var user4 UserDeleted4
  382. has, err = testEngine.Unscoped().Get(&user4)
  383. assert.NoError(t, err)
  384. assert.True(t, has)
  385. assert.EqualValues(t, user3.DeletedAt.Unix(), user4.DeletedAt.Unix())
  386. assert.EqualValues(t, formatTime(time.Time(user3.DeletedAt)), formatTime(time.Time(user4.DeletedAt)))
  387. fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
  388. }