summaryrefslogtreecommitdiff
path: root/spec/support_specs/database/multiple_databases_spec.rb
blob: 6ad15fd6594cd4b20844674a989498bd378ee6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Database::MultipleDatabases' do
  describe '.with_reestablished_active_record_base' do
    context 'when doing establish_connection' do
      context 'on ActiveRecord::Base' do
        it 'raises exception' do
          expect { ActiveRecord::Base.establish_connection(:main) }.to raise_error /Cannot re-establish/
        end

        context 'when using with_reestablished_active_record_base' do
          it 'does not raise exception' do
            with_reestablished_active_record_base do
              expect { ActiveRecord::Base.establish_connection(:main) }.not_to raise_error
            end
          end
        end
      end

      context 'on Ci::CiDatabaseRecord' do
        before do
          skip_if_multiple_databases_not_setup
        end

        it 'raises exception' do
          expect { Ci::CiDatabaseRecord.establish_connection(:ci) }.to raise_error /Cannot re-establish/
        end

        context 'when using with_reestablished_active_record_base' do
          it 'does not raise exception' do
            with_reestablished_active_record_base do
              expect { Ci::CiDatabaseRecord.establish_connection(:main) }.not_to raise_error
            end
          end
        end
      end
    end

    context 'when trying to access connection' do
      context 'when reconnect is true' do
        it 'does not raise exception' do
          with_reestablished_active_record_base(reconnect: true) do
            expect { ActiveRecord::Base.connection.execute("SELECT 1") }.not_to raise_error # rubocop:disable Database/MultipleDatabases
          end
        end
      end

      context 'when reconnect is false' do
        it 'does raise exception' do
          with_reestablished_active_record_base(reconnect: false) do
            expect { ActiveRecord::Base.connection.execute("SELECT 1") }.to raise_error(ActiveRecord::ConnectionNotEstablished) # rubocop:disable Database/MultipleDatabases
          end
        end
      end
    end
  end
end