diff options
Diffstat (limited to 'spec/rubocop/cop/usage_data')
-rw-r--r-- | spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb | 38 | ||||
-rw-r--r-- | spec/rubocop/cop/usage_data/large_table_spec.rb | 90 |
2 files changed, 128 insertions, 0 deletions
diff --git a/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb b/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb new file mode 100644 index 00000000000..db931c50bdf --- /dev/null +++ b/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/usage_data/distinct_count_by_large_foreign_key' + +RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey, type: :rubocop do + include CopHelper + + let(:allowed_foreign_keys) { %i[author_id user_id] } + + let(:config) do + RuboCop::Config.new('UsageData/DistinctCountByLargeForeignKey' => { + 'AllowedForeignKeys' => allowed_foreign_keys + }) + end + + subject(:cop) { described_class.new(config) } + + context 'when counting by disallowed key' do + it 'register an offence' do + inspect_source('distinct_count(Issue, :creator_id)') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when calling by allowed key' do + it 'does not register an offence' do + inspect_source('distinct_count(Issue, :author_id)') + + expect(cop.offenses).to be_empty + end + end +end diff --git a/spec/rubocop/cop/usage_data/large_table_spec.rb b/spec/rubocop/cop/usage_data/large_table_spec.rb new file mode 100644 index 00000000000..de6fb9c17e2 --- /dev/null +++ b/spec/rubocop/cop/usage_data/large_table_spec.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/usage_data/large_table' + +RSpec.describe RuboCop::Cop::UsageData::LargeTable, type: :rubocop do + include CopHelper + + let(:large_tables) { %i[Rails Time] } + let(:count_methods) { %i[count distinct_count] } + let(:allowed_methods) { %i[minimum maximum] } + + let(:config) do + RuboCop::Config.new('UsageData/LargeTable' => { + 'NonRelatedClasses' => large_tables, + 'CountMethods' => count_methods, + 'AllowedMethods' => allowed_methods + }) + end + + subject(:cop) { described_class.new(config) } + + 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 + it 'register an offence' do + inspect_source('Issue.count') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when calling Issue.active.count' do + it 'register an offence' do + inspect_source('Issue.active.count') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when calling count(Issue)' do + it 'does not register an offence' do + inspect_source('count(Issue)') + + expect(cop.offenses).to be_empty + end + end + + context 'when calling count(Ci::Build.active)' do + it 'does not register an offence' do + inspect_source('count(Ci::Build.active)') + + expect(cop.offenses).to be_empty + end + end + + context 'when calling Ci::Build.active.count' do + it 'register an offence' do + inspect_source('Ci::Build.active.count') + + expect(cop.offenses.size).to eq(1) + end + end + + context 'when using allowed methods' do + it 'does not register an offence' do + inspect_source('Issue.minimum') + + expect(cop.offenses).to be_empty + end + end + end + + context 'with non related class' do + it 'does not register an offence' do + inspect_source('Rails.count') + + expect(cop.offenses).to be_empty + end + end + end +end |