summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/performance/active_record_subtransactions_spec.rb
blob: 0da2e30062a2fb88abdc6cf102e8cf939bcd2dd4 (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
60
61
62
# frozen_string_literal: true

require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/performance/active_record_subtransactions'

RSpec.describe RuboCop::Cop::Performance::ActiveRecordSubtransactions do
  subject(:cop) { described_class.new }

  let(:message) { described_class::MSG }

  context 'when calling #transaction with only requires_new: true' do
    it 'registers an offense' do
      expect_offense(<<~RUBY)
        ApplicationRecord.transaction(requires_new: true) do
                                      ^^^^^^^^^^^^^^^^^^ #{message}
          Project.create!(name: 'MyProject')
        end
      RUBY
    end
  end

  context 'when passing multiple arguments to #transaction, including requires_new: true' do
    it 'registers an offense' do
      expect_offense(<<~RUBY)
        ApplicationRecord.transaction(isolation: :read_committed, requires_new: true) do
                                                                  ^^^^^^^^^^^^^^^^^^ #{message}
          Project.create!(name: 'MyProject')
        end
      RUBY
    end
  end

  context 'when calling #transaction with requires_new: false' do
    it 'does not register an offense' do
      expect_no_offenses(<<~RUBY)
        ApplicationRecord.transaction(requires_new: false) do
          Project.create!(name: 'MyProject')
        end
      RUBY
    end
  end

  context 'when calling #transaction with other options' do
    it 'does not register an offense' do
      expect_no_offenses(<<~RUBY)
        ApplicationRecord.transaction(isolation: :read_committed) do
          Project.create!(name: 'MyProject')
        end
      RUBY
    end
  end

  context 'when calling #transaction with no arguments' do
    it 'does not register an offense' do
      expect_no_offenses(<<~RUBY)
        ApplicationRecord.transaction do
          Project.create!(name: 'MyProject')
        end
      RUBY
    end
  end
end