Browse Source

Refactoring: Migrate migration_ror_42_to50_store_test to RSpec

Ryan Lue 6 years ago
parent
commit
93d2a6a76f

+ 1 - 1
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

+ 65 - 0
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

+ 0 - 81
test/unit/migration_ror_42_to50_store_test.rb

@@ -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