2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'rubocop_spec_helper'
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
require_relative '../../../../rubocop/cop/usage_data/large_table'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::UsageData::LargeTable do
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:large_tables) { %i[Rails Time] }
|
|
|
|
let(:count_methods) { %i[count distinct_count] }
|
|
|
|
let(:allowed_methods) { %i[minimum maximum] }
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:msg) { 'Use one of the count, distinct_count methods for counting on' }
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
let(:config) do
|
|
|
|
RuboCop::Config.new('UsageData/LargeTable' => {
|
|
|
|
'NonRelatedClasses' => large_tables,
|
|
|
|
'CountMethods' => count_methods,
|
|
|
|
'AllowedMethods' => allowed_methods
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in usage_data files' do
|
|
|
|
before do
|
|
|
|
allow(cop).to receive(:usage_data_files?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with large tables' do
|
|
|
|
context 'when calling Issue.count' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense' do
|
|
|
|
expect_offense(<<~CODE)
|
|
|
|
Issue.count
|
|
|
|
^^^^^^^^^^^ #{msg} Issue
|
|
|
|
CODE
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when calling Issue.active.count' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense' do
|
|
|
|
expect_offense(<<~CODE)
|
|
|
|
Issue.active.count
|
|
|
|
^^^^^^^^^^^^ #{msg} Issue
|
|
|
|
CODE
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when calling count(Issue)' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses('count(Issue)')
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when calling count(Ci::Build.active)' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses('count(Ci::Build.active)')
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when calling Ci::Build.active.count' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense' do
|
|
|
|
expect_offense(<<~CODE)
|
|
|
|
Ci::Build.active.count
|
|
|
|
^^^^^^^^^^^^^^^^ #{msg} Ci::Build
|
|
|
|
CODE
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using allowed methods' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses('Issue.minimum')
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with non related class' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'does not register an offense' do
|
|
|
|
expect_no_offenses('Rails.count')
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|