session_sum_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. assert.NoError(t, prepareEngine())
  17. type SumStruct struct {
  18. Int int
  19. Float float32
  20. }
  21. var (
  22. cases = []SumStruct{
  23. {1, 6.2},
  24. {2, 5.3},
  25. {92, -0.2},
  26. }
  27. )
  28. var i int
  29. var f float32
  30. for _, v := range cases {
  31. i += v.Int
  32. f += v.Float
  33. }
  34. assert.NoError(t, testEngine.Sync2(new(SumStruct)))
  35. cnt, err := testEngine.Insert(cases)
  36. assert.NoError(t, err)
  37. assert.EqualValues(t, 3, cnt)
  38. colInt := testEngine.ColumnMapper.Obj2Table("Int")
  39. colFloat := testEngine.ColumnMapper.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. func TestSumCustomColumn(t *testing.T) {
  61. assert.NoError(t, prepareEngine())
  62. type SumStruct struct {
  63. Int int
  64. Float float32
  65. }
  66. var (
  67. cases = []SumStruct{
  68. {1, 6.2},
  69. {2, 5.3},
  70. {92, -0.2},
  71. }
  72. )
  73. assert.NoError(t, testEngine.Sync2(new(SumStruct)))
  74. cnt, err := testEngine.Insert(cases)
  75. assert.NoError(t, err)
  76. assert.EqualValues(t, 3, cnt)
  77. sumInt, err := testEngine.Sum(new(SumStruct),
  78. "CASE WHEN `int` <= 2 THEN `int` ELSE 0 END")
  79. assert.NoError(t, err)
  80. assert.EqualValues(t, 3, int(sumInt))
  81. }
  82. func TestCount(t *testing.T) {
  83. assert.NoError(t, prepareEngine())
  84. type UserinfoCount struct {
  85. Departname string
  86. }
  87. assert.NoError(t, testEngine.Sync2(new(UserinfoCount)))
  88. colName := testEngine.ColumnMapper.Obj2Table("Departname")
  89. var cond builder.Cond = builder.Eq{
  90. "`" + colName + "`": "dev",
  91. }
  92. total, err := testEngine.Where(cond).Count(new(UserinfoCount))
  93. assert.NoError(t, err)
  94. assert.EqualValues(t, 0, total)
  95. cnt, err := testEngine.Insert(&UserinfoCount{
  96. Departname: "dev",
  97. })
  98. assert.NoError(t, err)
  99. assert.EqualValues(t, 1, cnt)
  100. total, err = testEngine.Where(cond).Count(new(UserinfoCount))
  101. assert.NoError(t, err)
  102. assert.EqualValues(t, 1, total)
  103. }
  104. func TestSQLCount(t *testing.T) {
  105. assert.NoError(t, prepareEngine())
  106. type UserinfoCount2 struct {
  107. Id int64
  108. Departname string
  109. }
  110. type UserinfoBooks struct {
  111. Id int64
  112. Pid int64
  113. IsOpen bool
  114. }
  115. assertSync(t, new(UserinfoCount2), new(UserinfoBooks))
  116. total, err := testEngine.SQL("SELECT count(id) FROM userinfo_count2").
  117. Count()
  118. assert.NoError(t, err)
  119. assert.EqualValues(t, 0, total)
  120. }