summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-04-14 15:09:44 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-04-14 15:09:44 +0000
commit874ead9c3a50de4c4ca4551eaf5b7eb976d26b50 (patch)
tree637ee9f2da5e251bc08ebf3e972209d51966bf7c /spec/rubocop/cop
parent2e4c4055181eec9186458dd5dd3219c937032ec7 (diff)
downloadgitlab-ce-874ead9c3a50de4c4ca4551eaf5b7eb976d26b50.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop/cop')
-rw-r--r--spec/rubocop/cop/performance/ar_count_each_spec.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/rubocop/cop/performance/ar_count_each_spec.rb b/spec/rubocop/cop/performance/ar_count_each_spec.rb
new file mode 100644
index 00000000000..f934a1fde48
--- /dev/null
+++ b/spec/rubocop/cop/performance/ar_count_each_spec.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+require_relative '../../../support/helpers/expect_offense'
+require_relative '../../../../rubocop/cop/performance/ar_count_each.rb'
+
+describe RuboCop::Cop::Performance::ARCountEach do
+ include CopHelper
+ include ExpectOffense
+
+ subject(:cop) { described_class.new }
+
+ context 'when it is not haml file' do
+ it 'does not flag it as an offense' do
+ expect(subject).to receive(:in_haml_file?).with(anything).at_least(:once).and_return(false)
+
+ expect_no_offenses <<~SOURCE
+ show(@users.count)
+ @users.each { |user| display(user) }
+ SOURCE
+ end
+ end
+
+ context 'when it is haml file' do
+ before do
+ expect(subject).to receive(:in_haml_file?).with(anything).at_least(:once).and_return(true)
+ end
+
+ context 'when the same object uses count and each' do
+ it 'flags it as an offense' do
+ expect_offense <<~SOURCE
+ show(@users.count)
+ ^^^^^^^^^^^^ If @users is AR relation, avoid `@users.count ...; @users.each... `, this will trigger two queries. Use `@users.load.size ...; @users.each... ` instead. If @users is an array, try to use @users.size.
+ @users.each { |user| display(user) }
+ SOURCE
+
+ expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARCountEach')
+ end
+ end
+
+ context 'when different object uses count and each' do
+ it 'does not flag it as an offense' do
+ expect_no_offenses <<~SOURCE
+ show(@emails.count)
+ @users.each { |user| display(user) }
+ SOURCE
+ end
+ end
+
+ context 'when just using count without each' do
+ it 'does not flag it as an offense' do
+ expect_no_offenses '@users.count'
+ end
+ end
+
+ context 'when just using each without count' do
+ it 'does not flag it as an offense' do
+ expect_no_offenses '@users.each { |user| display(user) }'
+ end
+ end
+ end
+end