TestErrorCodeExtraction tests that error codes can be reliably extracted from errors returned by the parser using the IsCode helper.
(t *testing.T)
| 130 | // TestErrorCodeExtraction tests that error codes can be reliably extracted |
| 131 | // from errors returned by the parser using the IsCode helper. |
| 132 | func TestErrorCodeExtraction(t *testing.T) { |
| 133 | testCases := []struct { |
| 134 | name string |
| 135 | sql string |
| 136 | expectedCode errors.ErrorCode |
| 137 | }{ |
| 138 | { |
| 139 | name: "unexpected token after SELECT", |
| 140 | sql: "SELECT *** FROM users", |
| 141 | expectedCode: errors.ErrCodeExpectedToken, // Parser expects FROM, semicolon, or end of statement |
| 142 | }, |
| 143 | { |
| 144 | name: "missing FROM clause", |
| 145 | sql: "SELECT * users", |
| 146 | expectedCode: errors.ErrCodeExpectedToken, // Parser expects FROM keyword |
| 147 | }, |
| 148 | { |
| 149 | name: "invalid WHERE clause", |
| 150 | sql: "SELECT * FROM users WHERE", |
| 151 | expectedCode: errors.ErrCodeExpectedToken, // Expected expression after WHERE, got EOF |
| 152 | }, |
| 153 | } |
| 154 | |
| 155 | for _, tc := range testCases { |
| 156 | t.Run(tc.name, func(t *testing.T) { |
| 157 | // Tokenize |
| 158 | tkz := tokenizer.GetTokenizer() |
| 159 | defer tokenizer.PutTokenizer(tkz) |
| 160 | |
| 161 | tokens, err := tkz.Tokenize([]byte(tc.sql)) |
| 162 | if err != nil { |
| 163 | t.Skipf("Tokenizer error: %v", err) |
| 164 | } |
| 165 | |
| 166 | // Parse |
| 167 | p := parser.NewParser() |
| 168 | _, parseErr := p.ParseFromModelTokens(tokens) |
| 169 | |
| 170 | if parseErr == nil { |
| 171 | t.Fatalf("Expected error for SQL: %s", tc.sql) |
| 172 | } |
| 173 | |
| 174 | // Check error code using IsCode |
| 175 | if !errors.IsCode(parseErr, tc.expectedCode) { |
| 176 | if structErr, ok := parseErr.(*errors.Error); ok { |
| 177 | t.Errorf("IsCode returned false for code %s, error has code %s", tc.expectedCode, structErr.Code) |
| 178 | } else { |
| 179 | t.Errorf("Error is not structured, cannot extract code: %v", parseErr) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | t.Logf("Successfully verified error code %s matches error: %v", tc.expectedCode, parseErr) |
| 184 | }) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // TestErrorLocationPropagation tests that error location information |
| 189 | // propagates correctly from tokenizer through parser. |
nothing calls this directly
no test coverage detected