Commit 7b753e95 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch...

Merge branch '250630-ensure-distinctcountbylargeforeignkey-only-works-for-batched-counts' into 'master'

DistinctCountByLargeForeignKey  should only work for batch counts

See merge request gitlab-org/gitlab!42559
parents faa89dfc cdec4a94
......@@ -22,6 +22,7 @@ module RuboCop
def on_send(node)
distinct_count?(node) do |method_name, method_arguments|
next unless method_arguments && method_arguments.length >= 2
next if batch_set_to_false?(method_arguments[2])
next if allowed_foreign_key?(method_arguments[1])
add_offense(node, location: :selector, message: format(MSG, method_name))
......@@ -37,6 +38,21 @@ module RuboCop
def allowed_foreign_keys
(cop_config['AllowedForeignKeys'] || []).map(&:to_s)
end
def batch_set_to_false?(options)
return false unless options.is_a?(RuboCop::AST::HashNode)
batch_set_to_false = false
options.each_pair do |key, value|
next unless value.boolean_type? && value.falsey_literal?
next unless key.type == :sym && key.value == :batch
batch_set_to_false = true
break
end
batch_set_to_false
end
end
end
end
......
......@@ -21,11 +21,23 @@ RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey, type: :r
subject(:cop) { described_class.new(config) }
context 'when counting by disallowed key' do
it 'register an offence' do
it 'registers an offence' do
inspect_source('distinct_count(Issue, :creator_id)')
expect(cop.offenses.size).to eq(1)
end
it 'does not register an offence when batch is false' do
inspect_source('distinct_count(Issue, :creator_id, batch: false)')
expect(cop.offenses).to be_empty
end
it 'register an offence when batch is true' do
inspect_source('distinct_count(Issue, :creator_id, batch: true)')
expect(cop.offenses.size).to eq(1)
end
end
context 'when calling by allowed key' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment