forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_stats_test.go
161 lines (127 loc) · 3.85 KB
/
session_stats_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"context"
"fmt"
"strconv"
"testing"
"github.com/go-xorm/builder"
"github.com/stretchr/testify/assert"
)
func isFloatEq(i, j float64, precision int) bool {
return fmt.Sprintf("%."+strconv.Itoa(precision)+"f", i) == fmt.Sprintf("%."+strconv.Itoa(precision)+"f", j)
}
func TestSum(t *testing.T) {
assert.NoError(t, prepareEngine())
type SumStruct struct {
Int int
Float float32
}
var (
cases = []SumStruct{
{1, 6.2},
{2, 5.3},
{92, -0.2},
}
)
var i int
var f float32
for _, v := range cases {
i += v.Int
f += v.Float
}
assert.NoError(t, testEngine.Sync2(context.Background(), new(SumStruct)))
cnt, err := testEngine.Insert(context.Background(), cases)
assert.NoError(t, err)
assert.EqualValues(t, 3, cnt)
colInt := testEngine.GetColumnMapper().Obj2Table("Int")
colFloat := testEngine.GetColumnMapper().Obj2Table("Float")
sumInt, err := testEngine.Sum(context.Background(), new(SumStruct), colInt)
assert.NoError(t, err)
assert.EqualValues(t, int(sumInt), i)
sumFloat, err := testEngine.Sum(context.Background(), new(SumStruct), colFloat)
assert.NoError(t, err)
assert.Condition(t, func() bool {
return isFloatEq(sumFloat, float64(f), 2)
})
sums, err := testEngine.Sums(context.Background(), new(SumStruct), colInt, colFloat)
assert.NoError(t, err)
assert.EqualValues(t, 2, len(sums))
assert.EqualValues(t, i, int(sums[0]))
assert.Condition(t, func() bool {
return isFloatEq(sums[1], float64(f), 2)
})
sumsInt, err := testEngine.SumsInt(context.Background(), new(SumStruct), colInt)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(sumsInt))
assert.EqualValues(t, i, int(sumsInt[0]))
}
func TestSumCustomColumn(t *testing.T) {
assert.NoError(t, prepareEngine())
type SumStruct2 struct {
Int int
Float float32
}
var (
cases = []SumStruct2{
{1, 6.2},
{2, 5.3},
{92, -0.2},
}
)
assert.NoError(t, testEngine.Sync2(context.Background(), new(SumStruct2)))
cnt, err := testEngine.Insert(context.Background(), cases)
assert.NoError(t, err)
assert.EqualValues(t, 3, cnt)
sumInt, err := testEngine.Sum(context.Background(), new(SumStruct2),
"CASE WHEN `int` <= 2 THEN `int` ELSE 0 END")
assert.NoError(t, err)
assert.EqualValues(t, 3, int(sumInt))
}
func TestCount(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserinfoCount struct {
Departname string
}
assert.NoError(t, testEngine.Sync2(context.Background(), new(UserinfoCount)))
colName := testEngine.GetColumnMapper().Obj2Table("Departname")
var cond builder.Cond = builder.Eq{
"`" + colName + "`": "dev",
}
total, err := testEngine.Where(cond).Count(context.Background(), new(UserinfoCount))
assert.NoError(t, err)
assert.EqualValues(t, 0, total)
cnt, err := testEngine.Insert(context.Background(), &UserinfoCount{
Departname: "dev",
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
total, err = testEngine.Where(cond).Count(context.Background(), new(UserinfoCount))
assert.NoError(t, err)
assert.EqualValues(t, 1, total)
total, err = testEngine.Where(cond).Table("userinfo_count").Count(context.Background())
assert.NoError(t, err)
assert.EqualValues(t, 1, total)
total, err = testEngine.Table("userinfo_count").Count(context.Background())
assert.NoError(t, err)
assert.EqualValues(t, 1, total)
}
func TestSQLCount(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserinfoCount2 struct {
Id int64
Departname string
}
type UserinfoBooks struct {
Id int64
Pid int64
IsOpen bool
}
assertSync(t, new(UserinfoCount2), new(UserinfoBooks))
total, err := testEngine.SQL("SELECT count(id) FROM userinfo_count2").
Count(context.Background())
assert.NoError(t, err)
assert.EqualValues(t, 0, total)
}