summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration/background_migration_record_spec.rb
blob: d5a451e00c9fa94d698f85a55f425482be13839a (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
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/migration/background_migration_record'

RSpec.describe RuboCop::Cop::Migration::BackgroundMigrationRecord do
  context 'outside of a migration' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~SOURCE)
        class MigrateProjectRecords
          class Project < ActiveRecord::Base
          end
        end
      SOURCE
    end
  end

  context 'in migration' do
    before do
      allow(cop).to receive(:in_background_migration?).and_return(true)
    end

    it 'adds an offense if inheriting from ActiveRecord::Base' do
      expect_offense(<<~RUBY)
        class MigrateProjectRecords
          class Project < ActiveRecord::Base
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use or inherit from ActiveRecord::Base.[...]
          end
        end
      RUBY
    end

    it 'adds an offense if create dynamic model from ActiveRecord::Base' do
      expect_offense(<<~RUBY)
        class MigrateProjectRecords
          def define_model(table_name)
            Class.new(ActiveRecord::Base) do
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use or inherit from ActiveRecord::Base.[...]
              self.table_name = table_name
              self.inheritance_column = :_type_disabled
            end
          end
        end
      RUBY
    end

    it 'adds an offense if inheriting from ::ActiveRecord::Base' do
      expect_offense(<<~RUBY)
        class MigrateProjectRecords
          class Project < ::ActiveRecord::Base
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use or inherit from ActiveRecord::Base.[...]
          end
        end
      RUBY
    end
  end
end