summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/migration/update_large_table_spec.rb
blob: 30cd84108dfd35bacb699f3ea935dcbbe4054754 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

require 'spec_helper'

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

require_relative '../../../../rubocop/cop/migration/update_large_table'

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

  subject(:cop) { described_class.new }

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

    shared_examples 'large tables' do |update_method|
      described_class::BLACKLISTED_TABLES.each do |table|
        it "registers an offense for the #{table} table" do
          inspect_source("#{update_method} :#{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
    end

    context 'for the add_column_with_default method' do
      include_examples 'large tables', 'add_column_with_default'
    end

    context 'for the change_column_type_concurrently method' do
      include_examples 'large tables', 'change_column_type_concurrently'
    end

    context 'for the rename_column_concurrently method' do
      include_examples 'large tables', 'rename_column_concurrently'
    end

    context 'for the update_column_in_batches method' do
      include_examples 'large tables', 'update_column_in_batches'
    end

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

      expect(cop.offenses).to be_empty
    end

    it 'registers no offense for non-blacklisted methods' do
      table = described_class::BLACKLISTED_TABLES.sample

      inspect_source("some_other_method :#{table}, :column, default: true")

      expect(cop.offenses).to be_empty
    end
  end

  context 'outside of migration' do
    let(:table) { described_class::BLACKLISTED_TABLES.sample }

    it 'registers no offense for add_column_with_default' do
      inspect_source("add_column_with_default :#{table}, :column, default: true")

      expect(cop.offenses).to be_empty
    end

    it 'registers no offense for change_column_type_concurrently' do
      inspect_source("change_column_type_concurrently :#{table}, :column, default: true")

      expect(cop.offenses).to be_empty
    end

    it 'registers no offense for rename_column_concurrently' do
      inspect_source("rename_column_concurrently :#{table}, :column, default: true")

      expect(cop.offenses).to be_empty
    end

    it 'registers no offense for update_column_concurrently' do
      inspect_source("update_column_concurrently :#{table}, :column, default: true")

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