forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
173 lines (143 loc) · 3.77 KB
/
iterator_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
162
163
164
165
166
167
168
169
170
171
172
173
package rel
import (
"context"
"errors"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIterator(t *testing.T) {
var (
user User
adapter = &testAdapter{}
query = From("users")
cur1 = createCursor(5)
cur2 = createCursor(5)
cur3 = createCursor(3)
options = []IteratorOption{BatchSize(5)}
it = newIterator(context.TODO(), adapter, query, options)
)
query = query.From("users").SortAsc("id").Limit(5)
adapter.On("Query", query).Return(cur1, nil).Once()
adapter.On("Query", query.Offset(5)).Return(cur2, nil).Once()
adapter.On("Query", query.Offset(10)).Return(cur3, nil).Once()
entitiesCount := 0
for {
if err := it.Next(&user); err == io.EOF {
break
} else {
assert.Nil(t, err)
}
assert.NotEqual(t, 0, user.ID)
entitiesCount++
}
it.Close()
assert.Equal(t, 13, entitiesCount)
// the last next is not called because it's already refetched.
// call here to make expectation pass.
cur1.Next()
cur2.Next()
adapter.AssertExpectations(t)
cur1.AssertExpectations(t)
cur2.AssertExpectations(t)
cur3.AssertExpectations(t)
}
func TestIterator_setTableName(t *testing.T) {
var (
user User
adapter = &testAdapter{}
query = Query{}
cur = createCursor(1)
it = newIterator(context.TODO(), adapter, query, nil)
)
adapter.On("Query", query.From("users").SortAsc("id").Limit(1000)).Return(cur, nil).Once()
for {
if err := it.Next(&user); err == io.EOF {
break
} else {
assert.Nil(t, err)
}
assert.NotEqual(t, 0, user.ID)
}
it.Close()
adapter.AssertExpectations(t)
cur.AssertExpectations(t)
}
func TestIterator_setStartAndFinishID(t *testing.T) {
var (
user User
adapter = &testAdapter{}
query = From("users")
cur = createCursor(1)
options = []IteratorOption{Start(10), Finish(20)}
it = newIterator(context.TODO(), adapter, query, options)
)
adapter.On("Query", query.Where(Gte("id", 10).AndLte("id", 20)).SortAsc("id").Limit(1000)).Return(cur, nil).Once()
for {
if err := it.Next(&user); err == io.EOF {
break
} else {
assert.Nil(t, err)
}
assert.NotEqual(t, 0, user.ID)
}
it.Close()
adapter.AssertExpectations(t)
cur.AssertExpectations(t)
}
func TestIterator_cursorFieldsError(t *testing.T) {
var (
user User
adapter = &testAdapter{}
query = From("users")
cur = &testCursor{}
it = newIterator(context.TODO(), adapter, query, nil)
err = errors.New("cursor error")
)
adapter.On("Query", query.SortAsc("id").Limit(1000)).Return(cur, nil).Once()
cur.On("Fields").Return([]string{}, err)
defer it.Close()
for {
if err := it.Next(&user); err == io.EOF {
break
} else {
assert.Equal(t, err, err)
}
assert.Equal(t, 0, user.ID)
break
}
adapter.AssertExpectations(t)
cur.AssertExpectations(t)
}
func TestIterator_queryError(t *testing.T) {
var (
user User
adapter = &testAdapter{}
query = From("users")
cur = &testCursor{}
it = newIterator(context.TODO(), adapter, query, nil)
err = errors.New("query error")
)
adapter.On("Query", query.SortAsc("id").Limit(1000)).Return(cur, err).Once()
defer it.Close()
for {
if err := it.Next(&user); err == io.EOF {
break
} else {
assert.Equal(t, err, err)
}
assert.Equal(t, 0, user.ID)
break
}
adapter.AssertExpectations(t)
}
func TestIteratorOption_String(t *testing.T) {
assert.Equal(t, "rel.BatchSize(10)", fmt.Sprint(BatchSize(10)))
assert.Equal(t, "rel.Start(20)", fmt.Sprint(Start(20)))
assert.Equal(t, "rel.Start(20, 21)", fmt.Sprint(Start(20, 21)))
assert.Equal(t, "rel.Start(\"abc\")", fmt.Sprint(Start("abc")))
assert.Equal(t, "rel.Finish(30)", fmt.Sprint(Finish(30)))
assert.Equal(t, "rel.Finish(30, 31)", fmt.Sprint(Finish(30, 31)))
assert.Equal(t, "rel.Finish(\"def\")", fmt.Sprint(Finish("def")))
}