mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
53331b478d
commit
1c634c3fda
5 changed files with 120 additions and 182 deletions
|
@ -24,44 +24,32 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_errors_when_an_insert_query_is_called_while_preventing_writes
|
||||
assert_no_queries do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
ActiveRecord::Base.while_preventing_writes do
|
||||
@connection.transaction do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_errors_when_an_update_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
||||
assert_no_queries do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
ActiveRecord::Base.while_preventing_writes do
|
||||
@connection.transaction do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_errors_when_a_delete_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
||||
assert_no_queries do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
ActiveRecord::Base.while_preventing_writes do
|
||||
@connection.transaction do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_doesnt_error_when_a_select_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
@ -97,15 +85,11 @@ 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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_doesnt_error_when_a_select_query_starting_with_double_dash_comments_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
@ -118,40 +102,28 @@ 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
|
||||
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
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AdapterPreventWritesLegacyTest < ActiveRecord::TestCase
|
||||
def setup
|
||||
|
@ -178,50 +150,32 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_errors_when_an_insert_query_is_called_while_preventing_writes
|
||||
assert_no_queries do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
assert_deprecated do
|
||||
@connection_handler.while_preventing_writes do
|
||||
@connection.transaction do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_errors_when_an_update_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
||||
assert_no_queries do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
assert_deprecated do
|
||||
@connection_handler.while_preventing_writes do
|
||||
@connection.transaction do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_errors_when_a_delete_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
||||
assert_no_queries do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
assert_deprecated do
|
||||
@connection_handler.while_preventing_writes do
|
||||
@connection.transaction do
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_doesnt_error_when_a_select_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
@ -257,15 +211,11 @@ 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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_doesnt_error_when_a_select_query_starting_with_double_dash_comments_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
@ -278,32 +228,22 @@ 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
|
||||
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
|
||||
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
|
||||
|
@ -311,5 +251,3 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@conn.execute("REPLACE INTO `engines` SET `engines`.`car_id` = '249823948'")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@connection.execute("DELETE FROM ex where data = '138853948594'")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
assert_raises(ActiveRecord::ReadOnlyError) do
|
||||
@conn.execute("REPLACE INTO ex (data) VALUES ('249823948')")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,38 +7,38 @@ 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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
error = assert_raises ActiveRecord::ReadOnlyError do
|
||||
bird.destroy!
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
bird = Bird.create! name: "Bluejay"
|
||||
|
@ -67,26 +67,26 @@ 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
|
||||
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
|
||||
end
|
||||
|
||||
assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message
|
||||
end
|
||||
|
||||
conn2_error = assert_raises ActiveRecord::ReadOnlyError 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
|
||||
end
|
||||
end
|
||||
|
||||
test "current_preventing_writes" do
|
||||
ActiveRecord::Base.while_preventing_writes do
|
||||
|
@ -110,38 +110,38 @@ 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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
error = assert_raises ActiveRecord::ReadOnlyError do
|
||||
bird.destroy!
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
bird = Bird.create! name: "Bluejay"
|
||||
|
@ -170,54 +170,54 @@ 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
|
||||
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
|
||||
end
|
||||
|
||||
assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message
|
||||
end
|
||||
|
||||
conn2_error = assert_raises ActiveRecord::ReadOnlyError 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
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
assert_match %r/\AWrite query attempted while in readonly mode: INSERT /, conn1_error.message
|
||||
end
|
||||
|
||||
conn2_error = assert_raises ActiveRecord::ReadOnlyError 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
|
||||
end
|
||||
end
|
||||
|
||||
test "current_preventing_writes" do
|
||||
ActiveRecord::Base.connection_handler.while_preventing_writes do
|
||||
|
|
Loading…
Reference in a new issue