Debian Codegraph @

parse_next_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqlparser
18
19 import (
20 "bytes"
21 "io"
22 "strings"
23 "testing"
24 )
25
26 // TestParseNextValid concatenates all the valid SQL test cases and check it can read
27 // them as one long string.
28 func TestParseNextValid(t *testing.T) {
29 var sql bytes.Buffer
30 for _, tcase := range validSQL {
31 sql.WriteString(strings.TrimSuffix(tcase.input, ";"))
32 sql.WriteRune(';')
33 }
34
35 tokens := NewTokenizer(&sql)
36 for i, tcase := range validSQL {
37 input := tcase.input + ";"
38 want := tcase.output
39 if want == "" {
40 want = tcase.input
41 }
42
43 tree, err := ParseNext(tokens)
44 if err != nil {
45 t.Fatalf("[%d] ParseNext(%q) err: %q, want nil", i, input, err)
46 continue
47 }
48
49 if got := String(tree); got != want {
50 t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, input, got, want)
51 }
52 }
53
54 // Read once more and it should be EOF.
55 if tree, err := ParseNext(tokens); err != io.EOF {
56 t.Errorf("ParseNext(tokens) = (%q, %v) want io.EOF", String(tree), err)
57 }
58 }
59
60 // TestParseNextErrors tests all the error cases, and ensures a valid
61 // SQL statement can be passed afterwards.
62 func TestParseNextErrors(t *testing.T) {
63 for _, tcase := range invalidSQL {
64 if tcase.excludeMulti {
65 // Skip tests which leave unclosed strings, or comments.
66 continue
67 }
68
69 sql := tcase.input + "; select 1 from t"
70 tokens := NewStringTokenizer(sql)
71
72 // The first statement should be an error
73 _, err := ParseNext(tokens)
74 if err == nil || err.Error() != tcase.output {
75 t.Fatalf("[0] ParseNext(%q) err: %q, want %q", sql, err, tcase.output)
76 continue
77 }
78
79 // The second should be valid
80 tree, err := ParseNext(tokens)
81 if err != nil {
82 t.Fatalf("[1] ParseNext(%q) err: %q, want nil", sql, err)
83 continue
84 }
85
86 want := "select 1 from t"
87 if got := String(tree); got != want {
88 t.Fatalf("[1] ParseNext(%q) = %q, want %q", sql, got, want)
89 }
90
91 // Read once more and it should be EOF.
92 if tree, err := ParseNext(tokens); err != io.EOF {
93 t.Errorf("ParseNext(tokens) = (%q, %v) want io.EOF", String(tree), err)
94 }
95 }
96 }
97
98 // TestParseNextEdgeCases tests various ParseNext edge cases.
99 func TestParseNextEdgeCases(t *testing.T) {
100 tests := []struct {
101 name string
102 input string
103 want []string
104 }{{
105 name: "Trailing ;",
106 input: "select 1 from a; update a set b = 2;",
107 want: []string{"select 1 from a", "update a set b = 2"},
108 }, {
109 name: "No trailing ;",
110 input: "select 1 from a; update a set b = 2",
111 want: []string{"select 1 from a", "update a set b = 2"},
112 }, {
113 name: "Trailing whitespace",
114 input: "select 1 from a; update a set b = 2 ",
115 want: []string{"select 1 from a", "update a set b = 2"},
116 }, {
117 name: "Trailing whitespace and ;",
118 input: "select 1 from a; update a set b = 2 ; ",
119 want: []string{"select 1 from a", "update a set b = 2"},
120 }, {
121 name: "Handle ForceEOF statements",
122 input: "set character set utf8; select 1 from a",
123 want: []string{"set charset 'utf8'", "select 1 from a"},
124 }, {
125 name: "Semicolin inside a string",
126 input: "set character set ';'; select 1 from a",
127 want: []string{"set charset ';'", "select 1 from a"},
128 }, {
129 name: "Partial DDL",
130 input: "create table a; select 1 from a",
131 want: []string{"create table a", "select 1 from a"},
132 }, {
133 name: "Partial DDL",
134 input: "create table a ignore me this is garbage; select 1 from a",
135 want: []string{"create table a", "select 1 from a"},
136 }}
137
138 for _, test := range tests {
139 tokens := NewStringTokenizer(test.input)
140
141 for i, want := range test.want {
142 tree, err := ParseNext(tokens)
143 if err != nil {
144 t.Fatalf("[%d] ParseNext(%q) err = %q, want nil", i, test.input, err)
145 continue
146 }
147
148 if got := String(tree); got != want {
149 t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, test.input, got, want)
150 }
151 }
152
153 // Read once more and it should be EOF.
154 if tree, err := ParseNext(tokens); err != io.EOF {
155 t.Errorf("ParseNext(%q) = (%q, %v) want io.EOF", test.input, String(tree), err)
156 }
157
158 // And again, once more should be EOF.
159 if tree, err := ParseNext(tokens); err != io.EOF {
160 t.Errorf("ParseNext(%q) = (%q, %v) want io.EOF", test.input, String(tree), err)
161 }
162 }
163 }