Welcome to mirror list, hosted at ThFree Co, Russian Federation.

add_reference_spec.rb « migration « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c348fc0efac0af08848c585952d73f9eb03a08f7 (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
require 'spec_helper'

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

require_relative '../../../../rubocop/cop/migration/add_reference'

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

  let(:cop) { described_class.new }

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

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

    it 'registers an offense when using add_reference without index' do
      expect_offense(<<~RUBY)
        call do
          add_reference(:projects, :users)
          ^^^^^^^^^^^^^ `add_reference` requires `index: true` or `index: { options... }`
        end
      RUBY
    end

    it 'registers an offense when using add_reference index disabled' do
      expect_offense(<<~RUBY)
        def up
          add_reference(:projects, :users, index: false)
          ^^^^^^^^^^^^^ `add_reference` requires `index: true` or `index: { options... }`
        end
      RUBY
    end

    it 'does not register an offense when using add_reference with index enabled' do
      expect_no_offenses(<<~RUBY)
        def up
          add_reference(:projects, :users, index: true)
        end
      RUBY
    end

    it 'does not register an offense when the index is unique' do
      expect_no_offenses(<<~RUBY)
        def up
          add_reference(:projects, :users, index: { unique: true } )
        end
      RUBY
    end
  end
end