-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
find_each_spec.rb
189 lines (157 loc) · 6.37 KB
/
find_each_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::FindEach, :config do
shared_examples 'register_offense' do |scope|
it "registers an offense when using #{scope}.each" do
expect_offense(<<~RUBY, scope: scope)
User.#{scope}.each { |u| u.something }
_{scope} ^^^^ Use `find_each` instead of `each`.
RUBY
end
it "does not register an offense when using #{scope}.order(...).each" do
expect_no_offenses("User.#{scope}.order(:name).each { |u| u.something }")
end
it "does not register an offense when using #{scope}.limit(...).each" do
expect_no_offenses("User.#{scope}.limit(10).each { |u| u.something }")
end
it "does not register an offense when using #{scope}.select(...).each" do
expect_no_offenses("User.#{scope}.select(:name, :age).each { |u| u.something }")
end
end
it_behaves_like('register_offense', 'all')
it_behaves_like('register_offense', 'eager_load(:association_name)')
it_behaves_like('register_offense', 'includes(:association_name)')
it_behaves_like('register_offense', 'joins(:association_name)')
it_behaves_like('register_offense', 'left_joins(:association_name)')
it_behaves_like('register_offense', 'left_outer_joins(:association_name)')
it_behaves_like('register_offense', 'preload(:association_name)')
it_behaves_like('register_offense', 'references(:association_name)')
it_behaves_like('register_offense', 'unscoped')
it_behaves_like('register_offense', 'where(name: name)')
it_behaves_like('register_offense', 'where.not(name: name)')
it 'does not register an offense when called on a constant' do
expect_no_offenses('FOO.each { |u| u.x }')
end
it 'does not register an offense when using find_by' do
expect_no_offenses('User.all.find_each { |u| u.x }')
end
# Active Model Errors slice from the new query interface introduced in Rails 6.1.
it 'does not register an offense when using `model.errors.where`' do
expect_no_offenses(<<~RUBY)
class Model < ApplicationRecord
model.errors.where(:title).each { |error| do_something(error) }
end
RUBY
end
it 'registers an offense when using `where` with no receiver' do
expect_offense(<<~RUBY)
class Model < ApplicationRecord
where(record: [record1, record2]).each(&:touch)
^^^^ Use `find_each` instead of `each`.
end
RUBY
end
it 'autocorrects each to find_each' do
expect_offense(<<~RUBY)
User.all.each { |u| u.x }
^^^^ Use `find_each` instead of `each`.
RUBY
expect_correction(<<~RUBY)
User.all.find_each { |u| u.x }
RUBY
end
it 'registers an offense with non-send ancestors' do
expect_offense(<<~RUBY)
class C; User.all.each { |u| u.x }; end
^^^^ Use `find_each` instead of `each`.
RUBY
expect_correction(<<~RUBY)
class C; User.all.find_each { |u| u.x }; end
RUBY
end
context 'with no receiver' do
it 'does not register an offense when not inheriting any class' do
expect_no_offenses(<<~RUBY)
class C
all.each { |u| u.x }
end
RUBY
end
it 'does not register an offense when not inheriting `ApplicationRecord`' do
expect_no_offenses(<<~RUBY)
class C < Foo
all.each { |u| u.x }
end
RUBY
end
it 'registers an offense when inheriting `ApplicationRecord`' do
expect_offense(<<~RUBY)
class C < ApplicationRecord
all.each { |u| u.x }
^^^^ Use `find_each` instead of `each`.
end
RUBY
end
it 'registers an offense when inheriting `ActiveRecord::Base`' do
expect_offense(<<~RUBY)
class C < ActiveRecord::Base
all.each { |u| u.x }
^^^^ Use `find_each` instead of `each`.
end
RUBY
end
end
context 'allowed methods' do
let(:cop_config) { { 'AllowedMethods' => %w[order lock], 'AllowedPatterns' => [], 'IgnoredMethods' => [] } }
it 'does not register an offense when using order(...) earlier' do
expect_no_offenses('User.order(:name).each { |u| u.something }')
end
it 'does not register an offense when using order(...) chained with other things' do
expect_no_offenses('User.order(:name).includes(:company).each { |u| u.something }')
end
it 'does not register an offense when using lock earlier' do
expect_no_offenses('User.lock.each { |u| u.something }')
end
it 'registers offense for methods not in `AllowedMethods`' do
expect_offense(<<~RUBY)
User.joins(:posts).each { |u| u.something }
^^^^ Use `find_each` instead of `each`.
RUBY
end
end
context 'allowed patterns' do
let(:cop_config) { { 'AllowedMethods' => [], 'AllowedPatterns' => [/order/, /lock/], 'IgnoredMethods' => [] } }
it 'does not register an offense when using order(...) earlier' do
expect_no_offenses('User.order(:name).each { |u| u.something }')
end
it 'does not register an offense when using order(...) chained with other things' do
expect_no_offenses('User.order(:name).includes(:company).each { |u| u.something }')
end
it 'does not register an offense when using lock earlier' do
expect_no_offenses('User.lock.each { |u| u.something }')
end
it 'registers offense for methods not in `AllowedPatterns`' do
expect_offense(<<~RUBY)
User.joins(:posts).each { |u| u.something }
^^^^ Use `find_each` instead of `each`.
RUBY
end
end
context 'ignored methods' do
let(:cop_config) { { 'AllowedPatterns' => [], 'AllowedMethods' => [], 'IgnoredMethods' => %w[order lock] } }
it 'does not register an offense when using order(...) earlier' do
expect_no_offenses('User.order(:name).each { |u| u.something }')
end
it 'does not register an offense when using order(...) chained with other things' do
expect_no_offenses('User.order(:name).includes(:company).each { |u| u.something }')
end
it 'does not register an offense when using lock earlier' do
expect_no_offenses('User.lock.each { |u| u.something }')
end
it 'registers offense for methods not in `IgnoredMethods`' do
expect_offense(<<~RUBY)
User.joins(:posts).each { |u| u.something }
^^^^ Use `find_each` instead of `each`.
RUBY
end
end
end