Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Test Execution Time #80

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def setup
Rails.logger = Logger.new($stdout)
ActionController::Base.view_paths = [File.expand_path("../../../app/views/", __dir__)]
active_record_setup
@utils.reset_database_yml(TestingState.db_config)
@utils.cleanup(TestingState.db_config)
@utils.prepare_phantom_migrations(TestingState.db_config)
end
Expand Down
1 change: 1 addition & 0 deletions test/rake_task_secondary_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
end

before do
utils.reset_database_yml(TestingState.db_config["secondary"])
ActiveRecord::Base.configurations = { "test" => TestingState.db_config["secondary"] }
ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config["secondary"] }
ActiveRecord::Base.establish_connection(**TestingState.db_config["secondary"])
Expand Down
1 change: 1 addition & 0 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
let(:utils) { TestUtils.new }

before do
utils.reset_database_yml(TestingState.db_config["primary"])
ActiveRecord::Base.configurations = { "test" => TestingState.db_config["primary"] }
ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config["primary"] }
ActiveRecord::Base.establish_connection(**TestingState.db_config["primary"])
Expand Down
1 change: 1 addition & 0 deletions test/rake_tasks_all_databases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
end

before do
utils.reset_database_yml(TestingState.db_config)
ActiveRecord::Base.configurations = { "test" => TestingState.db_config }
ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config }
utils.cleanup(TestingState.db_config)
Expand Down
24 changes: 12 additions & 12 deletions test/support/test_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ def applied_migrations(db_config = nil)
end
end

def clear_schema(db_config = nil)
if db_config
db_config.each_value do |config|
ActiveRecord::Base.establish_connection(**config)
clear_schema_call
end
else
clear_schema_call
end
end

def simulate_input(input)
$stdin = StringIO.new("#{([input] * 999).join("\n")}\n")
yield
Expand Down Expand Up @@ -107,6 +96,16 @@ def down
end
end

def reset_database_yml(db_config)
database_yml_path = Rails.root.join("config", "database.yml")
File.delete(database_yml_path) if File.exist?(database_yml_path)
File.open(database_yml_path, "w") do |file|
file.write({
"test" => db_config
}.to_yaml)
end
end

def prepare_phantom_migrations(db_config = nil)
run_migrations
if db_config
Expand Down Expand Up @@ -150,9 +149,10 @@ def branch_for(version)
def cleanup_call(prefix_name = nil)
delete_migrations_files(prefix_name)
create_schema_migration_table
run_sql("delete from schema_migrations")
clear_schema_call
remove_app_dir(MIGRATED_PATHS.fetch(prefix_name&.to_sym, migrated_paths.first))
define_migrations(prefix_name)
Rake::Task.clear
Rails.application.load_tasks
end

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "actual_db_schema"
require "minitest/autorun"
require "debug"
require "rake"
require "support/test_utils"

Rails.env = "test"
Expand Down
Loading