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.

180 lines
4.4KB

  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. )
  10. func TestCacheFind(t *testing.T) {
  11. assert.NoError(t, prepareEngine())
  12. type MailBox struct {
  13. Id int64 `xorm:"pk"`
  14. Username string
  15. Password string
  16. }
  17. oldCacher := testEngine.GetDefaultCacher()
  18. cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
  19. testEngine.SetDefaultCacher(cacher)
  20. assert.NoError(t, testEngine.Sync2(new(MailBox)))
  21. var inserts = []*MailBox{
  22. {
  23. Id: 0,
  24. Username: "user1",
  25. Password: "pass1",
  26. },
  27. {
  28. Id: 1,
  29. Username: "user2",
  30. Password: "pass2",
  31. },
  32. }
  33. _, err := testEngine.Insert(inserts[0], inserts[1])
  34. assert.NoError(t, err)
  35. var boxes []MailBox
  36. assert.NoError(t, testEngine.Find(&boxes))
  37. assert.EqualValues(t, 2, len(boxes))
  38. for i, box := range boxes {
  39. assert.Equal(t, inserts[i].Id, box.Id)
  40. assert.Equal(t, inserts[i].Username, box.Username)
  41. assert.Equal(t, inserts[i].Password, box.Password)
  42. }
  43. boxes = make([]MailBox, 0, 2)
  44. assert.NoError(t, testEngine.Find(&boxes))
  45. assert.EqualValues(t, 2, len(boxes))
  46. for i, box := range boxes {
  47. assert.Equal(t, inserts[i].Id, box.Id)
  48. assert.Equal(t, inserts[i].Username, box.Username)
  49. assert.Equal(t, inserts[i].Password, box.Password)
  50. }
  51. boxes = make([]MailBox, 0, 2)
  52. assert.NoError(t, testEngine.Alias("a").Where("a.id > -1").Asc("a.id").Find(&boxes))
  53. assert.EqualValues(t, 2, len(boxes))
  54. for i, box := range boxes {
  55. assert.Equal(t, inserts[i].Id, box.Id)
  56. assert.Equal(t, inserts[i].Username, box.Username)
  57. assert.Equal(t, inserts[i].Password, box.Password)
  58. }
  59. type MailBox4 struct {
  60. Id int64
  61. Username string
  62. Password string
  63. }
  64. boxes2 := make([]MailBox4, 0, 2)
  65. assert.NoError(t, testEngine.Table("mail_box").Where("mail_box.id > -1").Asc("mail_box.id").Find(&boxes2))
  66. assert.EqualValues(t, 2, len(boxes2))
  67. for i, box := range boxes2 {
  68. assert.Equal(t, inserts[i].Id, box.Id)
  69. assert.Equal(t, inserts[i].Username, box.Username)
  70. assert.Equal(t, inserts[i].Password, box.Password)
  71. }
  72. testEngine.SetDefaultCacher(oldCacher)
  73. }
  74. func TestCacheFind2(t *testing.T) {
  75. assert.NoError(t, prepareEngine())
  76. type MailBox2 struct {
  77. Id uint64 `xorm:"pk"`
  78. Username string
  79. Password string
  80. }
  81. oldCacher := testEngine.GetDefaultCacher()
  82. cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
  83. testEngine.SetDefaultCacher(cacher)
  84. assert.NoError(t, testEngine.Sync2(new(MailBox2)))
  85. var inserts = []*MailBox2{
  86. {
  87. Id: 0,
  88. Username: "user1",
  89. Password: "pass1",
  90. },
  91. {
  92. Id: 1,
  93. Username: "user2",
  94. Password: "pass2",
  95. },
  96. }
  97. _, err := testEngine.Insert(inserts[0], inserts[1])
  98. assert.NoError(t, err)
  99. var boxes []MailBox2
  100. assert.NoError(t, testEngine.Find(&boxes))
  101. assert.EqualValues(t, 2, len(boxes))
  102. for i, box := range boxes {
  103. assert.Equal(t, inserts[i].Id, box.Id)
  104. assert.Equal(t, inserts[i].Username, box.Username)
  105. assert.Equal(t, inserts[i].Password, box.Password)
  106. }
  107. boxes = make([]MailBox2, 0, 2)
  108. assert.NoError(t, testEngine.Find(&boxes))
  109. assert.EqualValues(t, 2, len(boxes))
  110. for i, box := range boxes {
  111. assert.Equal(t, inserts[i].Id, box.Id)
  112. assert.Equal(t, inserts[i].Username, box.Username)
  113. assert.Equal(t, inserts[i].Password, box.Password)
  114. }
  115. testEngine.SetDefaultCacher(oldCacher)
  116. }
  117. func TestCacheGet(t *testing.T) {
  118. assert.NoError(t, prepareEngine())
  119. type MailBox3 struct {
  120. Id uint64
  121. Username string
  122. Password string
  123. }
  124. oldCacher := testEngine.GetDefaultCacher()
  125. cacher := NewLRUCacher2(NewMemoryStore(), time.Hour, 10000)
  126. testEngine.SetDefaultCacher(cacher)
  127. assert.NoError(t, testEngine.Sync2(new(MailBox3)))
  128. var inserts = []*MailBox3{
  129. {
  130. Username: "user1",
  131. Password: "pass1",
  132. },
  133. }
  134. _, err := testEngine.Insert(inserts[0])
  135. assert.NoError(t, err)
  136. var box1 MailBox3
  137. has, err := testEngine.Where("id = ?", inserts[0].Id).Get(&box1)
  138. assert.NoError(t, err)
  139. assert.True(t, has)
  140. assert.EqualValues(t, "user1", box1.Username)
  141. assert.EqualValues(t, "pass1", box1.Password)
  142. var box2 MailBox3
  143. has, err = testEngine.Where("id = ?", inserts[0].Id).Get(&box2)
  144. assert.NoError(t, err)
  145. assert.True(t, has)
  146. assert.EqualValues(t, "user1", box2.Username)
  147. assert.EqualValues(t, "pass1", box2.Password)
  148. testEngine.SetDefaultCacher(oldCacher)
  149. }