From 93d2a6a76fb24818fe3dd89f6dc26a5bfc006f32 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Wed, 13 Feb 2019 12:56:20 +0800 Subject: [PATCH] Refactoring: Migrate migration_ror_42_to50_store_test to RSpec --- .../active_record/store/indifferent_coder.rb | 2 +- ...3000001_fixed_store_upgrade_ror_45_spec.rb | 65 +++++++++++++++ test/unit/migration_ror_42_to50_store_test.rb | 81 ------------------- 3 files changed, 66 insertions(+), 82 deletions(-) create mode 100644 spec/db/migrate/20171023000001_fixed_store_upgrade_ror_45_spec.rb delete mode 100644 test/unit/migration_ror_42_to50_store_test.rb diff --git a/lib/core_ext/active_record/store/indifferent_coder.rb b/lib/core_ext/active_record/store/indifferent_coder.rb index 3e5ce3dd4..eacbd0a89 100644 --- a/lib/core_ext/active_record/store/indifferent_coder.rb +++ b/lib/core_ext/active_record/store/indifferent_coder.rb @@ -1,6 +1,6 @@ # Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores # we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters -# and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20170910000001_fixed_store_upgrade_45.rb. +# and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20171023000001_fixed_store_upgrade_ror_45.rb. require 'active_record/store' module ActiveRecord module Store diff --git a/spec/db/migrate/20171023000001_fixed_store_upgrade_ror_45_spec.rb b/spec/db/migrate/20171023000001_fixed_store_upgrade_ror_45_spec.rb new file mode 100644 index 000000000..18e7415f9 --- /dev/null +++ b/spec/db/migrate/20171023000001_fixed_store_upgrade_ror_45_spec.rb @@ -0,0 +1,65 @@ +require 'rails_helper' + +# Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores +# we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters +# and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20171023000001_fixed_store_upgrade_ror_45.rb. +RSpec.describe FixedStoreUpgradeRor45, type: :db_migration do + subject(:taskbar) { Taskbar.last } + + context 'when DB contains `store`d attributes saved as unpermitted ActionController::Parameters' do + before do + ActiveRecord::Base.connection.execute(<<~SQL.tap { |sql| sql.delete!('`') if !mysql? }) + INSERT INTO taskbars (`user_id`, `client_id`, `key`, `callback`, `state`, `params`, `prio`, `notify`, `active`, `preferences`, `last_contact`, `updated_at`, `created_at`) + VALUES (#{user.id}, + '123', + 'Ticket-123', + 'TicketZoom', + '#{state.to_yaml}', + '#{params.to_yaml}', + 1, + FALSE, + TRUE, + '#{preferences.to_yaml}', + '#{last_contact}', + '#{last_contact}', + '#{last_contact}') + SQL + end + + let(:mysql?) { ActiveRecord::Base.connection_config[:adapter] == 'mysql2' } + let(:user) { User.last } + let(:last_contact) { '2017-09-01 10:10:00' } + let(:state) { ActionController::Parameters.new('ticket' => {}, 'article' => {}) } + let(:params) { ActionController::Parameters.new('ticket_id' => 1234, 'shown' => true) } + let(:preferences) do + ActionController::Parameters.new( + 'tasks' => [ + { + 'id' => 99_282, + 'user_id' => 85_370, + 'last_contact' => 1.week.after(Time.zone.parse(last_contact)), + 'changed' => true + } + ] + ) + end + + it 'converts `store`d attributes to ActiveSupport::HashWithIndifferentAccess, preserving original values' do + expect { migrate } + .to change { taskbar.reload.read_attribute_before_type_cast(:state) } + .and not_change { taskbar.reload.state } + .and change { taskbar.reload.read_attribute_before_type_cast(:params) } + .and not_change { taskbar.reload.params } + .and change { taskbar.reload.read_attribute_before_type_cast(:preferences) } + + expect(taskbar.read_attribute_before_type_cast(:state)) + .to start_with('--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess') + + expect(taskbar.read_attribute_before_type_cast(:params)) + .to start_with('--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess') + + expect(taskbar.read_attribute_before_type_cast(:preferences)) + .to start_with('--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess') + end + end +end diff --git a/test/unit/migration_ror_42_to50_store_test.rb b/test/unit/migration_ror_42_to50_store_test.rb deleted file mode 100644 index 449c383db..000000000 --- a/test/unit/migration_ror_42_to50_store_test.rb +++ /dev/null @@ -1,81 +0,0 @@ -# Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores -# we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters -# and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20170910000001_fixed_store_upgrade_45.rb. - -require 'test_helper' - -class MigrationRor42To50StoreTest < ActiveSupport::TestCase - test 'store with ActionController::Parameters object - get ActiveSupport::HashWithIndifferentAccess' do - - user = User.last - last_contact = '2017-09-01 10:10:00' - state = "--- !ruby/hash-with-ivars:ActionController::Parameters -elements: - ticket: !ruby/hash-with-ivars:ActionController::Parameters - elements: {} - ivars: - :@permitted: false - article: !ruby/hash-with-ivars:ActionController::Parameters - elements: {} - ivars: - :@permitted: false -ivars: - :@permitted: false -" - params = "--- !ruby/hash-with-ivars:ActionController::Parameters -elements: - ticket_id: 1234 - shown: true -ivars: - :@permitted: false -" - preferences = "--- !ruby/hash-with-ivars:ActionController::Parameters -elements: - tasks: &1 - - !ruby/hash-with-ivars:ActionController::Parameters - elements: - id: 99282 - user_id: 85370 - last_contact: 2017-09-08 11:28:00.289663000 Z - changed: true - ivars: - :@permitted: false -ivars: - :@permitted: false - :@converted_arrays: !ruby/object:Set - hash: - *1: true -" - sql = "INSERT INTO taskbars (`user_id`, `client_id`, `key`, `callback`, `state`, `params`, `prio`, `notify`, `active`, `preferences`, `last_contact`, `updated_at`, `created_at`) VALUES (#{user.id}, '123', 'Ticket-123', 'TicketZoom', '#{state}', '#{params}', 1, FALSE, TRUE, '#{preferences}', '#{last_contact}', '#{last_contact}', '#{last_contact}')" - if ActiveRecord::Base.connection_config[:adapter] != 'mysql2' - sql.delete!('`') - end - records_array = ActiveRecord::Base.connection.execute(sql) - - taskbar = Taskbar.last - assert(taskbar) - assert(taskbar.params) - assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.params.class) - assert_equal(1234, taskbar.params[:ticket_id]) - assert(taskbar.state) - assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.state.class) - assert(taskbar.state[:ticket].blank?) - assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:ticket].class) - assert(taskbar.state[:article].blank?) - assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:article].class) - - taskbar.save! - taskbar.reload - assert(taskbar) - assert(taskbar.params) - assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.params.class) - assert_equal(1234, taskbar.params[:ticket_id]) - assert(taskbar.state) - assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.state.class) - assert(taskbar.state[:ticket].blank?) - assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:ticket].class) - assert(taskbar.state[:article].blank?) - assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:article].class) - end - -end