-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqueryParser_test.go
86 lines (81 loc) · 2.39 KB
/
queryParser_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
package main
import (
"testing"
)
func TestQueryParser(t *testing.T) {
var tests = []struct {
input string
outcome string
err string
}{
{"", "EmptyQuery", ""},
{"--", "EmptyQuery", ""},
{"/**/", "EmptyQuery", ""},
{"/*/**/*/", "EmptyQuery", ""},
{"/*/**/", "error", errQueryParserUnexpectedEOF.Error()},
{"\xFF\x00", "error", errQueryParserInputNotUtf8.Error()},
{"-", "error", errQueryParserUnexpectedEOF.Error()},
{"select", "error", errQueryParserUnexpectedEOF.Error()},
{"select 1", "TrivialSelect", ""},
{"select 1;", "TrivialSelect", ""},
{"select 1 ", "TrivialSelect", ""},
{"select 1 --", "TrivialSelect", ""},
{"select 1 /*/**/*/", "TrivialSelect", ""},
{"select 1; ", "TrivialSelect", ""},
{"select 1; f", "error", "garbage after semicolon"},
{"select 2", "error", "parse error"},
{`listen "foo"`, "ListenRequest", ""},
{`listen *`, "error", `parse error: unexpected token "asterisk"`},
{`unlisten *`, "UnlistenRequest", ""},
{"notify", "error", `parse error at or near "notify"`},
}
for n, ts := range tests {
q, err := ParseQuery(ts.input)
switch ts.outcome {
case "error":
if err == nil {
t.Errorf("test %d failed: err is nil; expected %s", n, ts.err)
} else if err.Error() != ts.err {
t.Errorf("test %d failed: err %q != %q", n, err.Error(), ts.err)
}
case "EmptyQuery":
if err != nil {
t.Errorf("test %d failed: unexpected error %q", n, err)
} else {
_, ok := q.(emptyQuery)
if !ok {
t.Errorf("test %d failed: unexpected msg %+#v; was expecting EmptyQuery", n, q)
}
}
case "ListenRequest":
if err != nil {
t.Errorf("test %d failed: unexpected error %q", n, err)
} else {
_, ok := q.(listenRequest)
if !ok {
t.Errorf("test %d failed: unexpected msg %+#v; was expecting ListenRequest", n, q)
}
}
case "UnlistenRequest":
if err != nil {
t.Errorf("test %d failed: unexpected error %q", n, err)
} else {
_, ok := q.(unlistenRequest)
if !ok {
t.Errorf("test %d failed: unexpected msg %+#v; was expecting UnlistenRequest", n, q)
}
}
case "TrivialSelect":
if err != nil {
t.Errorf("test %d failed: unexpected error %q", n, err)
} else {
_, ok := q.(trivialSelect)
if !ok {
t.Errorf("test %d failed: unexpected msg %+#v; was expecting TrivialSelect", n, q)
}
}
default:
t.Fatalf("unexpected outcome %q", ts.outcome)
}
}
}