GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sqlparser/parse_next_test.go at master · yext-eng/sqlparser Expand file tree/ Copy pathparse_next_test.goMore file actions 179 lines (156 loc) · 5.19 KB / Copy pathparse_next_test.goFile metadata and controls179 lines (156 loc) · 5.19 KB 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 /* Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package sqlparser import ( "bytes" "io" "strings" "testing" ) // TestParseNextValid concatenates all the valid SQL test cases and check it can read // them as one long string. func TestParseNextValid(t *testing.T) { var sql bytes.Buffer for _, tcase := range validSQL { sql.WriteString(strings.TrimSuffix(tcase.input, ";")) sql.WriteRune(';') } tokens := NewTokenizer(&sql) for i, tcase := range validSQL { input := tcase.input + ";" want := tcase.output if want == "" { want = tcase.input } tree, err := ParseNext(tokens) if err != nil { t.Fatalf("[%d] ParseNext(%q) err: %q, want nil", i, input, err) continue } if got := String(tree); got != want { t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, input, got, want) } } // Read once more and it should be EOF. if tree, err := ParseNext(tokens); err != io.EOF { t.Errorf("ParseNext(tokens) = (%q, %v) want io.EOF", String(tree), err) } } // TestParseNextErrors tests all the error cases, and ensures a valid // SQL statement can be passed afterwards. func TestParseNextErrors(t *testing.T) { for _, tcase := range invalidSQL { if tcase.excludeMulti { // Skip tests which leave unclosed strings, or comments. continue } sql := tcase.input + "; select 1 from t" tokens := NewStringTokenizer(sql) // The first statement should be an error _, err := ParseNext(tokens) if err == nil || err.Error() != tcase.output { t.Fatalf("[0] ParseNext(%q) err: %q, want %q", sql, err, tcase.output) continue } // The second should be valid tree, err := ParseNext(tokens) if err != nil { t.Fatalf("[1] ParseNext(%q) err: %q, want nil", sql, err) continue } want := "select 1 from t" if got := String(tree); got != want { t.Fatalf("[1] ParseNext(%q) = %q, want %q", sql, got, want) } // Read once more and it should be EOF. if tree, err := ParseNext(tokens); err != io.EOF { t.Errorf("ParseNext(tokens) = (%q, %v) want io.EOF", String(tree), err) } } } // TestParseNextEdgeCases tests various ParseNext edge cases. func TestParseNextEdgeCases(t *testing.T) { tests := []struct { name string input string want []string firstErr string }{{ name: "Trailing ;", input: "select 1 from a; update a set b = 2;", want: []string{"select 1 from a", "update a set b = 2"}, }, { name: "No trailing ;", input: "select 1 from a; update a set b = 2", want: []string{"select 1 from a", "update a set b = 2"}, }, { name: "Trailing whitespace", input: "select 1 from a; update a set b = 2 ", want: []string{"select 1 from a", "update a set b = 2"}, }, { name: "Trailing whitespace and ;", input: "select 1 from a; update a set b = 2 ; ", want: []string{"select 1 from a", "update a set b = 2"}, }, { name: "Handle ForceEOF statements", input: "set character set utf8; select 1 from a", want: []string{"set charset 'utf8'", "select 1 from a"}, }, { name: "Handle trigger DDL statements", input: "drop trigger if exists trg_one; create trigger trg_two after insert on tbl_one for each row set @v = 1; select 1 from a", want: []string{"drop trigger if exists trg_one", "create trigger trg_two", "select 1 from a"}, }, { name: "Semicolin inside a string", input: "set character set ';'; select 1 from a", want: []string{"set charset ';'", "select 1 from a"}, }, { name: "Invalid DDL", input: "create table a; select 1 from a", firstErr: "syntax error at position 15", want: []string{"select 1 from a"}, }, { name: "Invalid DDL with trailing garbage", input: "create table a ignore me this is garbage; select 1 from a", firstErr: "syntax error at position 22 near 'ignore'", want: []string{"select 1 from a"}, }} for _, test := range tests { tokens := NewStringTokenizer(test.input) i := 0 if test.firstErr != "" { if _, err := ParseNext(tokens); err == nil || err.Error() != test.firstErr { t.Fatalf("[%d] ParseNext(%q) err = %q, want %q", i, test.input, err, test.firstErr) } i++ } for _, want := range test.want { tree, err := ParseNext(tokens) if err != nil { t.Fatalf("[%d] ParseNext(%q) err = %q, want nil", i, test.input, err) continue } if got := String(tree); got != want { t.Fatalf("[%d] ParseNext(%q) = %q, want %q", i, test.input, got, want) } i++ } // Read once more and it should be EOF. if tree, err := ParseNext(tokens); err != io.EOF { t.Errorf("ParseNext(%q) = (%q, %v) want io.EOF", test.input, String(tree), err) } // And again, once more should be EOF. if tree, err := ParseNext(tokens); err != io.EOF { t.Errorf("ParseNext(%q) = (%q, %v) want io.EOF", test.input, String(tree), err) } } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |