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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
// 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 (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"runtime/debug"
"strings"
"testing"
)
var (
oN = flag.Int("N", 0, "")
oM = flag.Int("M", 0, "")
oFastFail = flag.Bool("fastFail", false, "")
oSlow = flag.Bool("slow", false, "Do not wrap storage tests in a single outer transaction, write everything to disk file. Very slow.")
)
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) {
recset := rs.(recordset)
p := recset.plan
a0 := append([]string(nil), p.fieldNames()...)
for i, v := range a0 {
a0[i] = fmt.Sprintf("%q", v)
}
a := []string{strings.Join(a0, ", ")}
if err := p.do(recset.ctx, func(id interface{}, data []interface{}) (bool, error) {
if err = expand(data); err != nil {
return false, err
}
a = append(a, fmt.Sprintf("%v", data))
return true, nil
}); err != nil {
return "", err
}
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),
("Williams", NULL),
;
COMMIT;
`
func explained(db *DB, s stmt, tctx *TCtx) (string, error) {
src := "explain " + s.String()
rs, _, err := db.Run(tctx, src, int64(30))
if err != nil {
return "", err
}
rows, err := rs[0].Rows(-1, 0)
if err != nil {
return "", err
}
if !strings.HasPrefix(rows[0][0].(string), "┌") {
return "", nil
}
var a []string
for _, v := range rows {
a = append(a, v[0].(string))
}
return strings.Join(a, "\n"), nil
}
// Test provides a testing facility for alternative storage implementations.
// The s.setup should return a 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
}
tctx := NewRWCtx()
if !*oSlow {
if _, _, err := db.Execute(tctx, txBegin); err != nil {
t.Error(err)
return nil
}
}
if err = s.mark(); err != nil {
t.Error(err)
return
}
defer func() {
x := tctx
if *oSlow {
x = nil
}
if err = s.teardown(x); 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
}
var logf *os.File
hasLogf := false
noErrors := true
if _, ok := s.(*memTestDB); ok {
if logf, err = ioutil.TempFile("", "ql-test-log-"); err != nil {
t.Error(err)
return nil
}
hasLogf = true
} else {
if logf, err = os.Create(os.DevNull); err != nil {
t.Error(err)
return nil
}
}
defer func() {
if hasLogf && noErrors {
func() {
if _, err := logf.Seek(0, 0); err != nil {
t.Error(err)
return
}
dst, err := os.Create("testdata.log")
if err != nil {
t.Error(err)
return
}
if _, err := io.Copy(dst, logf); err != nil {
t.Error(err)
return
}
if err := dst.Close(); err != nil {
t.Error(err)
}
}()
}
nm := logf.Name()
if err := logf.Close(); err != nil {
t.Error(err)
}
if hasLogf {
if err := os.Remove(nm); err != nil {
t.Error(err)
}
}
}()
log := bufio.NewWriter(logf)
defer func() {
if err := log.Flush(); err != nil {
t.Error(err)
}
}()
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) && *oFastFail {
return
}
continue
}
for _, s := range list.l {
if err := testMentionedColumns(s); err != nil {
t.Error(itest, err)
return
}
}
s1 := list.String()
list1, err := Compile(s1)
if err != nil {
t.Errorf("recreated source does not compile: %v\n---- orig\n%s\n---- recreated\n%s", err, q, s1)
if *oFastFail {
return
}
continue
}
s2 := list1.String()
if g, e := s2, s1; g != e {
t.Errorf("recreated source is not idempotent\n---- orig\n%s\n---- recreated1\n%s\n---- recreated2\n%s", q, s1, s2)
if *oFastFail {
return
}
continue
}
if !func() (ok bool) {
tnl0 := db.tnl
defer func() {
s3 := list.String()
if g, e := s1, s3; g != e {
t.Errorf("#%d: execution mutates compiled statement list\n---- orig\n%s----new\n%s", itest, g, e)
}
if !ok {
noErrors = false
}
if noErrors {
hdr := false
for _, v := range list.l {
s, err := explained(db, v, tctx)
if err != nil {
t.Error(err)
return
}
if !strings.HasPrefix(s, "┌") {
continue
}
if !hdr {
fmt.Fprintf(log, "---- %v\n", itest)
hdr = true
}
fmt.Fprintf(log, "%s\n", v)
fmt.Fprintf(log, "%s\n\n", s)
}
}
tnl := db.tnl
if tnl != tnl0 {
panic(fmt.Errorf("internal error 057: tnl0 %v, tnl %v", tnl0, tnl))
}
nfo, err := db.Info()
if err != nil {
dbg("", err)
panic(err)
}
for _, idx := range nfo.Indices {
//dbg("#%d: cleanup index %s", itest, idx.Name)
if _, _, err = db.run(tctx, fmt.Sprintf(`
BEGIN TRANSACTION;
DROP INDEX %s;
COMMIT;
`,
idx.Name)); err != nil {
t.Errorf("#%d: cleanup DROP INDEX %s: %v", itest, idx.Name, err)
ok = false
}
}
for _, tab := range nfo.Tables {
//dbg("#%d: cleanup table %s", itest, tab.Name)
if _, _, err = db.run(tctx, fmt.Sprintf(`
BEGIN TRANSACTION;
DROP table %s;
COMMIT;
`,
tab.Name)); err != nil {
t.Errorf("#%d: cleanup DROP TABLE %s: %v", itest, tab.Name, err)
ok = false
}
}
db.hasIndex2 = 0
}()
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
}() && *oFastFail {
return
}
}
return
}
|