THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

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.

277 lines
6.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. "fmt"
  7. "strconv"
  8. "testing"
  9. "xorm.io/builder"
  10. "github.com/stretchr/testify/assert"
  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. }