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

Fix specs about upsert_keys and literal #71

Merged
merged 2 commits into from
Apr 25, 2018
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
2 changes: 1 addition & 1 deletion lib/active_record_upsert/arel/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Arel
module Crud
def compile_upsert(upsert_keys, upsert_options, upsert_values, insert_values, wheres)
# Support non-attribute key (like `md5(my_attribute)``)
target = self[upsert_keys.kind_of?(Hash) ? ::Arel::Nodes::SqlLiteral.new(upsert_keys[:literal]) : upsert_keys.join(',')]
target = self[upsert_options.key?(:literal) ? ::Arel::Nodes::SqlLiteral.new(upsert_options[:literal]) : upsert_keys.join(',')]
on_conflict_do_update = OnConflictDoUpdateManager.new

on_conflict_do_update.target = target
Expand Down
29 changes: 16 additions & 13 deletions spec/active_record/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module ActiveRecord
end
context 'different ways of setting keys' do
let(:attrs) { {make: 'Ford', name: 'Focus', long_field: SecureRandom.uuid} }
before { Vehicle.create(attrs) }
let!(:vehicule) { Vehicle.create(attrs) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it work with before?

Copy link
Contributor

Choose a reason for hiding this comment

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

The removed line before { Vehicle.create(attrs) } was useless, it should have been written before { @vehicle = Vehicle.create(attrs) }.

Changing let!(:vehicule) { Vehicle.create(attrs) } by let(:vehicule) { Vehicle.create(attrs) } recreate vehicle object on each tests. May be it's better for tests integrity...

Copy link
Collaborator

Choose a reason for hiding this comment

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

The current documentation on let and let!:

https://relishapp.com/rspec/rspec-core/v/3-7/docs/helper-methods/let-and-let

Let's keep the French wording in there. And use let!. In this case, we would like to recalculate before each test, so let! it is.

Copy link
Contributor

Choose a reason for hiding this comment

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

@olleolleolle you're right


it 'works with multiple symbol args' do
Vehicle.upsert_keys :make, :name
Expand All @@ -71,28 +71,31 @@ module ActiveRecord
expect(upserted.wheels_count).to eq(1)
end
it 'works with a single symbol' do
v = Vehicle.create
Vehicle.upsert_keys :id
upserted = Vehicle.new(id: v.id, wheels_count: 1)
upserted.upsert
upserted = Vehicle.new(id: vehicule.id, name: 'ford', wheels_count: 1)
result = upserted.upsert

expect(result).to be_truthy
expect(upserted.wheels_count).to eq(1)
expect(upserted.id).to eq(v.id)
expect(upserted.id).to eq(vehicule.id)
end
it 'works with a single string' do
v = Vehicle.create
Vehicle.upsert_keys 'id'
upserted = Vehicle.new(id: v.id, wheels_count: 1)
upserted.upsert
upserted = Vehicle.new(id: vehicule.id, name: 'ford', wheels_count: 1)
result = upserted.upsert

expect(result).to be_truthy
expect(upserted.wheels_count).to eq(1)
expect(upserted.id).to eq(v.id)
expect(upserted.id).to eq(vehicule.id)
end
it 'works with a literal' do
v = Vehicle.create
Vehicle.upsert_keys literal: 'md5(long_field)'
upserted = Vehicle.new(id: v.id, long_field: attrs[:long_field])
upserted.upsert
upserted = Vehicle.new(id: vehicule.id, name: 'ford', long_field: attrs[:long_field])
result = upserted.upsert

expect(result).to be_truthy
expect(upserted.long_field).to eq(attrs[:long_field])
expect(upserted.id).to eq(v.id)
expect(upserted.id).to eq(vehicule.id)
end
end

Expand Down