summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration/add_column_with_default_to_large_table_spec.rb
blob: 07cb3fc4a2eb1778ae173aac635f30a97ba09356 (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
require 'spec_helper'

require 'rubocop'
require 'rubocop/rspec/support'

require_relative '../../../../rubocop/cop/migration/add_column_with_default_to_large_table'

describe RuboCop::Cop::Migration::AddColumnWithDefaultToLargeTable do
  include CopHelper

  subject(:cop) { described_class.new }

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

    described_class::LARGE_TABLES.each do |table|
      it "registers an offense for the #{table} table" do
        inspect_source(cop, "add_column_with_default :#{table}, :column, default: true")

        aggregate_failures do
          expect(cop.offenses.size).to eq(1)
          expect(cop.offenses.map(&:line)).to eq([1])
        end
      end
    end

    it 'registers no offense for non-blacklisted tables' do
      inspect_source(cop, "add_column_with_default :table, :column, default: true")

      expect(cop.offenses).to be_empty
    end
  end

  context 'outside of migration' do
    it 'registers no offense' do
      table = described_class::LARGE_TABLES.sample
      inspect_source(cop, "add_column_with_default :#{table}, :column, default: true")

      expect(cop.offenses).to be_empty
    end
  end
end