Module: TestProf::BeforeAll::Adapters::ActiveRecord

Defined in:
lib/test_prof/before_all/adapters/active_record.rb

Overview

ActiveRecord adapter for ‘before_all`

Constant Summary collapse

POOL_ARGS =
((::ActiveRecord::VERSION::MAJOR > 6) ? [:writing] : []).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_fixtures(test_object) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 66

def setup_fixtures(test_object)
  test_object.instance_eval do
    @@already_loaded_fixtures ||= {}
    @fixture_cache ||= {}
    config = ::ActiveRecord::Base

    if @@already_loaded_fixtures[self.class]
      @loaded_fixtures = @@already_loaded_fixtures[self.class]
    else
      @loaded_fixtures = load_fixtures(config)
      @@already_loaded_fixtures[self.class] = @loaded_fixtures
    end
  end
end

Instance Method Details

#all_connectionsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 24

def all_connections
  @all_connections ||= if ::ActiveRecord::Base.respond_to? :connects_to
    ::ActiveRecord::Base.connection_handler.connection_pool_list(*POOL_ARGS).filter_map { |pool|
      begin
        pool.connection
      rescue *pool_connection_errors => error
        log_pool_connection_error(pool, error)
        nil
      end
    }
  else
    Array.wrap(::ActiveRecord::Base.connection)
  end
end

#begin_transactionObject



12
13
14
15
16
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 12

def begin_transaction
  ::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).each do |pool|
    pool.pin_connection!(true)
  end
end

#log_pool_connection_error(pool, error) ⇒ Object



43
44
45
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 43

def log_pool_connection_error(pool, error)
  warn "Could not connect to pool #{pool.connection_class.name}. #{error.class}: #{error.message}"
end

#pool_connection_errorsObject



39
40
41
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 39

def pool_connection_errors
  @pool_connection_errors ||= []
end

#rollback_transactionObject



18
19
20
21
22
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 18

def rollback_transaction
  ::ActiveRecord::Base.connection_handler.connection_pool_list(:writing).each do |pool|
    pool.unpin_connection!
  end
end