mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #33409 from utilum/correct_epxectations_to_meet_minitest_strict_mocking
Use MethodCallAssertions instead of Mocha part 2
This commit is contained in:
commit
9ca579ad6e
4 changed files with 112 additions and 49 deletions
|
@ -157,14 +157,19 @@ class Mysql2ActiveSchemaTest < ActiveRecord::Mysql2TestCase
|
|||
end
|
||||
|
||||
def test_indexes_in_create
|
||||
ActiveRecord::Base.connection.stubs(:data_source_exists?).with(:temp).returns(false)
|
||||
assert_called_with(
|
||||
ActiveRecord::Base.connection,
|
||||
:data_source_exists?,
|
||||
[:temp],
|
||||
returns: false
|
||||
) do
|
||||
expected = "CREATE TEMPORARY TABLE `temp` ( INDEX `index_temp_on_zip` (`zip`)) AS SELECT id, name, zip FROM a_really_complicated_query"
|
||||
actual = ActiveRecord::Base.connection.create_table(:temp, temporary: true, as: "SELECT id, name, zip FROM a_really_complicated_query") do |t|
|
||||
t.index :zip
|
||||
end
|
||||
|
||||
expected = "CREATE TEMPORARY TABLE `temp` ( INDEX `index_temp_on_zip` (`zip`)) AS SELECT id, name, zip FROM a_really_complicated_query"
|
||||
actual = ActiveRecord::Base.connection.create_table(:temp, temporary: true, as: "SELECT id, name, zip FROM a_really_complicated_query") do |t|
|
||||
t.index :zip
|
||||
assert_equal expected, actual
|
||||
end
|
||||
|
||||
assert_equal expected, actual
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -143,9 +143,6 @@ module ActiveRecord
|
|||
def setup
|
||||
@configurations = { "development" => { "database" => "my-db" } }
|
||||
|
||||
# To refrain from connecting to a newly created empty DB in sqlite3_mem tests
|
||||
ActiveRecord::Base.connection_handler.stubs(:establish_connection)
|
||||
|
||||
$stdout, @original_stdout = StringIO.new, $stdout
|
||||
$stderr, @original_stderr = StringIO.new, $stderr
|
||||
end
|
||||
|
@ -157,7 +154,7 @@ module ActiveRecord
|
|||
def test_ignores_configurations_without_databases
|
||||
@configurations["development"].merge!("database" => nil)
|
||||
|
||||
with_stubbed_configurations do
|
||||
with_stubbed_configurations_establish_connection do
|
||||
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create_all
|
||||
end
|
||||
|
@ -167,7 +164,7 @@ module ActiveRecord
|
|||
def test_ignores_remote_databases
|
||||
@configurations["development"].merge!("host" => "my.server.tld")
|
||||
|
||||
with_stubbed_configurations do
|
||||
with_stubbed_configurations_establish_connection do
|
||||
assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create_all
|
||||
end
|
||||
|
@ -177,7 +174,7 @@ module ActiveRecord
|
|||
def test_warning_for_remote_databases
|
||||
@configurations["development"].merge!("host" => "my.server.tld")
|
||||
|
||||
with_stubbed_configurations do
|
||||
with_stubbed_configurations_establish_connection do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create_all
|
||||
|
||||
assert_match "This task only modifies local databases. my-db is on a remote host.",
|
||||
|
@ -188,7 +185,7 @@ module ActiveRecord
|
|||
def test_creates_configurations_with_local_ip
|
||||
@configurations["development"].merge!("host" => "127.0.0.1")
|
||||
|
||||
with_stubbed_configurations do
|
||||
with_stubbed_configurations_establish_connection do
|
||||
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create_all
|
||||
end
|
||||
|
@ -198,7 +195,7 @@ module ActiveRecord
|
|||
def test_creates_configurations_with_local_host
|
||||
@configurations["development"].merge!("host" => "localhost")
|
||||
|
||||
with_stubbed_configurations do
|
||||
with_stubbed_configurations_establish_connection do
|
||||
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create_all
|
||||
end
|
||||
|
@ -208,7 +205,7 @@ module ActiveRecord
|
|||
def test_creates_configurations_with_blank_hosts
|
||||
@configurations["development"].merge!("host" => nil)
|
||||
|
||||
with_stubbed_configurations do
|
||||
with_stubbed_configurations_establish_connection do
|
||||
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create_all
|
||||
end
|
||||
|
@ -217,9 +214,16 @@ module ActiveRecord
|
|||
|
||||
private
|
||||
|
||||
def with_stubbed_configurations
|
||||
def with_stubbed_configurations_establish_connection
|
||||
ActiveRecord::Base.stub(:configurations, @configurations) do
|
||||
yield
|
||||
# To refrain from connecting to a newly created empty DB in
|
||||
# sqlite3_mem tests
|
||||
ActiveRecord::Base.connection_handler.stub(
|
||||
:establish_connection,
|
||||
nil
|
||||
) do
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,11 +21,17 @@ if current_adapter?(:Mysql2Adapter)
|
|||
end
|
||||
|
||||
def test_establishes_connection_without_database
|
||||
ActiveRecord::Base.stubs(:establish_connection)
|
||||
ActiveRecord::Base.stub(:connection, @connection) do
|
||||
ActiveRecord::Base.expects(:establish_connection).
|
||||
with("adapter" => "mysql2", "database" => nil)
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
assert_called_with(
|
||||
ActiveRecord::Base,
|
||||
:establish_connection,
|
||||
[
|
||||
[ "adapter" => "mysql2", "database" => nil ],
|
||||
[ "adapter" => "mysql2", "database" => "my-app-db" ],
|
||||
]
|
||||
) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -58,12 +64,17 @@ if current_adapter?(:Mysql2Adapter)
|
|||
end
|
||||
|
||||
def test_establishes_connection_to_database
|
||||
ActiveRecord::Base.stubs(:establish_connection)
|
||||
|
||||
ActiveRecord::Base.stub(:connection, @connection) do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(@configuration)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
assert_called_with(
|
||||
ActiveRecord::Base,
|
||||
:establish_connection,
|
||||
[
|
||||
["adapter" => "mysql2", "database" => nil],
|
||||
[@configuration]
|
||||
]
|
||||
) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -21,14 +21,24 @@ if current_adapter?(:PostgreSQLAdapter)
|
|||
end
|
||||
|
||||
def test_establishes_connection_to_postgresql_database
|
||||
ActiveRecord::Base.stubs(:establish_connection)
|
||||
ActiveRecord::Base.stub(:connection, @connection) do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
"adapter" => "postgresql",
|
||||
"database" => "postgres",
|
||||
"schema_search_path" => "public"
|
||||
)
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
assert_called_with(
|
||||
ActiveRecord::Base,
|
||||
:establish_connection,
|
||||
[
|
||||
[
|
||||
"adapter" => "postgresql",
|
||||
"database" => "postgres",
|
||||
"schema_search_path" => "public"
|
||||
],
|
||||
[
|
||||
"adapter" => "postgresql",
|
||||
"database" => "my-app-db"
|
||||
]
|
||||
]
|
||||
) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -78,11 +88,23 @@ if current_adapter?(:PostgreSQLAdapter)
|
|||
end
|
||||
|
||||
def test_establishes_connection_to_new_database
|
||||
ActiveRecord::Base.stubs(:establish_connection)
|
||||
ActiveRecord::Base.stub(:connection, @connection) do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(@configuration)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
assert_called_with(
|
||||
ActiveRecord::Base,
|
||||
:establish_connection,
|
||||
[
|
||||
[
|
||||
"adapter" => "postgresql",
|
||||
"database" => "postgres",
|
||||
"schema_search_path" => "public"
|
||||
],
|
||||
[
|
||||
@configuration
|
||||
]
|
||||
]
|
||||
) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -207,15 +229,24 @@ if current_adapter?(:PostgreSQLAdapter)
|
|||
end
|
||||
|
||||
def test_establishes_connection_to_postgresql_database
|
||||
ActiveRecord::Base.stubs(:establish_connection)
|
||||
with_stubbed_connection do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(
|
||||
"adapter" => "postgresql",
|
||||
"database" => "postgres",
|
||||
"schema_search_path" => "public"
|
||||
)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
|
||||
assert_called_with(
|
||||
ActiveRecord::Base,
|
||||
:establish_connection,
|
||||
[
|
||||
[
|
||||
"adapter" => "postgresql",
|
||||
"database" => "postgres",
|
||||
"schema_search_path" => "public"
|
||||
],
|
||||
[
|
||||
"adapter" => "postgresql",
|
||||
"database" => "my-app-db"
|
||||
]
|
||||
]
|
||||
) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -244,11 +275,23 @@ if current_adapter?(:PostgreSQLAdapter)
|
|||
end
|
||||
|
||||
def test_establishes_connection
|
||||
ActiveRecord::Base.stubs(:establish_connection)
|
||||
with_stubbed_connection do
|
||||
ActiveRecord::Base.expects(:establish_connection).with(@configuration)
|
||||
|
||||
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
|
||||
assert_called_with(
|
||||
ActiveRecord::Base,
|
||||
:establish_connection,
|
||||
[
|
||||
[
|
||||
"adapter" => "postgresql",
|
||||
"database" => "postgres",
|
||||
"schema_search_path" => "public"
|
||||
],
|
||||
[
|
||||
@configuration
|
||||
]
|
||||
]
|
||||
) do
|
||||
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue