Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
cdec4a94
Commit
cdec4a94
authored
Sep 17, 2020
by
Alper Akgun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DistinctCountByLargeForeignKey should only work for batch counts
parent
fe61831f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
1 deletion
+29
-1
rubocop/cop/usage_data/distinct_count_by_large_foreign_key.rb
...cop/cop/usage_data/distinct_count_by_large_foreign_key.rb
+16
-0
spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb
...op/usage_data/distinct_count_by_large_foreign_key_spec.rb
+13
-1
No files found.
rubocop/cop/usage_data/distinct_count_by_large_foreign_key.rb
View file @
cdec4a94
...
...
@@ -22,6 +22,7 @@ module RuboCop
def
on_send
(
node
)
distinct_count?
(
node
)
do
|
method_name
,
method_arguments
|
next
unless
method_arguments
&&
method_arguments
.
length
>=
2
next
if
batch_set_to_false?
(
method_arguments
[
2
])
next
if
allowed_foreign_key?
(
method_arguments
[
1
])
add_offense
(
node
,
location: :selector
,
message:
format
(
MSG
,
method_name
))
...
...
@@ -37,6 +38,21 @@ module RuboCop
def
allowed_foreign_keys
(
cop_config
[
'AllowedForeignKeys'
]
||
[]).
map
(
&
:to_s
)
end
def
batch_set_to_false?
(
options
)
return
false
unless
options
.
is_a?
(
RuboCop
::
AST
::
HashNode
)
batch_set_to_false
=
false
options
.
each_pair
do
|
key
,
value
|
next
unless
value
.
boolean_type?
&&
value
.
falsey_literal?
next
unless
key
.
type
==
:sym
&&
key
.
value
==
:batch
batch_set_to_false
=
true
break
end
batch_set_to_false
end
end
end
end
...
...
spec/rubocop/cop/usage_data/distinct_count_by_large_foreign_key_spec.rb
View file @
cdec4a94
...
...
@@ -21,11 +21,23 @@ RSpec.describe RuboCop::Cop::UsageData::DistinctCountByLargeForeignKey, type: :r
subject
(
:cop
)
{
described_class
.
new
(
config
)
}
context
'when counting by disallowed key'
do
it
'register an offence'
do
it
'register
s
an offence'
do
inspect_source
(
'distinct_count(Issue, :creator_id)'
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
it
'does not register an offence when batch is false'
do
inspect_source
(
'distinct_count(Issue, :creator_id, batch: false)'
)
expect
(
cop
.
offenses
).
to
be_empty
end
it
'register an offence when batch is true'
do
inspect_source
(
'distinct_count(Issue, :creator_id, batch: true)'
)
expect
(
cop
.
offenses
.
size
).
to
eq
(
1
)
end
end
context
'when calling by allowed key'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment