MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / TestErrorCodeExtraction

Function TestErrorCodeExtraction

pkg/errors/integration_test.go:132–186  ·  view source on GitHub ↗

TestErrorCodeExtraction tests that error codes can be reliably extracted from errors returned by the parser using the IsCode helper.

(t *testing.T)

Source from the content-addressed store, hash-verified

130// TestErrorCodeExtraction tests that error codes can be reliably extracted
131// from errors returned by the parser using the IsCode helper.
132func 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.

Callers

nothing calls this directly

Calls 9

ParseFromModelTokensMethod · 0.95
GetTokenizerFunction · 0.92
PutTokenizerFunction · 0.92
NewParserFunction · 0.92
IsCodeFunction · 0.92
RunMethod · 0.80
TokenizeMethod · 0.80
FatalfMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected