debian-mirror-gitlab/spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb

48 lines
1.5 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/usage_data/distinct_count_by_large_foreign_key'
2021-03-08 18:12:59 +05:30
RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey do
2020-11-24 15:15:51 +05:30
let(:allowed_foreign_keys) { [:author_id, :user_id, :'merge_requests.target_project_id'] }
2021-03-11 19:13:27 +05:30
let(:msg) { 'Avoid doing `distinct_count` on foreign keys for large tables having above 100 million rows.' }
2020-10-24 23:57:45 +05:30
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
2021-03-11 19:13:27 +05:30
it 'registers an offense' do
expect_offense(<<~CODE)
distinct_count(Issue, :creator_id)
^^^^^^^^^^^^^^ #{msg}
CODE
2020-10-24 23:57:45 +05:30
end
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it 'does not register an offense when batch is false' do
expect_no_offenses('distinct_count(Issue, :creator_id, batch: false)')
2020-11-24 15:15:51 +05:30
end
2021-03-11 19:13:27 +05:30
it 'registers an offense when batch is true' do
expect_offense(<<~CODE)
distinct_count(Issue, :creator_id, batch: true)
^^^^^^^^^^^^^^ #{msg}
CODE
2020-11-24 15:15:51 +05:30
end
2020-10-24 23:57:45 +05:30
end
context 'when calling by allowed key' do
2021-03-11 19:13:27 +05:30
it 'does not register an offense with symbol' do
expect_no_offenses('distinct_count(Issue, :author_id)')
2020-10-24 23:57:45 +05:30
end
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it 'does not register an offense with string' do
expect_no_offenses("distinct_count(Issue, 'merge_requests.target_project_id')")
2020-11-24 15:15:51 +05:30
end
2020-10-24 23:57:45 +05:30
end
end