-
Notifications
You must be signed in to change notification settings - Fork 19
/
iterator_test.go
186 lines (163 loc) · 4.19 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
174
175
176
177
178
179
180
181
182
183
184
185
186
package gockle
import (
"context"
"fmt"
"reflect"
"testing"
)
func TestIterator(t *testing.T) {
var s = newSession(t)
defer s.Close()
var exec = func(q string) {
if err := s.Exec(q); err != nil {
t.Fatalf("Actual error %v, expected no error", err)
}
}
exec(ksDropIf)
exec(ksCreate)
defer exec(ksDrop)
exec(tabCreate)
defer exec(tabDrop)
exec(rowInsert)
if i := s.ScanIterator("select * from gockle_test.test"); i == nil {
t.Error("Actual iterator nil, expected not nil")
} else {
var id, n int
if !i.Scan(&id, &n) {
t.Errorf("Actual more false, expected true")
}
if i.WillSwitchPage() {
t.Errorf("Actual WillSwitchPage true, expected false")
}
if state := i.PageState(); state != nil {
t.Errorf("Actual PageState not nil, expected nil")
}
if id != 1 {
t.Errorf("Actual id %v, expected 1", id)
}
if n != 2 {
t.Errorf("Actual n %v, expected 2", n)
}
if err := i.Close(); err != nil {
t.Errorf("Actual error %v, expected no error", err)
}
}
if i := s.ScanIterator("select * from gockle_test.test"); i == nil {
t.Error("Actual iterator nil, expected not nil")
} else {
var a = map[string]interface{}{}
if !i.ScanMap(a) {
t.Errorf("Actual more false, expected true")
}
if e := (map[string]interface{}{"id": 1, "n": 2}); !reflect.DeepEqual(a, e) {
t.Errorf("Actual map %v, expected %v", a, e)
}
if err := i.Close(); err != nil {
t.Errorf("Actual error %v, expected no error", err)
}
}
}
func TestIteratorPaging(t *testing.T) {
var s = newSession(t)
defer s.Close()
var exec = func(q string) {
if err := s.Exec(q); err != nil {
t.Fatalf("Actual error %v, expected no error", err)
}
}
exec(ksDropIf)
exec(ksCreate)
defer exec(ksDrop)
exec(tabCreate)
defer exec(tabDrop)
exec(rowInsert)
exec(rowInsert2)
q := s.Query("select * from gockle_test.test").
PageSize(1).
WithContext(context.Background())
var state []byte
if i := q.Iter(); i == nil {
t.Error("Actual iterator nil, expected not nil")
} else {
var id, n int
// First row
if !i.Scan(&id, &n) {
t.Errorf("Actual more false, expected true")
}
if !i.WillSwitchPage() {
t.Errorf("Actual WillSwitchPage false, expected true")
}
state = i.PageState()
if state == nil {
t.Errorf("Actual PageState nil, expected not nil")
}
if id != 1 {
t.Errorf("Actual id %v, expected 1", id)
}
if n != 2 {
t.Errorf("Actual n %v, expected 2", n)
}
}
// second row
q = q.PageState(state)
if i := q.Iter(); i == nil {
t.Error("Actual iterator nil, expected not nil")
} else {
var id, n int
if !i.Scan(&id, &n) {
t.Errorf("Actual more false, expected true")
}
if i.WillSwitchPage() {
t.Errorf("Actual WillSwitchPage true, expected false")
}
state = i.PageState()
if state == nil {
t.Errorf("Actual PageState nil, expected not nil")
}
if id != 3 {
t.Errorf("Actual id %v, expected 3", id)
}
if n != 4 {
t.Errorf("Actual n %v, expected 4", n)
}
if err := i.Close(); err != nil {
t.Errorf("Actual error %v, expected no error", err)
}
}
// third row (not existing)
q = q.PageState(state)
if i := q.Iter(); i == nil {
t.Error("Actual iterator nil, expected not nil")
} else {
var id, n int
if i.Scan(&id, &n) {
t.Errorf("Actual more true, expected false")
}
if i.WillSwitchPage() {
t.Errorf("Actual WillSwitchPage true, expected false")
}
state = i.PageState()
if state != nil {
t.Errorf("Actual PageState not nil, expected nil")
}
}
}
func TestIteratorMock(t *testing.T) {
var m, e = &IteratorMock{}, fmt.Errorf("e")
testMock(t, m, &m.Mock, []struct {
method string
arguments []interface{}
results []interface{}
}{
{"Close", nil, []interface{}{nil}},
{"Close", nil, []interface{}{e}},
{"Scan", []interface{}{[]interface{}(nil)}, []interface{}{false}},
{"Scan", []interface{}{[]interface{}{1}}, []interface{}{true}},
{"ScanMap", []interface{}{map[string]interface{}(nil)}, []interface{}{false}},
{"ScanMap", []interface{}{map[string]interface{}{"a": 1}}, []interface{}{true}},
{"WillSwitchPage", nil, []interface{}{false}},
{"WillSwitchPage", nil, []interface{}{true}},
{"PageState", nil, []interface{}{[]byte{}}},
{"PageState", nil, []interface{}{[]byte{1}}},
})
}