session_stats_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. "strconv"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/xormplus/builder"
  11. )
  12. func isFloatEq(i, j float64, precision int) bool {
  13. return fmt.Sprintf("%."+strconv.Itoa(precision)+"f", i) == fmt.Sprintf("%."+strconv.Itoa(precision)+"f", j)
  14. }
  15. func TestSum(t *testing.T) {
  16. type SumStruct struct {
  17. Int int
  18. Float float32
  19. }
  20. assert.NoError(t, prepareEngine())
  21. assert.NoError(t, testEngine.Sync2(new(SumStruct)))
  22. var (
  23. cases = []SumStruct{
  24. {1, 6.2},
  25. {2, 5.3},
  26. {92, -0.2},
  27. }
  28. )
  29. var i int
  30. var f float32
  31. for _, v := range cases {
  32. i += v.Int
  33. f += v.Float
  34. }
  35. cnt, err := testEngine.Insert(cases)
  36. assert.NoError(t, err)
  37. assert.EqualValues(t, 3, cnt)
  38. colInt := testEngine.GetColumnMapper().Obj2Table("Int")
  39. colFloat := testEngine.GetColumnMapper().Obj2Table("Float")
  40. sumInt, err := testEngine.Sum(new(SumStruct), colInt)
  41. assert.NoError(t, err)
  42. assert.EqualValues(t, int(sumInt), i)
  43. sumFloat, err := testEngine.Sum(new(SumStruct), colFloat)
  44. assert.NoError(t, err)
  45. assert.Condition(t, func() bool {
  46. return isFloatEq(sumFloat, float64(f), 2)
  47. })
  48. sums, err := testEngine.Sums(new(SumStruct), colInt, colFloat)
  49. assert.NoError(t, err)
  50. assert.EqualValues(t, 2, len(sums))
  51. assert.EqualValues(t, i, int(sums[0]))
  52. assert.Condition(t, func() bool {
  53. return isFloatEq(sums[1], float64(f), 2)
  54. })
  55. sumsInt, err := testEngine.SumsInt(new(SumStruct), colInt)
  56. assert.NoError(t, err)
  57. assert.EqualValues(t, 1, len(sumsInt))
  58. assert.EqualValues(t, i, int(sumsInt[0]))
  59. }
  60. type SumStructWithTableName struct {
  61. Int int
  62. Float float32
  63. }
  64. func (s SumStructWithTableName) TableName() string {
  65. return "sum_struct_with_table_name_1"
  66. }
  67. func TestSumWithTableName(t *testing.T) {
  68. assert.NoError(t, prepareEngine())
  69. assert.NoError(t, testEngine.Sync2(new(SumStructWithTableName)))
  70. var (
  71. cases = []SumStructWithTableName{
  72. {1, 6.2},
  73. {2, 5.3},
  74. {92, -0.2},
  75. }
  76. )
  77. var i int
  78. var f float32
  79. for _, v := range cases {
  80. i += v.Int
  81. f += v.Float
  82. }
  83. cnt, err := testEngine.Insert(cases)
  84. assert.NoError(t, err)
  85. assert.EqualValues(t, 3, cnt)
  86. colInt := testEngine.GetColumnMapper().Obj2Table("Int")
  87. colFloat := testEngine.GetColumnMapper().Obj2Table("Float")
  88. sumInt, err := testEngine.Sum(new(SumStructWithTableName), colInt)
  89. assert.NoError(t, err)
  90. assert.EqualValues(t, int(sumInt), i)
  91. sumFloat, err := testEngine.Sum(new(SumStructWithTableName), colFloat)
  92. assert.NoError(t, err)
  93. assert.Condition(t, func() bool {
  94. return isFloatEq(sumFloat, float64(f), 2)
  95. })
  96. sums, err := testEngine.Sums(new(SumStructWithTableName), colInt, colFloat)
  97. assert.NoError(t, err)
  98. assert.EqualValues(t, 2, len(sums))
  99. assert.EqualValues(t, i, int(sums[0]))
  100. assert.Condition(t, func() bool {
  101. return isFloatEq(sums[1], float64(f), 2)
  102. })
  103. sumsInt, err := testEngine.SumsInt(new(SumStructWithTableName), colInt)
  104. assert.NoError(t, err)
  105. assert.EqualValues(t, 1, len(sumsInt))
  106. assert.EqualValues(t, i, int(sumsInt[0]))
  107. }
  108. func TestSumCustomColumn(t *testing.T) {
  109. assert.NoError(t, prepareEngine())
  110. type SumStruct2 struct {
  111. Int int
  112. Float float32
  113. }
  114. var (
  115. cases = []SumStruct2{
  116. {1, 6.2},
  117. {2, 5.3},
  118. {92, -0.2},
  119. }
  120. )
  121. assert.NoError(t, testEngine.Sync2(new(SumStruct2)))
  122. cnt, err := testEngine.Insert(cases)
  123. assert.NoError(t, err)
  124. assert.EqualValues(t, 3, cnt)
  125. sumInt, err := testEngine.Sum(new(SumStruct2),
  126. "CASE WHEN `int` <= 2 THEN `int` ELSE 0 END")
  127. assert.NoError(t, err)
  128. assert.EqualValues(t, 3, int(sumInt))
  129. }
  130. func TestCount(t *testing.T) {
  131. assert.NoError(t, prepareEngine())
  132. type UserinfoCount struct {
  133. Departname string
  134. }
  135. assert.NoError(t, testEngine.Sync2(new(UserinfoCount)))
  136. colName := testEngine.GetColumnMapper().Obj2Table("Departname")
  137. var cond builder.Cond = builder.Eq{
  138. "`" + colName + "`": "dev",
  139. }
  140. total, err := testEngine.Where(cond).Count(new(UserinfoCount))
  141. assert.NoError(t, err)
  142. assert.EqualValues(t, 0, total)
  143. cnt, err := testEngine.Insert(&UserinfoCount{
  144. Departname: "dev",
  145. })
  146. assert.NoError(t, err)
  147. assert.EqualValues(t, 1, cnt)
  148. total, err = testEngine.Where(cond).Count(new(UserinfoCount))
  149. assert.NoError(t, err)
  150. assert.EqualValues(t, 1, total)
  151. total, err = testEngine.Where(cond).Table("userinfo_count").Count()
  152. assert.NoError(t, err)
  153. assert.EqualValues(t, 1, total)
  154. total, err = testEngine.Table("userinfo_count").Count()
  155. assert.NoError(t, err)
  156. assert.EqualValues(t, 1, total)
  157. }
  158. func TestSQLCount(t *testing.T) {
  159. assert.NoError(t, prepareEngine())
  160. type UserinfoCount2 struct {
  161. Id int64
  162. Departname string
  163. }
  164. type UserinfoBooks struct {
  165. Id int64
  166. Pid int64
  167. IsOpen bool
  168. }
  169. assertSync(t, new(UserinfoCount2), new(UserinfoBooks))
  170. total, err := testEngine.SQL("SELECT count(id) FROM " + testEngine.TableName("userinfo_count2", true)).
  171. Count()
  172. assert.NoError(t, err)
  173. assert.EqualValues(t, 0, total)
  174. }
  175. func TestCountWithOthers(t *testing.T) {
  176. assert.NoError(t, prepareEngine())
  177. type CountWithOthers struct {
  178. Id int64
  179. Name string
  180. }
  181. assertSync(t, new(CountWithOthers))
  182. _, err := testEngine.Insert(&CountWithOthers{
  183. Name: "orderby",
  184. })
  185. assert.NoError(t, err)
  186. _, err = testEngine.Insert(&CountWithOthers{
  187. Name: "limit",
  188. })
  189. assert.NoError(t, err)
  190. total, err := testEngine.OrderBy("id desc").Limit(1).Count(new(CountWithOthers))
  191. assert.NoError(t, err)
  192. assert.EqualValues(t, 2, total)
  193. }
  194. type CountWithTableName struct {
  195. Id int64
  196. Name string
  197. }
  198. func (CountWithTableName) TableName() string {
  199. return "count_with_table_name1"
  200. }
  201. func TestWithTableName(t *testing.T) {
  202. assert.NoError(t, prepareEngine())
  203. assertSync(t, new(CountWithTableName))
  204. _, err := testEngine.Insert(&CountWithTableName{
  205. Name: "orderby",
  206. })
  207. assert.NoError(t, err)
  208. _, err = testEngine.Insert(CountWithTableName{
  209. Name: "limit",
  210. })
  211. assert.NoError(t, err)
  212. total, err := testEngine.OrderBy("id desc").Count(new(CountWithTableName))
  213. assert.NoError(t, err)
  214. assert.EqualValues(t, 2, total)
  215. total, err = testEngine.OrderBy("id desc").Count(CountWithTableName{})
  216. assert.NoError(t, err)
  217. assert.EqualValues(t, 2, total)
  218. }