-
-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reject sequence definitions for Active Record primary keys #419
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module FactoryBotRails | ||
class FactoryValidator | ||
def initialize(validators = []) | ||
@validators = Array(validators) | ||
end | ||
|
||
def add_validator(validator) | ||
@validators << validator | ||
end | ||
|
||
def run | ||
ActiveSupport::Notifications.subscribe("factory_bot.compile_factory", &validate_compiled_factory) | ||
end | ||
|
||
private | ||
|
||
def validate_compiled_factory | ||
if Rails.version >= "6.0" | ||
rails_6_0_support | ||
else | ||
rails_5_2_support | ||
end | ||
end | ||
|
||
def rails_6_0_support | ||
proc do |event| | ||
@validators.each { |validator| validator.validate!(event.payload) } | ||
end | ||
end | ||
|
||
def rails_5_2_support | ||
proc do |*notification_event_arguments| | ||
event = ActiveSupport::Notifications::Event.new(*notification_event_arguments) | ||
|
||
rails_6_0_support.call(event) | ||
end | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
lib/factory_bot_rails/factory_validator/active_record_validator.rb
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,18 @@ | ||
module FactoryBotRails | ||
class FactoryValidator | ||
class ActiveRecordValidator | ||
def validate!(payload) | ||
attributes, for_class = payload.values_at(:attributes, :class) | ||
attributes.each do |attribute| | ||
if for_class < ActiveRecord::Base && for_class.primary_key == attribute.name.to_s | ||
raise FactoryBot::AttributeDefinitionError, <<~ERROR | ||
Attribute generates #{for_class.primary_key.inspect} primary key for #{for_class.name}" | ||
|
||
Do not define #{for_class.primary_key.inspect}. Instead, rely on the database to generate it. | ||
ERROR | ||
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
24 changes: 24 additions & 0 deletions
24
spec/factory_bot_rails/factory_validator/active_record_validator_spec.rb
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
describe FactoryBotRails::FactoryValidator do | ||
before { FactoryBot.reload } | ||
|
||
describe "ActiveRecordValidator" do | ||
context "when defined with a class that descends from ActiveRecord::Base" do | ||
it "raises an error for a sequence generating its primary key" do | ||
define_model "Article", an_id: :integer do | ||
self.primary_key = :an_id | ||
end | ||
|
||
FactoryBot.define do | ||
factory :article do | ||
sequence(:an_id) | ||
end | ||
end | ||
|
||
expect { FactoryBot.create(:article) } | ||
.to raise_error(FactoryBot::AttributeDefinitionError) | ||
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,67 @@ | ||
require "active_record" | ||
|
||
module DefineConstantMacros | ||
def define_class(path, base = Object, &block) | ||
const = stub_const(path, Class.new(base)) | ||
const.class_eval(&block) if block | ||
const | ||
end | ||
|
||
def define_model(name, columns = {}, &block) | ||
model = define_class(name, ActiveRecord::Base, &block) | ||
create_table(model.table_name) do |table| | ||
columns.each do |column_name, type| | ||
table.column column_name, type | ||
end | ||
end | ||
model | ||
end | ||
|
||
def create_table(table_name, &block) | ||
connection = ActiveRecord::Base.connection | ||
|
||
begin | ||
connection.execute("DROP TABLE IF EXISTS #{table_name}") | ||
connection.create_table(table_name, &block) | ||
created_tables << table_name | ||
connection | ||
rescue Exception => e # rubocop:disable Lint/RescueException | ||
connection.execute("DROP TABLE IF EXISTS #{table_name}") | ||
raise e | ||
end | ||
end | ||
|
||
def clear_generated_tables | ||
created_tables.each do |table_name| | ||
clear_generated_table(table_name) | ||
end | ||
created_tables.clear | ||
end | ||
|
||
def clear_generated_table(table_name) | ||
ActiveRecord::Base | ||
.connection | ||
.execute("DROP TABLE IF EXISTS #{table_name}") | ||
end | ||
|
||
private | ||
|
||
def created_tables | ||
@created_tables ||= [] | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include DefineConstantMacros | ||
|
||
config.before(:all) do | ||
ActiveRecord::Base.establish_connection( | ||
adapter: "sqlite3", | ||
database: ":memory:" | ||
) | ||
end | ||
|
||
config.after do | ||
clear_generated_tables | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding these.