From 1c634c3fda708fd14b2197d0084ce92c47f55414 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Mon, 9 Nov 2020 11:11:26 -0500 Subject: [PATCH] Clean up preventing writes tests These tests included a lot of unnecessary code. We don't need a transaction, in many cases the assert_deprecated wasn't actually asserting anything, and the assertion for raising an error should be inside the `while_preventing_writes` call. --- .../test/cases/adapter_prevent_writes_test.rb | 118 +++++------------- .../mysql2_adapter_prevent_writes_test.rb | 32 ++--- .../postgresql_adapter_prevent_writes_test.rb | 24 ++-- .../sqlite3_adapter_prevent_writes_test.rb | 32 ++--- .../test/cases/base_prevent_writes_test.rb | 96 +++++++------- 5 files changed, 120 insertions(+), 182 deletions(-) diff --git a/activerecord/test/cases/adapter_prevent_writes_test.rb b/activerecord/test/cases/adapter_prevent_writes_test.rb index 2f4fcac066..2def5ff0f4 100644 --- a/activerecord/test/cases/adapter_prevent_writes_test.rb +++ b/activerecord/test/cases/adapter_prevent_writes_test.rb @@ -24,13 +24,9 @@ module ActiveRecord end def test_errors_when_an_insert_query_is_called_while_preventing_writes - assert_no_queries do + ActiveRecord::Base.while_preventing_writes do assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end @@ -38,13 +34,9 @@ module ActiveRecord def test_errors_when_an_update_query_is_called_while_preventing_writes @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')") - assert_no_queries do + ActiveRecord::Base.while_preventing_writes do assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - @connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'") - end - end + @connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'") end end end @@ -52,13 +44,9 @@ module ActiveRecord def test_errors_when_a_delete_query_is_called_while_preventing_writes @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')") - assert_no_queries do + ActiveRecord::Base.while_preventing_writes do assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - @connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'") - end - end + @connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'") end end end @@ -97,12 +85,8 @@ module ActiveRecord def test_errors_when_an_insert_query_prefixed_by_a_slash_star_comment_is_called_while_preventing_writes ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("/* some comment */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("/* some comment */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end @@ -118,36 +102,24 @@ module ActiveRecord def test_errors_when_an_insert_query_prefixed_by_a_double_dash_comment_is_called_while_preventing_writes ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("-- some comment\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("-- some comment\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end def test_errors_when_an_insert_query_prefixed_by_a_slash_star_comment_containing_read_command_is_called_while_preventing_writes ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("/* SELECT */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("/* SELECT */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end def test_errors_when_an_insert_query_prefixed_by_a_double_dash_comment_containing_read_command_is_called_while_preventing_writes ActiveRecord::Base.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("-- SELECT\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("-- SELECT\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end @@ -178,15 +150,9 @@ module ActiveRecord end def test_errors_when_an_insert_query_is_called_while_preventing_writes - assert_no_queries do + @connection_handler.while_preventing_writes do assert_raises(ActiveRecord::ReadOnlyError) do - assert_deprecated do - @connection_handler.while_preventing_writes do - @connection.transaction do - @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end - end + @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end @@ -194,15 +160,9 @@ module ActiveRecord def test_errors_when_an_update_query_is_called_while_preventing_writes @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')") - assert_no_queries do + @connection_handler.while_preventing_writes do assert_raises(ActiveRecord::ReadOnlyError) do - assert_deprecated do - @connection_handler.while_preventing_writes do - @connection.transaction do - @connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'") - end - end - end + @connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'") end end end @@ -210,15 +170,9 @@ module ActiveRecord def test_errors_when_a_delete_query_is_called_while_preventing_writes @connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')") - assert_no_queries do + @connection_handler.while_preventing_writes do assert_raises(ActiveRecord::ReadOnlyError) do - assert_deprecated do - @connection_handler.while_preventing_writes do - @connection.transaction do - @connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'") - end - end - end + @connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'") end end end @@ -257,12 +211,8 @@ module ActiveRecord def test_errors_when_an_insert_query_prefixed_by_a_slash_star_comment_is_called_while_preventing_writes @connection_handler.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("/* some comment */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("/* some comment */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end @@ -278,36 +228,24 @@ module ActiveRecord def test_errors_when_an_insert_query_prefixed_by_a_double_dash_comment_is_called_while_preventing_writes @connection_handler.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("-- some comment\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("-- some comment\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end def test_errors_when_an_insert_query_prefixed_by_a_slash_star_comment_containing_read_command_is_called_while_preventing_writes @connection_handler.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("/* SELECT */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("/* SELECT */ INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end def test_errors_when_an_insert_query_prefixed_by_a_double_dash_comment_containing_read_command_is_called_while_preventing_writes @connection_handler.while_preventing_writes do - @connection.transaction do - assert_no_queries do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection.insert("-- SELECT\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) - end - end + assert_raises(ActiveRecord::ReadOnlyError) do + @connection.insert("-- SELECT\nINSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false) end end end diff --git a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_prevent_writes_test.rb b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_prevent_writes_test.rb index 5a95a8e1d0..3ea7fa6e66 100644 --- a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_prevent_writes_test.rb +++ b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_prevent_writes_test.rb @@ -11,8 +11,8 @@ class Mysql2AdapterPreventWritesTest < ActiveRecord::Mysql2TestCase end def test_errors_when_an_insert_query_is_called_while_preventing_writes - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.insert("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") end end @@ -21,8 +21,8 @@ class Mysql2AdapterPreventWritesTest < ActiveRecord::Mysql2TestCase def test_errors_when_an_update_query_is_called_while_preventing_writes @conn.insert("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.update("UPDATE `engines` SET `engines`.`car_id` = '9989' WHERE `engines`.`car_id` = '138853948594'") end end @@ -31,8 +31,8 @@ class Mysql2AdapterPreventWritesTest < ActiveRecord::Mysql2TestCase def test_errors_when_a_delete_query_is_called_while_preventing_writes @conn.execute("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("DELETE FROM `engines` where `engines`.`car_id` = '138853948594'") end end @@ -41,8 +41,8 @@ class Mysql2AdapterPreventWritesTest < ActiveRecord::Mysql2TestCase def test_errors_when_a_replace_query_is_called_while_preventing_writes @conn.execute("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("REPLACE INTO `engines` SET `engines`.`car_id` = '249823948'") end end @@ -117,8 +117,8 @@ class Mysql2AdapterPreventWritesLegacyTest < ActiveRecord::Mysql2TestCase end def test_errors_when_an_insert_query_is_called_while_preventing_writes - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.insert("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") end end @@ -127,8 +127,8 @@ class Mysql2AdapterPreventWritesLegacyTest < ActiveRecord::Mysql2TestCase def test_errors_when_an_update_query_is_called_while_preventing_writes @conn.insert("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.update("UPDATE `engines` SET `engines`.`car_id` = '9989' WHERE `engines`.`car_id` = '138853948594'") end end @@ -137,8 +137,8 @@ class Mysql2AdapterPreventWritesLegacyTest < ActiveRecord::Mysql2TestCase def test_errors_when_a_delete_query_is_called_while_preventing_writes @conn.execute("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("DELETE FROM `engines` where `engines`.`car_id` = '138853948594'") end end @@ -147,8 +147,8 @@ class Mysql2AdapterPreventWritesLegacyTest < ActiveRecord::Mysql2TestCase def test_errors_when_a_replace_query_is_called_while_preventing_writes @conn.execute("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("REPLACE INTO `engines` SET `engines`.`car_id` = '249823948'") end end diff --git a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_prevent_writes_test.rb b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_prevent_writes_test.rb index 4613203f8e..d34f83be24 100644 --- a/activerecord/test/cases/adapters/postgresql/postgresql_adapter_prevent_writes_test.rb +++ b/activerecord/test/cases/adapters/postgresql/postgresql_adapter_prevent_writes_test.rb @@ -17,8 +17,8 @@ module ActiveRecord def test_errors_when_an_insert_query_is_called_while_preventing_writes with_example_table do - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @connection.execute("INSERT INTO ex (data) VALUES ('138853948594')") end end @@ -29,8 +29,8 @@ module ActiveRecord with_example_table do @connection.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @connection.execute("UPDATE ex SET data = '9989' WHERE data = '138853948594'") end end @@ -41,8 +41,8 @@ module ActiveRecord with_example_table do @connection.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @connection.execute("DELETE FROM ex where data = '138853948594'") end end @@ -119,8 +119,8 @@ module ActiveRecord def test_errors_when_an_insert_query_is_called_while_preventing_writes with_example_table do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @connection.execute("INSERT INTO ex (data) VALUES ('138853948594')") end end @@ -131,8 +131,8 @@ module ActiveRecord with_example_table do @connection.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @connection.execute("UPDATE ex SET data = '9989' WHERE data = '138853948594'") end end @@ -143,8 +143,8 @@ module ActiveRecord with_example_table do @connection.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @connection.execute("DELETE FROM ex where data = '138853948594'") end end diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_prevent_writes_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_prevent_writes_test.rb index ed4e9ad03b..53727d9efc 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_prevent_writes_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_prevent_writes_test.rb @@ -18,8 +18,8 @@ module ActiveRecord def test_errors_when_an_insert_query_is_called_while_preventing_writes with_example_table "id int, data string" do - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") end end @@ -30,8 +30,8 @@ module ActiveRecord with_example_table "id int, data string" do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("UPDATE ex SET data = '9989' WHERE data = '138853948594'") end end @@ -42,8 +42,8 @@ module ActiveRecord with_example_table "id int, data string" do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("DELETE FROM ex where data = '138853948594'") end end @@ -54,8 +54,8 @@ module ActiveRecord with_example_table "id int, data string" do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("REPLACE INTO ex (data) VALUES ('249823948')") end end @@ -113,8 +113,8 @@ module ActiveRecord def test_errors_when_an_insert_query_is_called_while_preventing_writes with_example_table "id int, data string" do - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") end end @@ -125,8 +125,8 @@ module ActiveRecord with_example_table "id int, data string" do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("UPDATE ex SET data = '9989' WHERE data = '138853948594'") end end @@ -137,8 +137,8 @@ module ActiveRecord with_example_table "id int, data string" do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("DELETE FROM ex where data = '138853948594'") end end @@ -149,8 +149,8 @@ module ActiveRecord with_example_table "id int, data string" do @conn.execute("INSERT INTO ex (data) VALUES ('138853948594')") - assert_raises(ActiveRecord::ReadOnlyError) do - @connection_handler.while_preventing_writes do + @connection_handler.while_preventing_writes do + assert_raises(ActiveRecord::ReadOnlyError) do @conn.execute("REPLACE INTO ex (data) VALUES ('249823948')") end end diff --git a/activerecord/test/cases/base_prevent_writes_test.rb b/activerecord/test/cases/base_prevent_writes_test.rb index bc1d791f61..f02fd44747 100644 --- a/activerecord/test/cases/base_prevent_writes_test.rb +++ b/activerecord/test/cases/base_prevent_writes_test.rb @@ -7,37 +7,37 @@ require "byebug" class BasePreventWritesTest < ActiveRecord::TestCase if !in_memory_db? test "creating a record raises if preventing writes" do - error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + error = assert_raises ActiveRecord::ReadOnlyError do Bird.create! name: "Bluejay" end - end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, error.message + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, error.message + end end test "updating a record raises if preventing writes" do bird = Bird.create! name: "Bluejay" - error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + error = assert_raises ActiveRecord::ReadOnlyError do bird.update! name: "Robin" end - end - assert_match %r/\AWrite query attempted while in readonly mode: UPDATE /, error.message + assert_match %r/\AWrite query attempted while in readonly mode: UPDATE /, error.message + end end test "deleting a record raises if preventing writes" do bird = Bird.create! name: "Bluejay" - error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + error = assert_raises ActiveRecord::ReadOnlyError do bird.destroy! end - end - assert_match %r/\AWrite query attempted while in readonly mode: DELETE /, error.message + assert_match %r/\AWrite query attempted while in readonly mode: DELETE /, error.message + end end test "selecting a record does not raise if preventing writes" do @@ -67,25 +67,25 @@ class BasePreventWritesTest < ActiveRecord::TestCase end test "preventing writes applies to all connections in block" do - conn1_error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + conn1_error = assert_raises ActiveRecord::ReadOnlyError do assert_equal ActiveRecord::Base.connection, Bird.connection assert_not_equal ARUnit2Model.connection, Bird.connection Bird.create!(name: "Bluejay") end + + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message - - conn2_error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.while_preventing_writes do + ActiveRecord::Base.while_preventing_writes do + conn2_error = assert_raises ActiveRecord::ReadOnlyError do assert_not_equal ActiveRecord::Base.connection, Professor.connection assert_equal ARUnit2Model.connection, Professor.connection Professor.create!(name: "Professor Bluejay") end - end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn2_error.message + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn2_error.message + end end test "current_preventing_writes" do @@ -110,37 +110,37 @@ class BasePreventWritesTest < ActiveRecord::TestCase if !in_memory_db? test "creating a record raises if preventing writes" do - error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connection_handler.while_preventing_writes do + ActiveRecord::Base.connection_handler.while_preventing_writes do + error = assert_raises ActiveRecord::ReadOnlyError do Bird.create! name: "Bluejay" end - end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, error.message + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, error.message + end end test "updating a record raises if preventing writes" do bird = Bird.create! name: "Bluejay" - error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connection_handler.while_preventing_writes do + ActiveRecord::Base.connection_handler.while_preventing_writes do + error = assert_raises ActiveRecord::ReadOnlyError do bird.update! name: "Robin" end - end - assert_match %r/\AWrite query attempted while in readonly mode: UPDATE /, error.message + assert_match %r/\AWrite query attempted while in readonly mode: UPDATE /, error.message + end end test "deleting a record raises if preventing writes" do bird = Bird.create! name: "Bluejay" - error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connection_handler.while_preventing_writes do + ActiveRecord::Base.connection_handler.while_preventing_writes do + error = assert_raises ActiveRecord::ReadOnlyError do bird.destroy! end - end - assert_match %r/\AWrite query attempted while in readonly mode: DELETE /, error.message + assert_match %r/\AWrite query attempted while in readonly mode: DELETE /, error.message + end end test "selecting a record does not raise if preventing writes" do @@ -170,53 +170,53 @@ class BasePreventWritesTest < ActiveRecord::TestCase end test "preventing writes applies to all connections on a handler" do - conn1_error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connection_handler.while_preventing_writes do + ActiveRecord::Base.connection_handler.while_preventing_writes do + conn1_error = assert_raises ActiveRecord::ReadOnlyError do assert_equal ActiveRecord::Base.connection, Bird.connection assert_not_equal ARUnit2Model.connection, Bird.connection Bird.create!(name: "Bluejay") end + + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message - - conn2_error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connection_handler.while_preventing_writes do + ActiveRecord::Base.connection_handler.while_preventing_writes do + conn2_error = assert_raises ActiveRecord::ReadOnlyError do assert_not_equal ActiveRecord::Base.connection, Professor.connection assert_equal ARUnit2Model.connection, Professor.connection Professor.create!(name: "Professor Bluejay") end - end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn2_error.message + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn2_error.message + end end test "preventing writes with multiple handlers" do ActiveRecord::Base.connects_to(database: { writing: :arunit, reading: :arunit }) - conn1_error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connected_to(role: :writing) do + ActiveRecord::Base.connected_to(role: :writing) do + conn1_error = assert_raises ActiveRecord::ReadOnlyError do assert_equal :writing, ActiveRecord::Base.current_role ActiveRecord::Base.connection_handler.while_preventing_writes do Bird.create!(name: "Bluejay") end end + + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message - - conn2_error = assert_raises ActiveRecord::ReadOnlyError do - ActiveRecord::Base.connected_to(role: :reading) do + ActiveRecord::Base.connected_to(role: :reading) do + conn2_error = assert_raises ActiveRecord::ReadOnlyError do assert_equal :reading, ActiveRecord::Base.current_role ActiveRecord::Base.connection_handler.while_preventing_writes do Bird.create!(name: "Bluejay") end end - end - assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn2_error.message + assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn2_error.message + end end test "current_preventing_writes" do