From 7a35e945ef887a94ac873e64edfe6d712f290c84 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Sun, 3 Nov 2024 15:37:22 +0200 Subject: [PATCH] Fix Rubocop warnings --- lib/dynamoid/transaction_write.rb | 19 ++-- lib/dynamoid/transaction_write/create.rb | 2 +- .../delete_with_primary_key.rb | 3 +- lib/dynamoid/transaction_write/destroy.rb | 5 +- lib/dynamoid/transaction_write/save.rb | 16 ++-- .../transaction_write/update_fields.rb | 3 +- lib/dynamoid/transaction_write/upsert.rb | 7 +- spec/dynamoid/persistence_spec.rb | 24 ++--- .../dynamoid/transaction_write/commit_spec.rb | 2 +- .../dynamoid/transaction_write/create_spec.rb | 67 +++++++------- .../dynamoid/transaction_write/delete_spec.rb | 18 ++-- .../transaction_write/destroy_spec.rb | 31 ++++--- spec/dynamoid/transaction_write/save_spec.rb | 92 ++++++++++--------- .../update_attributes_spec.rb | 47 ++++------ .../transaction_write/update_fields_spec.rb | 2 +- .../dynamoid/transaction_write/upsert_spec.rb | 6 +- spec/dynamoid/type_casting_spec.rb | 2 +- spec/support/scratch_pad.rb | 1 - 18 files changed, 165 insertions(+), 182 deletions(-) diff --git a/lib/dynamoid/transaction_write.rb b/lib/dynamoid/transaction_write.rb index ccff937c..4de4a61a 100644 --- a/lib/dynamoid/transaction_write.rb +++ b/lib/dynamoid/transaction_write.rb @@ -14,7 +14,7 @@ class TransactionWrite attr_reader :actions def self.execute - transaction = self.new + transaction = new yield transaction transaction.commit end @@ -68,12 +68,12 @@ def create(model_class, attributes = {}, &block) end end - def upsert(model_class, hash_key, range_key = nil, attributes) + def upsert(model_class, hash_key, range_key = nil, attributes) # rubocop:disable Style/OptionalArguments action = Dynamoid::TransactionWrite::Upsert.new(model_class, hash_key, range_key, attributes) register_action action end - def update_fields(model_class, hash_key, range_key = nil, attributes) + def update_fields(model_class, hash_key, range_key = nil, attributes) # rubocop:disable Style/OptionalArguments action = Dynamoid::TransactionWrite::UpdateFields.new(model_class, hash_key, range_key, attributes) register_action action end @@ -89,13 +89,12 @@ def update_attributes!(model, attributes) end def delete(model_or_model_class, hash_key = nil, range_key = nil) - if model_or_model_class.is_a? Class - action = Dynamoid::TransactionWrite::DeleteWithPrimaryKey.new(model_or_model_class, hash_key, range_key) - register_action action - else - action = Dynamoid::TransactionWrite::DeleteWithInstance.new(model_or_model_class) - register_action action - end + action = if model_or_model_class.is_a? Class + Dynamoid::TransactionWrite::DeleteWithPrimaryKey.new(model_or_model_class, hash_key, range_key) + else + Dynamoid::TransactionWrite::DeleteWithInstance.new(model_or_model_class) + end + register_action action end def destroy!(model) diff --git a/lib/dynamoid/transaction_write/create.rb b/lib/dynamoid/transaction_write/create.rb index f05e4a01..d4838a2f 100644 --- a/lib/dynamoid/transaction_write/create.rb +++ b/lib/dynamoid/transaction_write/create.rb @@ -7,7 +7,7 @@ def initialize(model_class, attributes = {}, **options, &block) @model = model_class.new(attributes) if block - block.call(@model) + yield(@model) end @save_action = Save.new(@model, **options) diff --git a/lib/dynamoid/transaction_write/delete_with_primary_key.rb b/lib/dynamoid/transaction_write/delete_with_primary_key.rb index 56d83d51..a7e72e99 100644 --- a/lib/dynamoid/transaction_write/delete_with_primary_key.rb +++ b/lib/dynamoid/transaction_write/delete_with_primary_key.rb @@ -13,8 +13,7 @@ def on_registration validate_primary_key! end - def on_completing - end + def on_completing; end def aborted? false diff --git a/lib/dynamoid/transaction_write/destroy.rb b/lib/dynamoid/transaction_write/destroy.rb index e3bc4edf..69e26989 100644 --- a/lib/dynamoid/transaction_write/destroy.rb +++ b/lib/dynamoid/transaction_write/destroy.rb @@ -26,6 +26,7 @@ def on_registration def on_completing return if @aborted + @model.destroyed = true end @@ -39,19 +40,19 @@ def skip? def observable_by_user_result return false if @aborted + @model end def action_request key = { @model_class.hash_key => @model.hash_key } key[@model_class.range_key] = @model.range_value if @model_class.range_key? - result = { + { delete: { key: key, table_name: @model_class.table_name } } - result end private diff --git a/lib/dynamoid/transaction_write/save.rb b/lib/dynamoid/transaction_write/save.rb index c5e1658e..b5ee221b 100644 --- a/lib/dynamoid/transaction_write/save.rb +++ b/lib/dynamoid/transaction_write/save.rb @@ -16,14 +16,12 @@ def initialize(model, **options) def on_registration validate_model! - unless @options[:validate] == false - unless @valid = @model.valid? - if @options[:raise_error] - raise Dynamoid::Errors::DocumentNotValid, @model - else - @aborted = true - return - end + if @options[:validate] != false && !(@valid = @model.valid?) + if @options[:raise_error] + raise Dynamoid::Errors::DocumentNotValid, @model + else + @aborted = true + return end end @@ -143,8 +141,6 @@ def action_request_to_update result end - private - def touch_model_timestamps(skip_created_at:) return unless @model_class.timestamps_enabled? diff --git a/lib/dynamoid/transaction_write/update_fields.rb b/lib/dynamoid/transaction_write/update_fields.rb index 8505d823..c25295ce 100644 --- a/lib/dynamoid/transaction_write/update_fields.rb +++ b/lib/dynamoid/transaction_write/update_fields.rb @@ -14,8 +14,7 @@ def on_registration validate_primary_key! end - def on_completing - end + def on_completing; end def aborted? false diff --git a/lib/dynamoid/transaction_write/upsert.rb b/lib/dynamoid/transaction_write/upsert.rb index a4a00c6f..acdb9724 100644 --- a/lib/dynamoid/transaction_write/upsert.rb +++ b/lib/dynamoid/transaction_write/upsert.rb @@ -14,8 +14,7 @@ def on_registration validate_primary_key! end - def on_completing - end + def on_completing; end def aborted? false @@ -54,7 +53,7 @@ def action_request # expression_attribute_names = attribute_keys_in_model.map{|k| ["##{k}","#{k}"]}.to_h expression_attribute_names.merge!(item_keys.each_with_index.map { |k, i| ["#_n#{i}", k.to_s] }.to_h) - result = { + { update: { key: key, table_name: @model_class.table_name, @@ -63,8 +62,6 @@ def action_request expression_attribute_values: expression_attribute_values } } - - result end private diff --git a/spec/dynamoid/persistence_spec.rb b/spec/dynamoid/persistence_spec.rb index 29a8ac1e..58356a57 100644 --- a/spec/dynamoid/persistence_spec.rb +++ b/spec/dynamoid/persistence_spec.rb @@ -871,17 +871,17 @@ def around_save_callback end klass_with_callbacks.create! - expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation - 'run before_validation', - 'run after_validation', - 'run before_save', - 'start around_save', - 'run before_create', - 'start around_create', - 'finish around_create', - 'run after_create', - 'finish around_save', - 'run after_save' + expect(ScratchPad.recorded).to eql [ + 'run before_validation', + 'run after_validation', + 'run before_save', + 'start around_save', + 'run before_create', + 'start around_create', + 'finish around_create', + 'run after_create', + 'finish around_save', + 'run after_save' ] end end @@ -2010,7 +2010,7 @@ def around_update_callback }.to('[Updated]') end - # TODO: + # TODO: implement the test later # it 'raises ...Error when range key is missing' # TODO: add this case for save/save! and update_attributes/other update operations diff --git a/spec/dynamoid/transaction_write/commit_spec.rb b/spec/dynamoid/transaction_write/commit_spec.rb index 8b6bcf52..bc6948d2 100644 --- a/spec/dynamoid/transaction_write/commit_spec.rb +++ b/spec/dynamoid/transaction_write/commit_spec.rb @@ -15,6 +15,6 @@ transaction.create klass transaction.create klass - expect { transaction.commit }.to change { klass.count }.by(2) + expect { transaction.commit }.to change(klass, :count).by(2) end end diff --git a/spec/dynamoid/transaction_write/create_spec.rb b/spec/dynamoid/transaction_write/create_spec.rb index 26975fde..bf359fa0 100644 --- a/spec/dynamoid/transaction_write/create_spec.rb +++ b/spec/dynamoid/transaction_write/create_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Dynamoid::TransactionWrite, '.create' do +describe Dynamoid::TransactionWrite, '.create' do # rubocop:disable RSpec/MultipleDescribes let(:klass) do new_class do field :name @@ -57,7 +57,7 @@ def around_save_callback described_class.execute do |txn| txn.create klass, name: 'Alex' end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) obj = klass.last expect(obj.name).to eql 'Alex' @@ -84,7 +84,7 @@ def around_save_callback described_class.execute do |txn| result = txn.create klass, [{ name: 'Alex' }, { name: 'Michael' }] end - }.to change { klass.count }.by(2) + }.to change(klass, :count).by(2) expect(klass.pluck(:name)).to contain_exactly('Alex', 'Michael') expect(result.size).to eq 2 @@ -100,7 +100,7 @@ def around_save_callback described_class.execute do |txn| obj = txn.create klass, { name: 'Alex' } do |u| - u.name += " [Updated]" + u.name += ' [Updated]' end end @@ -113,7 +113,7 @@ def around_save_callback described_class.execute do |txn| objects = txn.create klass, [{ name: 'Alex' }, { name: 'Michael' }] do |u| - u.name += " [Updated]" + u.name += ' [Updated]' end end @@ -121,7 +121,7 @@ def around_save_callback end end - # TODO: + # TODO: implement the test later it 'can be called without attributes to modify' describe 'primary key schema' do @@ -133,7 +133,7 @@ def around_save_callback described_class.execute do |txn| txn.create klass, name: 'Alex' end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) end end @@ -145,7 +145,7 @@ def around_save_callback described_class.execute do |txn| txn.create klass_with_composite_key, name: 'Alex', age: 3 end - }.to change { klass_with_composite_key.count }.by(1) + }.to change(klass_with_composite_key, :count).by(1) end end end @@ -226,7 +226,7 @@ def around_save_callback described_class.execute do |txn| obj = txn.create(klass_with_validation, name: 'oneone') end - }.to change { klass_with_validation.count }.by(1) + }.to change(klass_with_validation, :count).by(1) expect(obj).to be_persisted expect(obj).not_to be_changed @@ -239,7 +239,7 @@ def around_save_callback described_class.execute do |txn| obj = txn.create(klass_with_validation, name: 'one') end - }.not_to change { klass_with_validation.count } + }.not_to change(klass_with_validation, :count) expect(obj).not_to be_persisted expect(obj).to be_changed @@ -253,7 +253,7 @@ def around_save_callback expect(obj).to be_a(klass_with_validation) expect(obj.name).to eql 'one' - expect(obj).to_not be_persisted + expect(obj).not_to be_persisted end it 'does not roll back the whole transaction when a model to be created is invalid' do @@ -265,9 +265,9 @@ def around_save_callback obj_invalid = txn.create(klass_with_validation, name: 'one') obj_valid = txn.create(klass_with_validation, name: 'twotwo') end - }.to change { klass_with_validation.count }.by(1) + }.to change(klass_with_validation, :count).by(1) - expect(obj_invalid).to_not be_persisted + expect(obj_invalid).not_to be_persisted expect(obj_valid).to be_persisted obj_valid_loaded = klass_with_validation.find(obj_valid.id) @@ -348,9 +348,9 @@ def around_save_callback expect { described_class.execute do |txn| obj = txn.create klass, name: 'Alex' - raise "trigger rollback" + raise 'trigger rollback' end - }.to raise_error("trigger rollback") + }.to raise_error('trigger rollback') expect(obj).not_to be_persisted expect(obj).to be_changed @@ -485,17 +485,17 @@ def around_save_callback txn.create klass_with_all_callbacks end - expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation - 'run before_validation', - 'run after_validation', - 'run before_save', - 'start around_save', - 'run before_create', - 'start around_create', - 'finish around_create', - 'run after_create', - 'finish around_save', - 'run after_save' + expect(ScratchPad.recorded).to eql [ + 'run before_validation', + 'run after_validation', + 'run before_save', + 'start around_save', + 'run before_create', + 'start around_create', + 'finish around_create', + 'run after_create', + 'finish around_save', + 'run after_save' ] end @@ -520,7 +520,8 @@ def around_save_callback 'finish around_create', 'run after_create', 'finish around_save', - 'run after_save') + 'run after_save' + ) expect(ScratchPad.recorded).to eql [] end end @@ -547,7 +548,7 @@ def around_save_callback described_class.execute do |txn| txn.create! klass, name: 'Alex' end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) obj = klass.last expect(obj.name).to eql 'Alex' @@ -574,7 +575,7 @@ def around_save_callback described_class.execute do |txn| result = txn.create! klass, [{ name: 'Alex' }, { name: 'Michael' }] end - }.to change { klass.count }.by(2) + }.to change(klass, :count).by(2) expect(klass.pluck(:name)).to contain_exactly('Alex', 'Michael') expect(result.size).to eq 2 @@ -590,7 +591,7 @@ def around_save_callback described_class.execute do |txn| obj = txn.create! klass, { name: 'Alex' } do |u| - u.name += " [Updated]" + u.name += ' [Updated]' end end @@ -603,7 +604,7 @@ def around_save_callback described_class.execute do |txn| objects = txn.create! klass, [{ name: 'Alex' }, { name: 'Michael' }] do |u| - u.name += " [Updated]" + u.name += ' [Updated]' end end @@ -623,7 +624,7 @@ def around_save_callback described_class.execute do |txn| obj = txn.create!(klass_with_validation, name: 'oneone') end - }.to change { klass_with_validation.count }.by(1) + }.to change(klass_with_validation, :count).by(1) expect(obj).to be_persisted expect(obj).not_to be_changed @@ -645,7 +646,7 @@ def around_save_callback txn.create!(klass_with_validation, name: 'one') end }.to raise_error(Dynamoid::Errors::DocumentNotValid) - }.to_not change { klass_with_validation.count } + }.not_to change(klass_with_validation, :count) end end diff --git a/spec/dynamoid/transaction_write/delete_spec.rb b/spec/dynamoid/transaction_write/delete_spec.rb index 9c6858d0..83ddd1d5 100644 --- a/spec/dynamoid/transaction_write/delete_spec.rb +++ b/spec/dynamoid/transaction_write/delete_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Dynamoid::TransactionWrite, '#delete(model)' do +describe Dynamoid::TransactionWrite, '#delete(model)' do # rubocop:disable RSpec/MultipleDescribes let(:klass) do new_class do field :name @@ -23,7 +23,7 @@ described_class.execute do |txn| txn.delete obj end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) expect(klass.exists?(obj.id)).to eql false expect(obj).to be_destroyed @@ -49,7 +49,7 @@ described_class.execute do |txn| txn.delete obj end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) end end @@ -61,7 +61,7 @@ described_class.execute do |txn| txn.delete obj end - }.to change { klass_with_composite_key.count }.by(-1) + }.to change(klass_with_composite_key, :count).by(-1) end end end @@ -130,9 +130,9 @@ expect { described_class.execute do |txn| txn.delete obj - raise "trigger rollback" + raise 'trigger rollback' end - }.to raise_error("trigger rollback") + }.to raise_error('trigger rollback') expect(obj).not_to be_destroyed end @@ -208,7 +208,7 @@ def around_destroy_callback described_class.execute do |txn| txn.delete klass, obj.id end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) expect(klass.exists?(obj.id)).to eql false end @@ -233,7 +233,7 @@ def around_destroy_callback described_class.execute do |txn| txn.delete klass, obj.id end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) end end @@ -245,7 +245,7 @@ def around_destroy_callback described_class.execute do |txn| txn.delete klass_with_composite_key, obj.id, obj.age end - }.to change { klass_with_composite_key.count }.by(-1) + }.to change(klass_with_composite_key, :count).by(-1) end it 'raises MissingHashKey if partition key is not specified' do diff --git a/spec/dynamoid/transaction_write/destroy_spec.rb b/spec/dynamoid/transaction_write/destroy_spec.rb index 256f4e44..fa0147dc 100644 --- a/spec/dynamoid/transaction_write/destroy_spec.rb +++ b/spec/dynamoid/transaction_write/destroy_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Dynamoid::TransactionWrite, '#destroy' do +describe Dynamoid::TransactionWrite, '#destroy' do # rubocop:disable RSpec/MultipleDescribes let(:klass) do new_class do field :name @@ -37,7 +37,7 @@ def around_destroy_callback described_class.execute do |txn| txn.destroy obj end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) expect(klass.exists?(obj.id)).to eql false expect(obj).to be_destroyed @@ -63,7 +63,7 @@ def around_destroy_callback described_class.execute do |txn| txn.destroy obj end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) end end @@ -75,7 +75,7 @@ def around_destroy_callback described_class.execute do |txn| txn.destroy obj end - }.to change { klass_with_composite_key.count }.by(-1) + }.to change(klass_with_composite_key, :count).by(-1) end end end @@ -130,7 +130,7 @@ def around_destroy_callback txn.destroy obj_to_destroy txn.save obj_to_save end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) expect(obj_to_destroy).to be_destroyed expect(obj_to_save).to be_persisted @@ -190,9 +190,9 @@ def around_destroy_callback expect { described_class.execute do |txn| txn.destroy obj - raise "trigger rollback" + raise 'trigger rollback' end - }.to raise_error("trigger rollback") + }.to raise_error('trigger rollback') expect(obj).not_to be_destroyed end @@ -256,11 +256,11 @@ def around_destroy_callback txn.destroy obj end - expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation - 'run before_destroy', - 'start around_destroy', - 'finish around_destroy', - 'run after_destroy' + expect(ScratchPad.recorded).to eql [ + 'run before_destroy', + 'start around_destroy', + 'finish around_destroy', + 'run after_destroy' ] end @@ -280,7 +280,8 @@ def around_destroy_callback 'run before_destroy', 'start around_destroy', 'finish around_destroy', - 'run after_destroy') + 'run after_destroy' + ) expect(ScratchPad.recorded).to eql [] end @@ -318,7 +319,7 @@ def around_destroy_callback described_class.execute do |txn| txn.destroy! obj end - }.to change { klass.count }.by(-1) + }.to change(klass, :count).by(-1) expect(klass.exists?(obj.id)).to eql false end @@ -367,7 +368,7 @@ def around_destroy_callback txn.destroy! obj_to_destroy txn.save obj_to_save end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) expect(obj_to_destroy).to be_destroyed expect(obj_to_save).to be_persisted diff --git a/spec/dynamoid/transaction_write/save_spec.rb b/spec/dynamoid/transaction_write/save_spec.rb index 15138abd..a85aff1b 100644 --- a/spec/dynamoid/transaction_write/save_spec.rb +++ b/spec/dynamoid/transaction_write/save_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Dynamoid::TransactionWrite, '.save' do +describe Dynamoid::TransactionWrite, '.save' do # rubocop:disable RSpec/MultipleDescribes let(:klass) do new_class do field :name @@ -70,7 +70,7 @@ def around_save_callback described_class.execute do |txn| txn.save obj end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) obj_loaded = klass.find(obj.id) expect(obj_loaded.name).to eql 'Alex' @@ -115,7 +115,7 @@ def around_save_callback described_class.execute do |txn| txn.save obj end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) end it 'persists changes in already persisted model' do @@ -139,7 +139,7 @@ def around_save_callback described_class.execute do |txn| txn.save obj end - }.to change { klass_with_composite_key.count }.by(1) + }.to change(klass_with_composite_key, :count).by(1) end it 'persists changes in already persisted model' do @@ -383,7 +383,7 @@ def around_save_callback described_class.execute do |txn| txn.save obj end - }.not_to change { klass_with_validation.count } + }.not_to change(klass_with_validation, :count) expect(obj).not_to be_persisted expect(obj).to be_changed @@ -398,7 +398,7 @@ def around_save_callback described_class.execute do |txn| txn.save obj, validate: false end - }.to change { klass_with_validation.count }.by(1) + }.to change(klass_with_validation, :count).by(1) obj_loaded = klass_with_validation.find(obj.id) expect(obj_loaded.name).to eql 'one' @@ -444,7 +444,7 @@ def around_save_callback txn.save obj_invalid txn.save obj_valid end - }.to change { klass_with_validation.count }.by(1) + }.to change(klass_with_validation, :count).by(1) expect(klass_with_validation.all.map(&:id)).to contain_exactly(obj_valid.id) end @@ -476,7 +476,7 @@ def around_save_callback described_class.execute do |txn| txn.save obj end - }.not_to change { klass_with_validation.count } + }.not_to change(klass_with_validation, :count) expect(obj).to be_persisted expect(obj).to be_changed @@ -680,11 +680,11 @@ def around_save_callback obj_to_create = nil expect { - described_class.execute do |txn| - txn.save obj_deleted - obj_to_create = txn.create klass, name: 'two' - end - }.to change { klass.count }.by(2) + described_class.execute do |txn| + txn.save obj_deleted + obj_to_create = txn.create klass, name: 'two' + end + }.to change(klass, :count).by(2) expect(obj_to_create).to be_persisted expect(obj_to_create).not_to be_changed @@ -696,9 +696,9 @@ def around_save_callback expect { described_class.execute do |txn| txn.save obj - raise "trigger rollback" + raise 'trigger rollback' end - }.to raise_error("trigger rollback") + }.to raise_error('trigger rollback') expect(obj).not_to be_persisted expect(obj).to be_changed @@ -711,9 +711,9 @@ def around_save_callback expect { described_class.execute do |txn| txn.save obj - raise "trigger rollback" + raise 'trigger rollback' end - }.to raise_error("trigger rollback") + }.to raise_error('trigger rollback') expect(obj).to be_changed end @@ -867,17 +867,17 @@ def around_create_callback txn.save obj end - expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation - 'run before_validation', - 'run after_validation', - 'run before_save', - 'start around_save', - 'run before_create', - 'start around_create', - 'finish around_create', - 'run after_create', - 'finish around_save', - 'run after_save' + expect(ScratchPad.recorded).to eql [ + 'run before_validation', + 'run after_validation', + 'run before_save', + 'start around_save', + 'run before_create', + 'start around_create', + 'finish around_create', + 'run after_create', + 'finish around_save', + 'run after_save' ] end @@ -903,7 +903,8 @@ def around_create_callback 'finish around_create', 'run after_create', 'finish around_save', - 'run after_save') + 'run after_save' + ) expect(ScratchPad.recorded).to eql [] end @@ -1098,17 +1099,17 @@ def around_update_callback txn.save obj end - expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation - 'run before_validation', - 'run after_validation', - 'run before_save', - 'start around_save', - 'run before_update', - 'start around_update', - 'finish around_update', - 'run after_update', - 'finish around_save', - 'run after_save' + expect(ScratchPad.recorded).to eql [ + 'run before_validation', + 'run after_validation', + 'run before_save', + 'start around_save', + 'run before_update', + 'start around_update', + 'finish around_update', + 'run after_update', + 'finish around_save', + 'run after_save' ] end @@ -1135,7 +1136,8 @@ def around_update_callback 'finish around_update', 'run after_update', 'finish around_save', - 'run after_save') + 'run after_save' + ) expect(ScratchPad.recorded).to eql [] end end @@ -1164,7 +1166,7 @@ def around_update_callback described_class.execute do |txn| txn.save! obj end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) obj_loaded = klass.find(obj.id) expect(obj_loaded.name).to eql 'Alex' @@ -1221,7 +1223,7 @@ def around_update_callback described_class.execute do |txn| txn.save! obj, validate: false end - }.to change { klass_with_validation.count }.by(1) + }.to change(klass_with_validation, :count).by(1) obj_loaded = klass_with_validation.find(obj.id) expect(obj_loaded.name).to eql 'one' @@ -1244,7 +1246,7 @@ def around_update_callback txn.save! obj_invalid end }.to raise_error(Dynamoid::Errors::DocumentNotValid) - }.not_to change { klass_with_validation.count } + }.not_to change(klass_with_validation, :count) end it 'rolls back the whole transaction when a model to be saved is invalid' do @@ -1393,7 +1395,7 @@ def around_update_callback obj_to_create = txn.create klass, name: 'Michael' end }.to raise_error(Aws::DynamoDB::Errors::TransactionCanceledException) - }.not_to change { klass.count } + }.not_to change(klass, :count) expect(klass.count).to eql 1 expect(klass.all.to_a).to eql [existing] diff --git a/spec/dynamoid/transaction_write/update_attributes_spec.rb b/spec/dynamoid/transaction_write/update_attributes_spec.rb index 5687300c..a4b2f040 100644 --- a/spec/dynamoid/transaction_write/update_attributes_spec.rb +++ b/spec/dynamoid/transaction_write/update_attributes_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Dynamoid::TransactionWrite, '#update_attributes' do +describe Dynamoid::TransactionWrite, '#update_attributes' do # rubocop:disable RSpec/MultipleDescribes let(:klass) do new_class do field :name @@ -64,10 +64,10 @@ def around_save_callback expect(obj.changed?).to eql false end - # TODO: + # TODO: implement the test later it 'raises exception if a model is not persisted' - # TODO: + # TODO: implement the test later it 'can be called without attributes to modify' describe 'primary key schema' do @@ -308,7 +308,7 @@ def around_save_callback txn.update_attributes obj_deleted, name: 'Alex [Updated]' obj_to_create = txn.create klass, name: 'Michael' end - }.to change { klass.count }.by(2) + }.to change(klass, :count).by(2) expect(obj_to_create).to be_persisted expect(obj_deleted).to be_persisted @@ -322,9 +322,9 @@ def around_save_callback expect { described_class.execute do |txn| txn.update_attributes obj, name: 'Alex [Updated]' - raise "trigger rollback" + raise 'trigger rollback' end - }.to raise_error("trigger rollback") + }.to raise_error('trigger rollback') expect(obj).to be_changed end @@ -474,17 +474,17 @@ def around_update_callback txn.update_attributes obj, name: 'Bob' end - expect(ScratchPad.recorded).to eql [ # rubocop:disable Style/StringConcatenation - 'run before_validation', - 'run after_validation', - 'run before_save', - 'start around_save', - 'run before_update', - 'start around_update', - 'finish around_update', - 'run after_update', - 'finish around_save', - 'run after_save' + expect(ScratchPad.recorded).to eql [ + 'run before_validation', + 'run after_validation', + 'run before_save', + 'start around_save', + 'run before_update', + 'start around_update', + 'finish around_update', + 'run after_update', + 'finish around_save', + 'run after_save' ] end @@ -592,17 +592,6 @@ def around_update_callback expect(result).to eql true end - it 'returns true when model valid' do - obj = klass_with_validation.create!(name: 'oneone') - - result = nil - described_class.execute do |txn| - result = txn.update_attributes!(obj, name: 'oneone [Updated]') - end - - expect(result).to eql true - end - it 'raise DocumentNotValid when model invalid' do obj = klass_with_validation.create!(name: 'oneone') @@ -686,7 +675,7 @@ def around_update_callback txn.update_attributes! obj_deleted, name: 'Alex [Updated]' obj_to_create = txn.create klass, name: 'Michael' end - }.to change { klass.count }.by(2) + }.to change(klass, :count).by(2) expect(obj_to_create).to be_persisted expect(obj_deleted).to be_persisted diff --git a/spec/dynamoid/transaction_write/update_fields_spec.rb b/spec/dynamoid/transaction_write/update_fields_spec.rb index 75afcb0d..96289074 100644 --- a/spec/dynamoid/transaction_write/update_fields_spec.rb +++ b/spec/dynamoid/transaction_write/update_fields_spec.rb @@ -28,7 +28,7 @@ expect(obj).not_to be_changed end - # TODO: + # TODO: implement the test later it 'can be called without attributes to modify' it 'returns nil' do diff --git a/spec/dynamoid/transaction_write/upsert_spec.rb b/spec/dynamoid/transaction_write/upsert_spec.rb index d968323d..88290301 100644 --- a/spec/dynamoid/transaction_write/upsert_spec.rb +++ b/spec/dynamoid/transaction_write/upsert_spec.rb @@ -26,7 +26,7 @@ described_class.execute do |txn| txn.upsert klass, id_new, name: 'Alex' end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) obj = klass.find(id_new) expect(obj.name).to eql 'Alex' @@ -74,7 +74,7 @@ described_class.execute do |txn| txn.upsert klass, id_new, {} end - }.to change { klass.count }.by(1) + }.to change(klass, :count).by(1) end it 'persists changes in already persisted model' do @@ -97,7 +97,7 @@ described_class.execute do |txn| txn.upsert klass_with_composite_key, id_new, 3, {} end - }.to change { klass_with_composite_key.count }.by(1) + }.to change(klass_with_composite_key, :count).by(1) end it 'persists changes in already persisted model' do diff --git a/spec/dynamoid/type_casting_spec.rb b/spec/dynamoid/type_casting_spec.rb index 38b91a82..9abb4964 100644 --- a/spec/dynamoid/type_casting_spec.rb +++ b/spec/dynamoid/type_casting_spec.rb @@ -274,7 +274,7 @@ obj = klass.new(values: Set.new([1, 1.5, '2'.to_d])) - expect(obj.values).to eql(Set.new([1, 1, 2])) + expect(obj.values).to eql(Set.new([1, 2])) end it 'type casts numbers' do diff --git a/spec/support/scratch_pad.rb b/spec/support/scratch_pad.rb index 5c73f318..cb2ca516 100644 --- a/spec/support/scratch_pad.rb +++ b/spec/support/scratch_pad.rb @@ -35,4 +35,3 @@ def self.inspect "" end end -