This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathstorage_test.go
330 lines (292 loc) · 6.27 KB
/
storage_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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) 2014 ql 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 ql
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"regexp"
"runtime/debug"
"strings"
"testing"
)
var (
oN = flag.Int("N", 0, "")
oM = flag.Int("M", 0, "")
)
var testdata []string
func init() {
tests, err := ioutil.ReadFile("testdata.ql")
if err != nil {
log.Panic(err)
}
a := bytes.Split(tests, []byte("\n-- "))
pre := []byte("-- ")
pres := []byte("S ")
for _, v := range a[1:] {
switch {
case bytes.HasPrefix(v, pres):
v = append(pre, v...)
v = append([]byte(sample), v...)
default:
v = append(pre, v...)
}
testdata = append(testdata, string(v))
}
}
func typeof(v interface{}) (r int) { //NTYPE
switch v.(type) {
case bool:
return qBool
case complex64:
return qComplex64
case complex128:
return qComplex128
case float32:
return qFloat32
case float64:
return qFloat64
case int8:
return qInt8
case int16:
return qInt16
case int32:
return qInt32
case int64:
return qInt64
case string:
return qString
case uint8:
return qUint8
case uint16:
return qUint16
case uint32:
return qUint32
case uint64:
return qUint64
}
return
}
func stypeof(nm string, val interface{}) string {
if t := typeof(val); t != 0 {
return fmt.Sprintf("%c%s", t, nm)
}
switch val.(type) {
case idealComplex:
return fmt.Sprintf("c%s", nm)
case idealFloat:
return fmt.Sprintf("f%s", nm)
case idealInt:
return fmt.Sprintf("l%s", nm)
case idealRune:
return fmt.Sprintf("k%s", nm)
case idealUint:
return fmt.Sprintf("x%s", nm)
default:
return fmt.Sprintf("?%s", nm)
}
}
func dumpCols(cols []*col) string {
a := []string{}
for _, col := range cols {
a = append(a, fmt.Sprintf("%d:%s %s", col.index, col.name, typeStr(col.typ)))
}
return strings.Join(a, ",")
}
func dumpFlds(flds []*fld) string {
a := []string{}
for _, fld := range flds {
a = append(a, fmt.Sprintf("%s AS %s", fld.expr, fld.name))
}
return strings.Join(a, ",")
}
func recSetDump(rs Recordset) (s string, err error) {
var state int
var a []string
var flds []*fld
rs2 := rs.(recordset)
if err = rs2.do(rs2.ctx, false, func(_ interface{}, rec []interface{}) (bool, error) {
switch state {
case 0:
flds = rec[0].([]*fld)
state++
case 1:
for i, v := range flds {
a = append(a, stypeof(v.name, rec[i]))
}
a = []string{strings.Join(a, ", ")}
state++
fallthrough
default:
if err = expand(rec); err != nil {
return false, err
}
a = append(a, fmt.Sprintf("%v", rec))
}
return true, nil
}); err != nil {
return
}
if state == 1 {
for _, v := range flds {
a = append(a, stypeof(v.name, nil))
}
a = []string{strings.Join(a, ", ")}
}
return strings.Join(a, "\n"), nil
}
// http://en.wikipedia.org/wiki/Join_(SQL)#Sample_tables
const sample = `
BEGIN TRANSACTION;
CREATE TABLE department (
DepartmentID int,
DepartmentName string,
);
INSERT INTO department VALUES
(31, "Sales"),
(33, "Engineering"),
(34, "Clerical"),
(35, "Marketing"),
;
CREATE TABLE employee (
LastName string,
DepartmentID int,
);
INSERT INTO employee VALUES
("Rafferty", 31),
("Jones", 33),
("Heisenberg", 33),
("Robinson", 34),
("Smith", 34),
("John", NULL),
;
COMMIT;
`
// Test provides a testing facility for alternative storage implementations.
// The storef should return freshly created and empty storage. Removing the
// store from the system is the responsibility of the caller. The test only
// guarantees not to panic on recoverable errors and return an error instead.
// Test errors are not returned but reported to t.
func test(t *testing.T, s testDB) (panicked error) {
defer func() {
if e := recover(); e != nil {
switch x := e.(type) {
case error:
panicked = x
default:
panicked = fmt.Errorf("%v", e)
}
}
if panicked != nil {
t.Errorf("PANIC: %v\n%s", panicked, debug.Stack())
}
}()
db, err := s.setup()
if err != nil {
t.Error(err)
return
}
if err = s.mark(); err != nil {
t.Error(err)
return
}
defer func() {
if err = s.teardown(); err != nil {
t.Error(err)
}
}()
chk := func(test int, err error, expErr string, re *regexp.Regexp) (ok bool) {
s := err.Error()
if re == nil {
t.Error("FAIL: ", test, s)
return false
}
if !re.MatchString(s) {
t.Error("FAIL: ", test, "error doesn't match:", s, "expected", expErr)
return false
}
return true
}
max := len(testdata)
if n := *oM; n != 0 && n < max {
max = n
}
for itest, test := range testdata[*oN:max] {
//dbg("---------------------------------------- itest %d", itest)
var re *regexp.Regexp
a := strings.Split(test+"|", "|")
q, rset := a[0], strings.TrimSpace(a[1])
var expErr string
if len(a) < 3 {
t.Error(itest, "internal error 066")
return
}
if expErr = a[2]; expErr != "" {
re = regexp.MustCompile("(?i:" + strings.TrimSpace(expErr) + ")")
}
q = strings.Replace(q, "∨", "|", -1)
q = strings.Replace(q, "⩖", "||", -1)
list, err := Compile(q)
if err != nil {
if !chk(itest, err, expErr, re) {
return
}
continue
}
tctx := NewRWCtx()
if !func() (ok bool) {
defer func() {
nfo, err := db.Info()
if err != nil {
panic(err)
}
for _, tab := range nfo.Tables {
if _, _, err = db.Run(NewRWCtx(), fmt.Sprintf(`
BEGIN TRANSACTION;
DROP table %s;
COMMIT;
`,
tab.Name)); err != nil {
panic(err)
}
}
}()
if err = s.mark(); err != nil {
t.Error(err)
return
}
rs, _, err := db.Execute(tctx, list, int64(30))
if err != nil {
return chk(itest, err, expErr, re)
}
if rs == nil {
t.Errorf("FAIL: %d: expected non nil Recordset or error %q", itest, expErr)
return
}
g, err := recSetDump(rs[len(rs)-1])
if err != nil {
return chk(itest, err, expErr, re)
}
if expErr != "" {
t.Errorf("FAIL: %d: expected error %q", itest, expErr)
return
}
a = strings.Split(rset, "\n")
for i, v := range a {
a[i] = strings.TrimSpace(v)
}
e := strings.Join(a, "\n")
if g != e {
t.Errorf("FAIL: test # %d\n%s\n---- g\n%s\n---- e\n%s\n----", itest, q, g, e)
return
}
return true
}() {
return
}
}
return
}