2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'fast_spec_helper'
|
2018-12-05 23:21:45 +05:30
|
|
|
require 'rubocop'
|
|
|
|
require_relative '../../../../rubocop/cop/code_reuse/active_record'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe RuboCop::Cop::CodeReuse::ActiveRecord, type: :rubocop do
|
2018-12-05 23:21:45 +05:30
|
|
|
include CopHelper
|
|
|
|
|
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
it 'flags the use of "where" without any arguments' do
|
|
|
|
expect_offense(<<~SOURCE)
|
|
|
|
def foo
|
|
|
|
User.where
|
2019-12-04 20:38:33 +05:30
|
|
|
^^^^^ This method can only be used inside an ActiveRecord model: https://gitlab.com/gitlab-org/gitlab-foss/issues/49653
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
SOURCE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flags the use of "where" with arguments' do
|
|
|
|
expect_offense(<<~SOURCE)
|
|
|
|
def foo
|
|
|
|
User.where(id: 10)
|
2019-12-04 20:38:33 +05:30
|
|
|
^^^^^ This method can only be used inside an ActiveRecord model: https://gitlab.com/gitlab-org/gitlab-foss/issues/49653
|
2018-12-05 23:21:45 +05:30
|
|
|
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)
|
2019-12-04 20:38:33 +05:30
|
|
|
^^^^^ This method can only be used inside an ActiveRecord model: https://gitlab.com/gitlab-org/gitlab-foss/issues/49653
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
SOURCE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not flag the use of ActiveRecord models in a model' do
|
2020-07-28 23:09:34 +05:30
|
|
|
path = rails_root_join('app', 'models', 'foo.rb').to_s
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
path = rails_root_join('spec', 'foo_spec.rb').to_s
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
path = rails_root_join('lib', 'gitlab', 'background_migration', 'foo.rb').to_s
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
path = rails_root_join('lib', 'gitlab', 'database', 'foo.rb').to_s
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
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
|