forked from xormplus/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_iterate_test.go
103 lines (87 loc) · 2.44 KB
/
session_iterate_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
// 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 (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIterate(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserIterate struct {
Id int64
IsMan bool
}
assert.NoError(t, testEngine.Sync2(new(UserIterate)))
cnt, err := testEngine.Insert(&UserIterate{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
cnt = 0
err = testEngine.Iterate(new(UserIterate), func(i int, bean interface{}) error {
user := bean.(*UserIterate)
assert.EqualValues(t, 1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
func TestBufferIterate(t *testing.T) {
assert.NoError(t, prepareEngine())
type UserBufferIterate struct {
Id int64
IsMan bool
}
assert.NoError(t, testEngine.Sync2(new(UserBufferIterate)))
var size = 20
for i := 0; i < size; i++ {
cnt, err := testEngine.Insert(&UserBufferIterate{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
var cnt = 0
err := testEngine.BufferSize(9).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
user := bean.(*UserBufferIterate)
assert.EqualValues(t, cnt+1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.NoError(t, err)
assert.EqualValues(t, size, cnt)
cnt = 0
err = testEngine.Limit(20).BufferSize(9).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
user := bean.(*UserBufferIterate)
assert.EqualValues(t, cnt+1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.NoError(t, err)
assert.EqualValues(t, size, cnt)
cnt = 0
err = testEngine.Limit(7).BufferSize(9).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
user := bean.(*UserBufferIterate)
assert.EqualValues(t, cnt+1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.NoError(t, err)
assert.EqualValues(t, 7, cnt)
cnt = 0
err = testEngine.Where("id <= 10").BufferSize(2).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
user := bean.(*UserBufferIterate)
assert.EqualValues(t, cnt+1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.NoError(t, err)
assert.EqualValues(t, 10, cnt)
}