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

Class Method Details

.all_connectionsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 11

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



34
35
36
37
38
39
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 34

def begin_transaction
  @all_connections = nil
  all_connections.each do |connection|
    connection.begin_transaction(joinable: false)
  end
end

.log_pool_connection_error(pool, error) ⇒ Object



30
31
32
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 30

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



26
27
28
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 26

def pool_connection_errors
  @pool_connection_errors ||= []
end

.rollback_transactionObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 41

def rollback_transaction
  all_connections.each do |connection|
    if connection.open_transactions.zero?
      warn "!!! before_all transaction has been already rollbacked and " \
            "could work incorrectly"
      next
    end
    connection.rollback_transaction
  end
end

.setup_fixtures(test_object) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/test_prof/before_all/adapters/active_record.rb', line 52

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