summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/active_record_association_reload_spec.rb
diff options
context:
space:
mode:
authorKamil Trzciński <ayufan@ayufan.eu>2019-04-08 15:33:36 +0200
committerKamil Trzciński <ayufan@ayufan.eu>2019-04-15 13:05:14 +0200
commit650f40865e5d8136cb366fbde689c4100aafb0c5 (patch)
treef740d9f235693ad8e2a3693d6ec30dee26d2a74a /spec/rubocop/cop/active_record_association_reload_spec.rb
parent7457c1e1229cd1e90e608e8b247e2fbb217f05b6 (diff)
downloadgitlab-ce-650f40865e5d8136cb366fbde689c4100aafb0c5.tar.gz
Forbid the use of `#reload` and prefer `#reset`forbid-the-usage-of-reload
The `#reload` makes to load all objects into memory, and the main purpose of `#reload` is to drop the association cache. The `#reset` seems to solve exactly that case.
Diffstat (limited to 'spec/rubocop/cop/active_record_association_reload_spec.rb')
-rw-r--r--spec/rubocop/cop/active_record_association_reload_spec.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/rubocop/cop/active_record_association_reload_spec.rb b/spec/rubocop/cop/active_record_association_reload_spec.rb
new file mode 100644
index 00000000000..69eb16a54d2
--- /dev/null
+++ b/spec/rubocop/cop/active_record_association_reload_spec.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require 'rubocop'
+require_relative '../../../rubocop/cop/active_record_association_reload'
+
+describe RuboCop::Cop::ActiveRecordAssociationReload do
+ include CopHelper
+
+ subject(:cop) { described_class.new }
+
+ context 'when using ActiveRecord::Base' do
+ it 'registers an offense on reload usage' do
+ expect_offense(<<~PATTERN.strip_indent)
+ users = User.all
+ users.reload
+ ^^^^^^ Use reset instead of reload. For more details check the https://gitlab.com/gitlab-org/gitlab-ce/issues/60218.
+ PATTERN
+ end
+
+ it 'does not register an offense on reset usage' do
+ expect_no_offenses(<<~PATTERN.strip_indent)
+ users = User.all
+ users.reset
+ PATTERN
+ end
+ end
+
+ context 'when using ActiveRecord::Relation' do
+ it 'registers an offense on reload usage' do
+ expect_offense(<<~PATTERN.strip_indent)
+ user = User.new
+ user.reload
+ ^^^^^^ Use reset instead of reload. For more details check the https://gitlab.com/gitlab-org/gitlab-ce/issues/60218.
+ PATTERN
+ end
+
+ it 'does not register an offense on reset usage' do
+ expect_no_offenses(<<~PATTERN.strip_indent)
+ user = User.new
+ user.reset
+ PATTERN
+ end
+ end
+
+ context 'when using on self' do
+ it 'registers an offense on reload usage' do
+ expect_offense(<<~PATTERN.strip_indent)
+ reload
+ ^^^^^^ Use reset instead of reload. For more details check the https://gitlab.com/gitlab-org/gitlab-ce/issues/60218.
+ PATTERN
+ end
+
+ it 'does not register an offense on reset usage' do
+ expect_no_offenses(<<~PATTERN.strip_indent)
+ reset
+ PATTERN
+ end
+ end
+end