-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from owst/add_id_sequence_cop
Add ` FactoryBot/IdSequence` cop
- Loading branch information
Showing
7 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module FactoryBot | ||
# Do not create a FactoryBot sequence for an id column. | ||
# | ||
# @example | ||
# # bad - can lead to conflicts between FactoryBot and DB sequences | ||
# factory :foo do | ||
# sequence :id | ||
# end | ||
# | ||
# # good - a non-id column | ||
# factory :foo do | ||
# sequence :some_non_id_column | ||
# end | ||
# | ||
class IdSequence < ::RuboCop::Cop::Base | ||
extend AutoCorrector | ||
include RangeHelp | ||
|
||
MSG = 'Do not create a sequence for an id attribute' | ||
RESTRICT_ON_SEND = %i[sequence].freeze | ||
|
||
def on_send(node) | ||
return unless node.first_argument.value == :id | ||
|
||
add_offense(node) do |corrector| | ||
range_to_remove = range_by_whole_lines( | ||
node.source_range, | ||
include_final_newline: true | ||
) | ||
|
||
corrector.remove(range_to_remove) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::FactoryBot::IdSequence do | ||
it 'registers an offense with no block' do | ||
expect_offense(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
sequence :id | ||
^^^^^^^^^^^^ Do not create a sequence for an id attribute | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with a default value' do | ||
expect_offense(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
sequence(:id, 1000) | ||
^^^^^^^^^^^^^^^^^^^ Do not create a sequence for an id attribute | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense across multiple lines' do | ||
expect_offense(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
sequence( | ||
^^^^^^^^^ Do not create a sequence for an id attribute | ||
:id, | ||
1000 | ||
) | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense with a Enumerable of values' do | ||
expect_offense(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
sequence(:id, (1..10).cycle) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not create a sequence for an id attribute | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for a non-id sequence' do | ||
expect_no_offenses(<<~RUBY) | ||
FactoryBot.define do | ||
factory :post do | ||
summary { "A summary" } | ||
sequence :something_else | ||
title { "A title" } | ||
end | ||
end | ||
RUBY | ||
end | ||
end |