-
Notifications
You must be signed in to change notification settings - Fork 19
/
session_test.go
269 lines (214 loc) · 6.93 KB
/
session_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package gockle
import (
"flag"
"fmt"
"reflect"
"testing"
"time"
"github.com/facebookgo/flagenv"
"github.com/gocql/gocql"
"github.com/maraino/go-mock"
)
const timeout = 5 * time.Second
const (
ksCreate = "create keyspace gockle_test with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};"
ksDrop = "drop keyspace gockle_test"
ksDropIf = "drop keyspace if exists gockle_test"
rowInsert = "insert into gockle_test.test (id, n) values (1, 2)"
tabCreate = "create table gockle_test.test(id int primary key, n int)"
tabDrop = "drop table gockle_test.test"
)
var protoVersion = flag.Int("proto-version", 4, "CQL protocol version")
func TestNewSession(t *testing.T) {
if a, e := NewSession(nil), (session{}); a != e {
t.Errorf("Actual session %v, expected %v", a, e)
}
var c = gocql.NewCluster("localhost")
c.ProtoVersion = *protoVersion
c.Timeout = timeout
var s, err = c.CreateSession()
if err != nil {
t.Skip(err)
}
if a, e := NewSession(s), (session{s: s}); a != e {
t.Errorf("Actual session %v, expected %v", a, e)
}
}
func TestSessionMetadata(t *testing.T) {
var exec = func(s Session, q string) {
if err := s.QueryExec(q); err != nil {
t.Errorf("Actual error %v, expected no error", err)
}
}
var s = newSession(t)
exec(s, ksDropIf)
exec(s, ksCreate)
exec(s, tabCreate)
s = newSession(t)
if a, err := s.Tables("gockle_test"); err == nil {
if e := ([]string{"test"}); !reflect.DeepEqual(a, e) {
t.Errorf("Actual tables %v, expected %v", a, e)
}
} else {
t.Errorf("Actual error %v, expected no error", err)
}
if a, err := s.Columns("gockle_test", "test"); err == nil {
var ts = map[string]gocql.Type{"id": gocql.TypeInt, "n": gocql.TypeInt}
if la, le := len(a), len(ts); la == le {
for n, at := range a {
if et, ok := ts[n]; ok {
if at.Type() != et {
t.Errorf("Actual type %v, expected %v", at, et)
}
} else {
t.Errorf("Actual name %v invalid, expected valid", n)
}
}
} else {
t.Errorf("Actual count %v, expected %v", la, le)
}
} else {
t.Errorf("Actual error %v, expected no error", err)
}
exec(s, tabDrop)
exec(s, ksDrop)
s.Close()
}
func TestSessionMock(t *testing.T) {
var m, e = &SessionMock{}, fmt.Errorf("e")
testMock(t, m, &m.Mock, []struct {
method string
arguments []interface{}
results []interface{}
}{
{"Close", nil, nil},
{"Columns", []interface{}{"", ""}, []interface{}{map[string]gocql.TypeInfo(nil), nil}},
{"Columns", []interface{}{"a", "b"}, []interface{}{map[string]gocql.TypeInfo{"c": gocql.NativeType{}}, e}},
{"QueryBatch", []interface{}{BatchKind(0)}, []interface{}{(*batch)(nil)}},
{"QueryBatch", []interface{}{BatchKind(1)}, []interface{}{&batch{}}},
{"QueryExec", []interface{}{"", []interface{}(nil)}, []interface{}{nil}},
{"QueryExec", []interface{}{"a", []interface{}{1}}, []interface{}{e}},
{"QueryIterator", []interface{}{"", []interface{}(nil)}, []interface{}{(*iterator)(nil)}},
{"QueryIterator", []interface{}{"a", []interface{}{1}}, []interface{}{iterator{}}},
{"QueryScan", []interface{}{"", []interface{}(nil), []interface{}(nil)}, []interface{}{nil}},
{"QueryScan", []interface{}{"a", []interface{}{1}, []interface{}{1}}, []interface{}{e}},
{"QueryScanMap", []interface{}{"", []interface{}(nil), map[string]interface{}(nil)}, []interface{}{nil}},
{"QueryScanMap", []interface{}{"a", []interface{}{1}, map[string]interface{}{"b": 2}}, []interface{}{e}},
{"QueryScanMapTx", []interface{}{"", []interface{}(nil), map[string]interface{}(nil)}, []interface{}{false, nil}},
{"QueryScanMapTx", []interface{}{"a", []interface{}{1}, map[string]interface{}{"b": 2}}, []interface{}{true, e}},
{"QuerySliceMap", []interface{}{"", []interface{}(nil)}, []interface{}{[]map[string]interface{}(nil), nil}},
{"QuerySliceMap", []interface{}{"a", []interface{}{1}}, []interface{}{[]map[string]interface{}{{"b": 2}}, e}},
{"Tables", []interface{}{""}, []interface{}{[]string(nil), nil}},
{"Tables", []interface{}{"a"}, []interface{}{[]string{"b"}, e}},
})
}
func TestSessionQuery(t *testing.T) {
var s = newSession(t)
var exec = func(q string) {
if err := s.QueryExec(q); err != nil {
t.Errorf("Actual error %v, expected no error", err)
}
}
exec(ksDropIf)
exec(ksCreate)
exec(tabCreate)
exec(rowInsert)
// QueryBatch
if s.QueryBatch(BatchKind(0)) == nil {
t.Error("Actual batch nil, expected not nil")
}
// QueryIterator
if s.QueryIterator("select * from gockle_test.test") == nil {
t.Error("Actual iterator nil, expected not nil")
}
// QueryScan
var id, n int
if err := s.QueryScan("select id, n from gockle_test.test", nil, []interface{}{&id, &n}); err == nil {
if id != 1 {
t.Errorf("Actual id %v, expected 1", id)
}
if n != 2 {
t.Errorf("Actual n %v, expected 2", n)
}
} else {
t.Errorf("Actual error %v, expected no error", err)
}
// QueryScanMap
var am, em = map[string]interface{}{}, map[string]interface{}{"id": 1, "n": 2}
if err := s.QueryScanMap("select id, n from gockle_test.test", nil, am); err == nil {
if !reflect.DeepEqual(am, em) {
t.Errorf("Actual map %v, expected %v", am, em)
}
} else {
t.Errorf("Actual error %v, expected no error", err)
}
// QueryScanMapTx
am = map[string]interface{}{}
if b, err := s.QueryScanMapTx("update gockle_test.test set n = 3 where id = 1 if n = 2", nil, am); err == nil {
if !b {
t.Error("Actual applied false, expected true")
}
if l := len(am); l != 0 {
t.Errorf("Actual length %v, expected 0", l)
}
} else {
t.Errorf("Actual error %v, expected no error", err)
}
// QuerySliceMap
var es = []map[string]interface{}{{"id": 1, "n": 3}}
if as, err := s.QuerySliceMap("select * from gockle_test.test"); err == nil {
if !reflect.DeepEqual(as, es) {
t.Errorf("Actual rows %v, expected %v", as, es)
}
} else {
t.Errorf("Actual error %v, expected no error", err)
}
exec(tabDrop)
exec(ksDrop)
s.Close()
}
func init() {
flag.Parse()
if err := flagenv.ParseSet("gockle_", flag.CommandLine); err != nil {
panic(err)
}
}
func newSession(t *testing.T) Session {
var c = gocql.NewCluster("localhost")
c.ProtoVersion = *protoVersion
c.Timeout = timeout
var s, err = c.CreateSession()
if err != nil {
t.Skip(err)
}
return NewSession(s)
}
func testMock(t *testing.T, i interface{}, m *mock.Mock, tests []struct {
method string
arguments []interface{}
results []interface{}
}) {
var v = reflect.ValueOf(i)
for _, test := range tests {
t.Log("Test:", test)
m.Reset()
m.When(test.method, test.arguments...).Return(test.results...)
var vs []reflect.Value
for _, a := range test.arguments {
vs = append(vs, reflect.ValueOf(a))
}
var method = v.MethodByName(test.method)
if method.Type().IsVariadic() {
vs = method.CallSlice(vs)
} else {
vs = method.Call(vs)
}
var is []interface{}
for _, v := range vs {
is = append(is, v.Interface())
}
if !reflect.DeepEqual(is, test.results) {
t.Errorf("Actual %v, expected %v", is, test.results)
}
}
}