-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
dynamic_find_by_spec.rb
270 lines (231 loc) · 7.62 KB
/
dynamic_find_by_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::DynamicFindBy, :config do
let(:cop_config) do
{ 'AllowedMethods' => %w[find_by_sql] }
end
context 'with dynamic find_by_*' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_name(name)
^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
RUBY
expect_correction(<<~RUBY)
User.find_by(name: name)
RUBY
end
end
context 'with dynamic find_by_*_and_*' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_name_and_email(name, email)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY
expect_correction(<<~RUBY)
User.find_by(name: name, email: email)
RUBY
end
end
context 'with dynamic find_by_*!' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_name!(name)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by!` instead of dynamic `find_by_name!`.
RUBY
expect_correction(<<~RUBY)
User.find_by!(name: name)
RUBY
end
end
context 'with dynamic find_by_*_and_*_and_*' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_name_and_email_and_token(name, email, token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email_and_token`.
RUBY
expect_correction(<<~RUBY)
User.find_by(name: name, email: email, token: token)
RUBY
end
end
context 'with dynamic find_by_*_and_*_and_*!' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_name_and_email_and_token!(name, email, token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by!` instead of dynamic `find_by_name_and_email_and_token!`.
RUBY
expect_correction(<<~RUBY)
User.find_by!(name: name, email: email, token: token)
RUBY
end
end
context 'with dynamic find_by_*_and_*_and_* with newline' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_name_and_email_and_token(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email_and_token`.
name,
email,
token
)
RUBY
expect_correction(<<~RUBY)
User.find_by(
name: name,
email: email,
token: token
)
RUBY
end
end
context 'with column includes underscore' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
User.find_by_first_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_first_name`.
RUBY
expect_correction(<<~RUBY)
User.find_by(first_name: name)
RUBY
end
end
context 'with too much arguments' do
it 'registers an offense and no corrects' do
expect_offense(<<~RUBY)
User.find_by_name_and_email(name, email, token)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY
expect_no_corrections
end
end
context 'with too few arguments' do
it 'registers an offense and no corrects' do
expect_offense(<<~RUBY)
User.find_by_name_and_email(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name_and_email`.
RUBY
expect_no_corrections
end
end
it 'accepts' do
expect_no_offenses('User.find_by(name: name)')
end
it 'accepts splat argument' do
expect_no_offenses('User.find_by_scan(*args)')
end
it 'accepts any of the arguments are splat argument' do
expect_no_offenses('User.find_by_foo_and_bar(arg, *args)')
end
it 'accepts dynamic finder with single hash argument' do
expect_no_offenses('Post.find_by_id(limit: 1)')
end
it 'accepts dynamic finder with multiple arguments including hash' do
expect_no_offenses('Post.find_by_title_and_id("foo", limit: 1)')
end
it 'accepts method in whitelist' do
expect_no_offenses(<<~RUBY)
User.find_by_sql(["select * from users where name = ?", name])
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
def do_something
find_by_name(name)
end
end
RUBY
end
it 'does not register an offense when not inheriting `ApplicationRecord`' do
expect_no_offenses(<<~RUBY)
class C < Foo
def do_something
find_by_name(name)
end
end
RUBY
end
it 'registers an offense when inheriting `ApplicationRecord`' do
expect_offense(<<~RUBY)
class C < ApplicationRecord
def do_something
find_by_name(name)
^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
end
end
RUBY
end
it 'registers an offense when inheriting `ActiveRecord::Base`' do
expect_offense(<<~RUBY)
class C < ActiveRecord::Base
def do_something
find_by_name(name)
^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
end
end
RUBY
end
end
context 'with allowed receiver name' do
let(:cop_config) do
{ 'AllowedReceivers' => %w[Gem::Specification] }
end
it 'accepts dynamic find_by for receiver names in whitelist' do
expect_no_offenses(<<~RUBY)
Gem::Specification.find_by_name("backend").gem_dir
RUBY
end
it 'registers offense for receiver name with no namespace' do
expect_offense(<<~RUBY)
Specification.find_by_name("backend").gem_dir
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
RUBY
end
it 'registers offense for receiver name with different namespace' do
expect_offense(<<~RUBY)
RubyGems::Specification.find_by_name("backend").gem_dir
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
RUBY
end
end
context 'when using safe navigation operator' do
context 'with dynamic find_by_*' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
user&.find_by_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
RUBY
expect_correction(<<~RUBY)
user&.find_by(name: name)
RUBY
end
end
end
# Whitelisted config will be deprecated.
context 'with WhiteListed config' do
context 'allowed class-names' do
let(:cop_config) do
{ 'Whitelist' => %w[Specification] }
end
# `Whitelist` should not allow receivers for API compatibility.
# New API `AllowedReceivers` should be used to add a receiver
# to the allowed receivers.
it 'registers offense for class methods in whitelist' do
expect_offense(<<~RUBY)
Specification.find_by_name("backend").gem_dir
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `find_by` instead of dynamic `find_by_name`.
RUBY
end
end
context 'allowed method-names' do
let(:cop_config) do
{ 'Whitelist' => %w[find_by_name] }
end
it 'allows for class methods in whitelist' do
expect_no_offenses(<<~RUBY)
User.find_by_name("backend").gem_dir
RUBY
end
end
end
end