mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add test coverage for dump_filename
This commit is contained in:
parent
3688a2e01a
commit
50b588144f
1 changed files with 87 additions and 0 deletions
|
@ -1507,6 +1507,10 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
class DatabaseTasksCheckSchemaFileMethods < ActiveRecord::TestCase
|
||||
setup do
|
||||
@configurations = { "development" => { "database" => "my-db" } }
|
||||
end
|
||||
|
||||
def test_check_schema_file_defaults
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
assert_deprecated do
|
||||
|
@ -1524,5 +1528,88 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_check_dump_filename_defaults
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
with_stubbed_configurations do
|
||||
assert_equal "/tmp/schema.rb", ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_for("development", "primary").name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_check_dump_filename_with_schema_env
|
||||
schema = ENV["SCHEMA"]
|
||||
ENV["SCHEMA"] = "schema_path"
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
with_stubbed_configurations do
|
||||
assert_equal "schema_path", ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_for("development", "primary").name)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
ENV["SCHEMA"] = schema
|
||||
end
|
||||
|
||||
{ ruby: "schema.rb", sql: "structure.sql" }.each_pair do |fmt, filename|
|
||||
define_method("test_check_dump_filename_for_#{fmt}_format") do
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
with_stubbed_configurations do
|
||||
assert_equal "/tmp/#{filename}", ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_for("development", "primary").name, fmt)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_check_dump_filename_defaults_for_non_primary_databases
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
configurations = {
|
||||
"development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } },
|
||||
}
|
||||
with_stubbed_configurations(configurations) do
|
||||
assert_equal "/tmp/secondary_schema.rb", ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_for("development", "secondary").name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_check_dump_filename_with_schema_env_with_non_primary_databases
|
||||
schema = ENV["SCHEMA"]
|
||||
ENV["SCHEMA"] = "schema_path"
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
configurations = {
|
||||
"development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } },
|
||||
}
|
||||
with_stubbed_configurations(configurations) do
|
||||
assert_equal "schema_path", ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_for("development", "secondary").name)
|
||||
end
|
||||
end
|
||||
ensure
|
||||
ENV["SCHEMA"] = schema
|
||||
end
|
||||
|
||||
{ ruby: "schema.rb", sql: "structure.sql" }.each_pair do |fmt, filename|
|
||||
define_method("test_check_dump_filename_for_#{fmt}_format_with_non_primary_databases") do
|
||||
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "/tmp") do
|
||||
configurations = {
|
||||
"development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } },
|
||||
}
|
||||
with_stubbed_configurations(configurations) do
|
||||
assert_equal "/tmp/secondary_#{filename}", ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_for("development", "secondary").name, fmt)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def config_for(env_name, name)
|
||||
ActiveRecord::Base.configurations.configs_for(env_name: env_name, name: name)
|
||||
end
|
||||
|
||||
def with_stubbed_configurations(configurations = @configurations)
|
||||
old_configurations = ActiveRecord::Base.configurations
|
||||
ActiveRecord::Base.configurations = configurations
|
||||
|
||||
yield
|
||||
ensure
|
||||
ActiveRecord::Base.configurations = old_configurations
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue