@@ -326,55 +326,45 @@ def test_database_timezone_changes_synced_to_connection
326
326
end
327
327
328
328
def test_ignores_warnings_when_behaviour_ignore
329
- ActiveRecord . db_warnings_action = :ignore
330
-
331
- result = @conn . execute ( 'SELECT 1 + "foo"' )
332
-
333
- assert_equal [ 1 ] , result . to_a . first
334
- ensure
335
- ActiveRecord . db_warnings_action = @original_db_warnings_action
329
+ with_db_warnings_action ( :ignore ) do
330
+ result = @conn . execute ( 'SELECT 1 + "foo"' )
331
+ assert_equal [ 1 ] , result . to_a . first
332
+ end
336
333
end
337
334
338
335
def test_logs_warnings_when_behaviour_log
339
- ActiveRecord . db_warnings_action = :log
340
-
341
- mysql_warning = "[ActiveRecord::SQLWarning] Truncated incorrect DOUBLE value: 'foo' (1292)"
336
+ with_db_warnings_action ( :log ) do
337
+ mysql_warning = "[ActiveRecord::SQLWarning] Truncated incorrect DOUBLE value: 'foo' (1292)"
342
338
343
- assert_called_with ( ActiveRecord ::Base . logger , :warn , [ mysql_warning ] ) do
344
- @conn . execute ( 'SELECT 1 + "foo"' )
339
+ assert_called_with ( ActiveRecord ::Base . logger , :warn , [ mysql_warning ] ) do
340
+ @conn . execute ( 'SELECT 1 + "foo"' )
341
+ end
345
342
end
346
- ensure
347
- ActiveRecord . db_warnings_action = @original_db_warnings_action
348
343
end
349
344
350
345
def test_raises_warnings_when_behaviour_raise
351
- ActiveRecord . db_warnings_action = :raise
352
-
353
- assert_raises ( ActiveRecord :: SQLWarning ) do
354
- @conn . execute ( 'SELECT 1 + "foo"' )
346
+ with_db_warnings_action ( :raise ) do
347
+ assert_raises ( ActiveRecord :: SQLWarning ) do
348
+ @conn . execute ( 'SELECT 1 + "foo"' )
349
+ end
355
350
end
356
- ensure
357
- ActiveRecord . db_warnings_action = @original_db_warnings_action
358
351
end
359
352
360
353
def test_reports_when_behaviour_report
361
- ActiveRecord . db_warnings_action = :report
362
-
363
- error_reporter = ActiveSupport ::ErrorReporter . new
364
- subscriber = ActiveSupport ::ErrorReporter ::TestHelper ::ErrorSubscriber . new
354
+ with_db_warnings_action ( :report ) do
355
+ error_reporter = ActiveSupport ::ErrorReporter . new
356
+ subscriber = ActiveSupport ::ErrorReporter ::TestHelper ::ErrorSubscriber . new
365
357
366
- Rails . define_singleton_method ( :error ) { error_reporter }
367
- Rails . error . subscribe ( subscriber )
358
+ Rails . define_singleton_method ( :error ) { error_reporter }
359
+ Rails . error . subscribe ( subscriber )
368
360
369
- @conn . execute ( 'SELECT 1 + "foo"' )
361
+ @conn . execute ( 'SELECT 1 + "foo"' )
370
362
371
- warning_event , * = subscriber . events . first
363
+ warning_event , * = subscriber . events . first
372
364
373
- assert_kind_of ActiveRecord ::SQLWarning , warning_event
374
- assert_equal "Truncated incorrect DOUBLE value: 'foo'" , warning_event . message
375
- ensure
376
- Rails . singleton_class . remove_method ( :error )
377
- ActiveRecord . db_warnings_action = @original_db_warnings_action
365
+ assert_kind_of ActiveRecord ::SQLWarning , warning_event
366
+ assert_equal "Truncated incorrect DOUBLE value: 'foo'" , warning_event . message
367
+ end
378
368
end
379
369
380
370
def test_warnings_behaviour_can_be_customized_with_a_proc
@@ -391,64 +381,68 @@ def test_warnings_behaviour_can_be_customized_with_a_proc
391
381
end
392
382
393
383
def test_allowlist_of_warnings_to_ignore
394
- old_ignored_warnings = ActiveRecord . db_warnings_ignore
395
- ActiveRecord . db_warnings_action = :raise
396
- ActiveRecord . db_warnings_ignore = [ /Truncated incorrect DOUBLE value/ ]
384
+ with_db_warnings_action ( :raise , [ /Truncated incorrect DOUBLE value/ ] ) do
385
+ result = @conn . execute ( 'SELECT 1 + "foo"' )
397
386
398
- result = @conn . execute ( 'SELECT 1 + "foo"' )
387
+ assert_equal [ 1 ] , result . to_a . first
388
+ end
389
+ end
399
390
400
- assert_equal [ 1 ] , result . to_a . first
401
- ensure
402
- ActiveRecord . db_warnings_action = @original_db_warnings_action
403
- ActiveRecord . db_warnings_ignore = old_ignored_warnings
391
+ def test_allowlist_of_warning_codes_to_ignore
392
+ with_db_warnings_action ( :raise , [ "1062" ] ) do
393
+ row_id = @conn . insert ( "INSERT INTO posts (title, body) VALUES('Title', 'Body')" )
394
+ result = @conn . execute ( "INSERT IGNORE INTO posts (id, title, body) VALUES(#{ row_id } , 'Title', 'Body')" )
395
+
396
+ assert_nil result
397
+ end
404
398
end
405
399
406
400
def test_does_not_raise_note_level_warnings
407
- ActiveRecord . db_warnings_action = :raise
408
-
409
- result = @conn . execute ( "DROP TABLE IF EXISTS non_existent_table" )
401
+ with_db_warnings_action ( :raise ) do
402
+ result = @conn . execute ( "DROP TABLE IF EXISTS non_existent_table" )
410
403
411
- assert_equal [ ] , result . to_a
412
- ensure
413
- ActiveRecord . db_warnings_action = @original_db_warnings_action
404
+ assert_equal [ ] , result . to_a
405
+ end
414
406
end
415
407
416
408
def test_warnings_do_not_change_returned_value_of_exec_update
417
409
previous_logger = ActiveRecord ::Base . logger
418
- ActiveRecord ::Base . logger = ActiveSupport ::Logger . new ( nil )
419
- ActiveRecord . db_warnings_action = :log
420
-
421
- # Mysql2 will raise an error when attempting to perform an update that warns if the sql_mode is set to strict
422
410
old_sql_mode = @conn . query_value ( "SELECT @@SESSION.sql_mode" )
423
- @conn . execute ( "SET @@SESSION.sql_mode=''" )
424
411
425
- @conn . execute ( "INSERT INTO posts (title, body) VALUES('Title', 'Body')" )
426
- result = @conn . update ( "UPDATE posts SET title = 'Updated' WHERE id > (0+'foo') LIMIT 1" )
412
+ with_db_warnings_action ( :log ) do
413
+ ActiveRecord ::Base . logger = ActiveSupport ::Logger . new ( nil )
414
+
415
+ # Mysql2 will raise an error when attempting to perform an update that warns if the sql_mode is set to strict
416
+ @conn . execute ( "SET @@SESSION.sql_mode=''" )
417
+
418
+ @conn . execute ( "INSERT INTO posts (title, body) VALUES('Title', 'Body')" )
419
+ result = @conn . update ( "UPDATE posts SET title = 'Updated' WHERE id > (0+'foo') LIMIT 1" )
427
420
428
- assert_equal 1 , result
421
+ assert_equal 1 , result
422
+ end
429
423
ensure
430
424
@conn . execute ( "SET @@SESSION.sql_mode='#{ old_sql_mode } '" )
431
425
ActiveRecord ::Base . logger = previous_logger
432
- ActiveRecord . db_warnings_action = @original_db_warnings_action
433
426
end
434
427
435
428
def test_warnings_do_not_change_returned_value_of_exec_delete
436
429
previous_logger = ActiveRecord ::Base . logger
437
- ActiveRecord ::Base . logger = ActiveSupport ::Logger . new ( nil )
438
- ActiveRecord . db_warnings_action = :log
439
-
440
- # Mysql2 will raise an error when attempting to perform a delete that warns if the sql_mode is set to strict
441
430
old_sql_mode = @conn . query_value ( "SELECT @@SESSION.sql_mode" )
442
- @conn . execute ( "SET @@SESSION.sql_mode=''" )
443
431
444
- @conn . execute ( "INSERT INTO posts (title, body) VALUES('Title', 'Body')" )
445
- result = @conn . delete ( "DELETE FROM posts WHERE id > (0+'foo') LIMIT 1" )
432
+ with_db_warnings_action ( :log ) do
433
+ ActiveRecord ::Base . logger = ActiveSupport ::Logger . new ( nil )
434
+
435
+ # Mysql2 will raise an error when attempting to perform a delete that warns if the sql_mode is set to strict
436
+ @conn . execute ( "SET @@SESSION.sql_mode=''" )
437
+
438
+ @conn . execute ( "INSERT INTO posts (title, body) VALUES('Title', 'Body')" )
439
+ result = @conn . delete ( "DELETE FROM posts WHERE id > (0+'foo') LIMIT 1" )
446
440
447
- assert_equal 1 , result
441
+ assert_equal 1 , result
442
+ end
448
443
ensure
449
444
@conn . execute ( "SET @@SESSION.sql_mode='#{ old_sql_mode } '" )
450
445
ActiveRecord ::Base . logger = previous_logger
451
- ActiveRecord . db_warnings_action = @original_db_warnings_action
452
446
end
453
447
454
448
private
0 commit comments