summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/code_reuse/active_record_spec.rb
blob: a30fc52d26fac471085f64dd0f9e30496e0a1547 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/code_reuse/active_record'

describe RuboCop::Cop::CodeReuse::ActiveRecord do
  include CopHelper

  subject(:cop) { described_class.new }

  it 'flags the use of "where" without any arguments' do
    expect_offense(<<~SOURCE)
    def foo
      User.where
           ^^^^^ This method can only be used inside an ActiveRecord model
    end
    SOURCE
  end

  it 'flags the use of "where" with arguments' do
    expect_offense(<<~SOURCE)
    def foo
      User.where(id: 10)
           ^^^^^ This method can only be used inside an ActiveRecord model
    end
    SOURCE
  end

  it 'does not flag the use of "group" without any arguments' do
    expect_no_offenses(<<~SOURCE)
    def foo
      project.group
    end
    SOURCE
  end

  it 'flags the use of "group" with arguments' do
    expect_offense(<<~SOURCE)
    def foo
      project.group(:name)
              ^^^^^ This method can only be used inside an ActiveRecord model
    end
    SOURCE
  end

  it 'does not flag the use of ActiveRecord models in a model' do
    path = Rails.root.join('app', 'models', 'foo.rb').to_s

    expect_no_offenses(<<~SOURCE, path)
    def foo
      project.group(:name)
    end
    SOURCE
  end

  it 'does not flag the use of ActiveRecord models in a spec' do
    path = Rails.root.join('spec', 'foo_spec.rb').to_s

    expect_no_offenses(<<~SOURCE, path)
    def foo
      project.group(:name)
    end
    SOURCE
  end

  it 'does not flag the use of ActiveRecord models in a background migration' do
    path = Rails
      .root
      .join('lib', 'gitlab', 'background_migration', 'foo.rb')
      .to_s

    expect_no_offenses(<<~SOURCE, path)
    def foo
      project.group(:name)
    end
    SOURCE
  end

  it 'does not flag the use of ActiveRecord models in lib/gitlab/database' do
    path = Rails.root.join('lib', 'gitlab', 'database', 'foo.rb').to_s

    expect_no_offenses(<<~SOURCE, path)
    def foo
      project.group(:name)
    end
    SOURCE
  end

  it 'autocorrects offenses in instance methods by whitelisting them' do
    corrected = autocorrect_source(<<~SOURCE)
    def foo
      User.where
    end
    SOURCE

    expect(corrected).to eq(<<~SOURCE)
    # rubocop: disable CodeReuse/ActiveRecord
    def foo
      User.where
    end
    # rubocop: enable CodeReuse/ActiveRecord
    SOURCE
  end

  it 'autocorrects offenses in class methods by whitelisting them' do
    corrected = autocorrect_source(<<~SOURCE)
    def self.foo
      User.where
    end
    SOURCE

    expect(corrected).to eq(<<~SOURCE)
    # rubocop: disable CodeReuse/ActiveRecord
    def self.foo
      User.where
    end
    # rubocop: enable CodeReuse/ActiveRecord
    SOURCE
  end

  it 'autocorrects offenses in blocks by whitelisting them' do
    corrected = autocorrect_source(<<~SOURCE)
    get '/' do
      User.where
    end
    SOURCE

    expect(corrected).to eq(<<~SOURCE)
    # rubocop: disable CodeReuse/ActiveRecord
    get '/' do
      User.where
    end
    # rubocop: enable CodeReuse/ActiveRecord
    SOURCE
  end
end