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

require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/migration/migration_record'

RSpec.describe RuboCop::Cop::Migration::MigrationRecord do
  subject(:cop) { described_class.new }

  shared_examples 'a disabled cop' do |klass|
    it 'does not register any offenses' do
      expect_no_offenses(<<~SOURCE)
        class MyMigration < Gitlab::Database::Migration[2.0]
          class Project < #{klass}
          end
        end
      SOURCE
    end
  end

  %w(ActiveRecord::Base ApplicationRecord).each do |klass|
    context 'outside of a migration' do
      it_behaves_like 'a disabled cop', klass
    end

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

      context 'in an old migration' do
        before do
          allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE - 5)
        end

        it_behaves_like 'a disabled cop', klass
      end

      context 'that is recent' do
        before do
          allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE)
        end

        it "adds an offense if inheriting from #{klass}" do
          expect_offense(<<~RUBY)
          class Project < #{klass}
          ^^^^^^^^^^^^^^^^#{'^' * klass.length} Don't inherit from ActiveRecord::Base or ApplicationRecord but use MigrationRecord instead.[...]
          end
          RUBY
        end

        it "adds an offense if inheriting from ::#{klass}" do
          expect_offense(<<~RUBY)
          class Project < ::#{klass}
          ^^^^^^^^^^^^^^^^^^#{'^' * klass.length} Don't inherit from ActiveRecord::Base or ApplicationRecord but use MigrationRecord instead.[...]
          end
          RUBY
        end
      end
    end
  end
end