summaryrefslogtreecommitdiff
path: root/spec/initializers/00_connection_logger_spec.rb
blob: 8b288b463c405b8ed4e984651600a5838858f915 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter do # rubocop:disable RSpec/FilePath
  before do
    allow(PG).to receive(:connect)
  end

  let(:conn_params) { PG::Connection.conndefaults_hash }

  context 'when warn_on_new_connection is enabled' do
    before do
      described_class.warn_on_new_connection = true
    end

    it 'warns on new connection' do
      expect(ActiveSupport::Deprecation)
        .to receive(:warn).with(/Database connection should not be called during initializers/, anything)

      expect(PG).to receive(:connect).with(conn_params)

      described_class.new_client(conn_params)
    end
  end

  context 'when warn_on_new_connection is disabled' do
    before do
      described_class.warn_on_new_connection = false
    end

    it 'does not warn on new connection' do
      expect(ActiveSupport::Deprecation).not_to receive(:warn)
      expect(PG).to receive(:connect).with(conn_params)

      described_class.new_client(conn_params)
    end
  end
end