@@ -212,6 +212,76 @@ func TestHandlerOutput(t *testing.T) {
212212 })
213213}
214214
215+ func TestHandlerErrors (t * testing.T ) {
216+ e , pro := setupMemDB (require .New (t ))
217+ dbFunc := pro .Database
218+
219+ dummyConn := newConn (1 )
220+ handler := & Handler {
221+ e : e ,
222+ sm : NewSessionManager (
223+ testSessionBuilder (pro ),
224+ sql .NoopTracer ,
225+ dbFunc ,
226+ sql .NewMemoryManager (nil ),
227+ sqle .NewProcessList (),
228+ "foo" ,
229+ ),
230+ readTimeout : time .Second ,
231+ }
232+ handler .NewConnection (dummyConn )
233+
234+ type expectedValues struct {
235+ callsToCallback int
236+ lenLastBatch int
237+ lastRowsAffected uint64
238+ }
239+
240+ setupCommands := []string {"CREATE TABLE `test_table` ( `id` INT NOT NULL PRIMARY KEY, `v` INT );" }
241+
242+ tests := []struct {
243+ name string
244+ query string
245+ expectedErrorCode int
246+ }{
247+ {
248+ name : "insert with nonexistent field name" ,
249+ query : "INSERT INTO `test_table` (`id`, `v_`) VALUES (1, 2)" ,
250+ expectedErrorCode : mysql .ERBadFieldError ,
251+ },
252+ {
253+ name : "insert into nonexistent table" ,
254+ query : "INSERT INTO `test`.`no_such_table` (`id`, `v`) VALUES (1, 2)" ,
255+ expectedErrorCode : mysql .ERNoSuchTable ,
256+ },
257+ {
258+ name : "insert into same column twice" ,
259+ query : "INSERT INTO `test`.`test_table` (`id`, `id`, `v`) VALUES (1, 2, 3)" ,
260+ expectedErrorCode : mysql .ERFieldSpecifiedTwice ,
261+ },
262+ }
263+
264+ handler .ComInitDB (dummyConn , "test" )
265+ for _ , setupCommand := range setupCommands {
266+ err := handler .ComQuery (dummyConn , setupCommand , func (res * sqltypes.Result , more bool ) error {
267+ return nil
268+ })
269+ require .NoError (t , err )
270+ }
271+
272+ for _ , test := range tests {
273+ t .Run (test .name , func (t * testing.T ) {
274+ err := handler .ComQuery (dummyConn , test .query , func (res * sqltypes.Result , more bool ) error {
275+ return nil
276+ })
277+ require .NotNil (t , err )
278+ sqlErr , isSqlError := err .(* mysql.SQLError )
279+ require .True (t , isSqlError )
280+ require .Equal (t , test .expectedErrorCode , sqlErr .Number ())
281+ })
282+ }
283+ }
284+
215285// TestHandlerComReset asserts that the Handler.ComResetConnection method correctly clears all session
216286// state (e.g. table locks, prepared statements, user variables, session variables), and keeps the current
217287// database selected.
0 commit comments