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

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

RSpec.describe RuboCop::Cop::Migration::AddColumnsToWideTables do
  let(:cop) { described_class.new }

  context 'when outside of a migration' do
    it 'does not register any offenses' do
      expect_no_offenses(<<~RUBY)
        def up
          add_column(:users, :another_column, :string)
        end
      RUBY
    end
  end

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

    context 'with wide tables' do
      it 'registers an offense when adding a column to a wide table' do
        offense = '`projects` is a wide table with several columns, [...]'

        expect_offense(<<~RUBY)
          def up
            add_column(:projects, :another_column, :integer)
            ^^^^^^^^^^ #{offense}
          end
        RUBY
      end

      it 'registers an offense when adding a column with default to a wide table' do
        offense = '`users` is a wide table with several columns, [...]'

        expect_offense(<<~RUBY)
          def up
            add_column(:users, :another_column, :boolean, default: false)
            ^^^^^^^^^^ #{offense}
          end
        RUBY
      end

      it 'registers an offense when adding a reference' do
        offense = '`ci_builds` is a wide table with several columns, [...]'

        expect_offense(<<~RUBY)
          def up
            add_reference(:ci_builds, :issue, :boolean, index: true)
            ^^^^^^^^^^^^^ #{offense}
          end
        RUBY
      end

      it 'registers an offense when adding timestamps' do
        offense = '`projects` is a wide table with several columns, [...]'

        expect_offense(<<~RUBY)
          def up
            add_timestamps_with_timezone(:projects, null: false)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{offense}
          end
        RUBY
      end

      it 'register no offense when using other method' do
        expect_no_offenses(<<~RUBY)
          def up
            add_concurrent_index(:projects, :new_index)
          end
        RUBY
      end
    end

    context 'with a regular table' do
      it 'registers no offense for notes' do
        expect_no_offenses(<<~RUBY)
          def up
            add_column(:notes, :another_column, :boolean)
          end
        RUBY
      end
    end
  end
end