Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING isn't necessary anymore,
after dropping support for Rails < 6.1.
  • Loading branch information
bquorning committed Oct 12, 2023
1 parent 58e69af commit 00d34a1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 57 deletions.
84 changes: 41 additions & 43 deletions test/helper.rb
Expand Up @@ -14,8 +14,6 @@

ActiveRecord::Base.legacy_connection_handling = (ENV['LEGACY_CONNECTION_HANDLING'] == 'true')

RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING = !ActiveRecord::Base.legacy_connection_handling

ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/test.log')

Thread.abort_on_exception = true
Expand Down Expand Up @@ -48,7 +46,46 @@ module ARHPTestSetup
def arhp_create_models
return if ARHPTestSetup.const_defined?('Pool1DbA')

if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
if ActiveRecord::Base.legacy_connection_handling
eval <<-RUBY
# The placement of the Pool1DbC class is important so that its
# connection will not be the most recent connection established
# for test_pool_1.
class Pool1DbC < ActiveRecord::Base
establish_connection(:test_pool_1_db_c)
end
class Pool1DbA < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_1_db_a)
end
class Pool1DbAReplica < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_1_db_a_replica)
end
class Pool1DbB < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_1_db_b)
end
class Pool2DbD < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_2_db_d)
end
class Pool2DbE < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_2_db_e)
end
class Pool3DbE < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_3_db_e)
end
RUBY
else
eval <<-RUBY
class AbstractPool1DbC < ActiveRecord::Base
self.abstract_class = true
Expand Down Expand Up @@ -121,45 +158,6 @@ class ShardedModel < AbstractShardedModel
self.table_name = "tests"
end
RUBY
else
eval <<-RUBY
# The placement of the Pool1DbC class is important so that its
# connection will not be the most recent connection established
# for test_pool_1.
class Pool1DbC < ActiveRecord::Base
establish_connection(:test_pool_1_db_c)
end
class Pool1DbA < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_1_db_a)
end
class Pool1DbAReplica < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_1_db_a_replica)
end
class Pool1DbB < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_1_db_b)
end
class Pool2DbD < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_2_db_d)
end
class Pool2DbE < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_2_db_e)
end
class Pool3DbE < ActiveRecord::Base
self.table_name = "tests"
establish_connection(:test_pool_3_db_e)
end
RUBY
end
end

Expand All @@ -179,7 +177,7 @@ def without_module_patch(mod, method_name)
end

def simulate_rails_app_active_record_railties
unless RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
if ActiveRecord::Base.legacy_connection_handling
# Necessary for testing ActiveRecord 6.0 which uses the connection
# handlers when clearing query caches across all handlers when
# an operation that dirties the cache is involved (e.g. create/insert,
Expand Down
6 changes: 3 additions & 3 deletions test/test_arhp.rb
Expand Up @@ -5,10 +5,10 @@
class ActiveRecordHostPoolTest < Minitest::Test
include ARHPTestSetup
def setup
if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
Phenix.rise! config_path: 'test/three_tier_database.yml'
else
if ActiveRecord::Base.legacy_connection_handling
Phenix.rise!
else
Phenix.rise! config_path: 'test/three_tier_database.yml'
end
arhp_create_models
end
Expand Down
6 changes: 3 additions & 3 deletions test/test_arhp_caching.rb
Expand Up @@ -5,10 +5,10 @@
class ActiveRecordHostCachingTest < Minitest::Test
include ARHPTestSetup
def setup
if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
Phenix.rise! config_path: 'test/three_tier_database.yml'
else
if ActiveRecord::Base.legacy_connection_handling
Phenix.rise!
else
Phenix.rise! config_path: 'test/three_tier_database.yml'
end
arhp_create_models
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_arhp_legacy_connection_handling_only_tests.rb
Expand Up @@ -2,7 +2,7 @@

require_relative 'helper'

unless RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
if ActiveRecord::Base.legacy_connection_handling
class ActiveRecordHostPoolLegacyConnectiongHandlingTest < Minitest::Test
include ARHPTestSetup
def setup
Expand Down
2 changes: 1 addition & 1 deletion test/test_arhp_with_non_legacy_connection_handling.rb
Expand Up @@ -3,7 +3,7 @@
require_relative 'helper'
require 'stringio'

if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
unless ActiveRecord::Base.legacy_connection_handling
class ActiveRecordHostPoolTestWithNonlegacyConnectionHandling < Minitest::Test
include ARHPTestSetup
def setup
Expand Down
6 changes: 3 additions & 3 deletions test/test_arhp_wrong_db.rb
Expand Up @@ -5,10 +5,10 @@
class ActiveRecordHostPoolWrongDBTest < Minitest::Test
include ARHPTestSetup
def setup
if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
Phenix.load_database_config 'test/three_tier_database.yml'
else
if ActiveRecord::Base.legacy_connection_handling
Phenix.load_database_config
else
Phenix.load_database_config 'test/three_tier_database.yml'
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/test_thread_safety.rb
Expand Up @@ -6,10 +6,10 @@ class ThreadSafetyTest < Minitest::Test
include ARHPTestSetup

def setup
if RAILS_6_1_WITH_NON_LEGACY_CONNECTION_HANDLING
Phenix.rise! config_path: 'test/three_tier_database.yml'
else
if ActiveRecord::Base.legacy_connection_handling
Phenix.rise!
else
Phenix.rise! config_path: 'test/three_tier_database.yml'
end

arhp_create_models
Expand Down

0 comments on commit 00d34a1

Please sign in to comment.