Skip to content
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

Raise error when trying to set missing attribute #45

Merged
merged 1 commit into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/rom/factory/builder/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(builder, relation = builder.relation)
# @api private
def create(*traits, **attrs)
tuple = tuple(*traits, attrs)
validate_keys(traits, tuple)
persisted = persist(tuple)

if tuple_evaluator.has_associations?(traits)
Expand All @@ -47,6 +48,15 @@ def persist(attrs)
def primary_key_names
relation.schema.primary_key.map(&:name)
end

# @api private
def validate_keys(traits, tuple)
schema_keys = relation.schema.attributes.map(&:name)
assoc_keys = tuple_evaluator.assoc_names(traits)
unknown_keys = tuple.keys - schema_keys - assoc_keys

raise UnknownFactoryAttributes, unknown_keys unless unknown_keys.empty?
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/rom/factory/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ def initialize(name)
super("Factory +#{name}+ not defined")
end
end

class UnknownFactoryAttributes < StandardError
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt like ROM::Factory::UnknownFactoryAttributes was a bit nicer for the user to see than Rom::Factory::Builder::Persistable::UnknownFactoryAttributes

def initialize(attrs)
super("Unknown attributes: #{attrs.join(', ')}")
end
end
end
end
15 changes: 14 additions & 1 deletion spec/integration/rom/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@

expect { define.() }.to raise_error(ArgumentError)
end

it 'raises error when trying to set missing attribute' do
factories.define(:user, relation: :users) do |f|
f.first_name 'Janis'
f.last_name 'Miezitis'
f.email 'janjiss@gmail.com'
f.timestamps
end

expect {
factories[:user, not_real_attribute: "invalid attribute value"]
}.to raise_error(ROM::Factory::UnknownFactoryAttributes)
end
end

context 'sequence' do
Expand Down Expand Up @@ -647,7 +660,7 @@
end

it 'allows overrides' do
user = factories[:user, name: "Joe"]
user = factories[:user, first_name: "Joe"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the exact kind of typo I would like to see an error for. I updated name to be first_name but we can just as easily make this be user = factories[:user]. I was not sure what the intent was for updating the name attribute considering there is no specific test that checks the update.

task = factories[:task, user: user]

expect(task.title).to eql('A task')
Expand Down