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

ignored_columns_spec.rb « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8f47f8aee920c09ab75a7a87b56f9a0787586da (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
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../rubocop/cop/ignored_columns'

RSpec.describe RuboCop::Cop::IgnoredColumns, feature_category: :database do
  it 'flags use of `self.ignored_columns +=` instead of the IgnorableColumns concern' do
    expect_offense(<<~RUBY)
      class Foo < ApplicationRecord
        self.ignored_columns += %i[id]
             ^^^^^^^^^^^^^^^ Use `IgnorableColumns` concern instead of adding to `self.ignored_columns`.
      end
    RUBY
  end

  it 'flags use of `self.ignored_columns =` instead of the IgnorableColumns concern' do
    expect_offense(<<~RUBY)
      class Foo < ApplicationRecord
        self.ignored_columns = %i[id]
             ^^^^^^^^^^^^^^^ Use `IgnorableColumns` concern instead of setting `self.ignored_columns`.
      end
    RUBY
  end

  context 'when only CE model exist' do
    let(:file_path) { full_path('app/models/bar.rb') }

    it 'does not flag `ignore_columns` usage in CE model' do
      expect_no_offenses(<<~RUBY, file_path)
        class Bar < ApplicationRecord
          ignore_columns :foo, remove_with: '14.3', remove_after: '2021-09-22'
        end
      RUBY
    end

    it 'does not flag `ignore_column` usage in CE model' do
      expect_no_offenses(<<~RUBY, file_path)
        class Baz < ApplicationRecord
          ignore_column :bar, remove_with: '14.3', remove_after: '2021-09-22'
        end
      RUBY
    end
  end

  context 'when only EE model exist' do
    let(:file_path) { full_path('ee/app/models/ee/bar.rb') }

    before do
      allow(File).to receive(:exist?).with(full_path('app/models/bar.rb')).and_return(false)
    end

    it 'does not flag `ignore_columns` usage in EE model' do
      expect_no_offenses(<<~RUBY, file_path)
        class Bar < ApplicationRecord
          ignore_columns :foo, remove_with: '14.3', remove_after: '2021-09-22'
        end
      RUBY
    end

    it 'does not flag `ignore_column` usage in EE model' do
      expect_no_offenses(<<~RUBY, file_path)
        class Bar < ApplicationRecord
          ignore_column :foo, remove_with: '14.3', remove_after: '2021-09-22'
        end
      RUBY
    end
  end

  context 'when CE and EE model exist' do
    let(:file_path) { full_path('ee/app/models/ee/bar.rb') }

    before do
      allow(File).to receive(:exist?).with(full_path('app/models/bar.rb')).and_return(true)
    end

    it 'flags `ignore_columns` usage in EE model' do
      expect_offense(<<~RUBY, file_path)
        class Bar < ApplicationRecord
          ignore_columns :foo, remove_with: '14.3', remove_after: '2021-09-22'
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If the model exists in CE and EE, [...]
        end
      RUBY
    end

    it 'flags `ignore_column` usage in EE model' do
      expect_offense(<<~RUBY, file_path)
        class Bar < ApplicationRecord
          ignore_column :foo, remove_with: '14.3', remove_after: '2021-09-22'
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If the model exists in CE and EE, [...]
        end
      RUBY
    end
  end

  private

  def full_path(path)
    rails_root = '../../../'

    File.expand_path(File.join(rails_root, path), __dir__)
  end
end