mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Adjust tests for serialized attributes so they no longer contain errors when run against ActiveRecord 4.2; prepare for RMing of SerializedAttributes
This commit is contained in:
parent
0360d413a4
commit
40b9c580de
3 changed files with 40 additions and 21 deletions
|
@ -15,6 +15,7 @@ class Person < ActiveRecord::Base
|
|||
|
||||
# Store TimeZone objects as strings when serialized to database
|
||||
class TimeZoneSerializer
|
||||
class << self
|
||||
def dump(zone)
|
||||
zone.try(:name)
|
||||
end
|
||||
|
@ -24,5 +25,14 @@ class Person < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def dump(zone)
|
||||
self.class.dump(zone)
|
||||
end
|
||||
|
||||
def load(value)
|
||||
self.class.load(value)
|
||||
end
|
||||
end
|
||||
|
||||
serialize :time_zone, TimeZoneSerializer.new
|
||||
end
|
||||
|
|
|
@ -60,7 +60,7 @@ module Dummy
|
|||
# Rails 4 key for generating secret key
|
||||
config.secret_key_base = 'A fox regularly kicked the screaming pile of biscuits.'
|
||||
|
||||
# Stop all the warnings!
|
||||
# supress warnings about raises in transactional callbacks
|
||||
config.active_record.raise_in_transactional_callbacks = true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -620,6 +620,8 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
assert_not_nil @wotsit.versions.last.reify.updated_at
|
||||
end
|
||||
|
||||
# Currently the gem generates a bunch of deprecation warnings about serialized attributes on AR 4.2
|
||||
if ActiveRecord::VERSION::STRING < '4.2'
|
||||
should 'not generate warning' do
|
||||
# Tests that it doesn't try to write created_on as an attribute just because a created_on
|
||||
# method exists.
|
||||
|
@ -630,6 +632,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
}
|
||||
assert_equal '', warnings
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
@ -974,12 +977,14 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
should 'version.object_changes attribute should have stored the value returned by the attribute serializer' do
|
||||
as_stored_in_version = HashWithIndifferentAccess[YAML::load(@person.versions.last.object_changes)]
|
||||
assert_equal [nil, 'Samoa'], as_stored_in_version[:time_zone]
|
||||
assert_equal @person.instance_variable_get(:@attributes)['time_zone'].serialized_value, as_stored_in_version[:time_zone].last
|
||||
serialized_value = Person::TimeZoneSerializer.dump(@person.time_zone)
|
||||
assert_equal serialized_value, as_stored_in_version[:time_zone].last
|
||||
end
|
||||
|
||||
# Tests for unserialization:
|
||||
should 'version.changeset should convert the attribute value back to its original, unserialized value' do
|
||||
assert_equal @person.instance_variable_get(:@attributes)['time_zone'].unserialized_value, @person.versions.last.changeset[:time_zone].last
|
||||
unserialized_value = Person::TimeZoneSerializer.load(@person.time_zone)
|
||||
assert_equal unserialized_value, @person.versions.last.changeset[:time_zone].last
|
||||
end
|
||||
should "record.changes (before save) returns the original, unserialized values" do
|
||||
assert_equal [NilClass, ActiveSupport::TimeZone], @changes_before_save[:time_zone].map(&:class)
|
||||
|
@ -991,7 +996,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
|
||||
context 'when that attribute is updated' do
|
||||
setup do
|
||||
@attribute_value_before_change = @person.instance_variable_get(:@attributes)['time_zone']
|
||||
@attribute_value_before_change = @person.time_zone
|
||||
@person.assign_attributes({ :time_zone => 'Pacific Time (US & Canada)' })
|
||||
@changes_before_save = @person.changes.dup
|
||||
@person.save!
|
||||
|
@ -1010,20 +1015,24 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|||
should 'version.object attribute should have stored the value returned by the attribute serializer' do
|
||||
as_stored_in_version = HashWithIndifferentAccess[YAML::load(@person.versions.last.object)]
|
||||
assert_equal 'Samoa', as_stored_in_version[:time_zone]
|
||||
assert_equal @attribute_value_before_change.serialized_value, as_stored_in_version[:time_zone]
|
||||
serialized_value = Person::TimeZoneSerializer.dump(@attribute_value_before_change)
|
||||
assert_equal serialized_value, as_stored_in_version[:time_zone]
|
||||
end
|
||||
should 'version.object_changes attribute should have stored the value returned by the attribute serializer' do
|
||||
as_stored_in_version = HashWithIndifferentAccess[YAML::load(@person.versions.last.object_changes)]
|
||||
assert_equal ['Samoa', 'Pacific Time (US & Canada)'], as_stored_in_version[:time_zone]
|
||||
assert_equal @person.instance_variable_get(:@attributes)['time_zone'].serialized_value, as_stored_in_version[:time_zone].last
|
||||
serialized_value = Person::TimeZoneSerializer.dump(@person.time_zone)
|
||||
assert_equal serialized_value, as_stored_in_version[:time_zone].last
|
||||
end
|
||||
|
||||
# Tests for unserialization:
|
||||
should 'version.reify should convert the attribute value back to its original, unserialized value' do
|
||||
assert_equal @attribute_value_before_change.unserialized_value, @person.versions.last.reify.time_zone
|
||||
unserialized_value = Person::TimeZoneSerializer.load(@attribute_value_before_change)
|
||||
assert_equal unserialized_value, @person.versions.last.reify.time_zone
|
||||
end
|
||||
should 'version.changeset should convert the attribute value back to its original, unserialized value' do
|
||||
assert_equal @person.instance_variable_get(:@attributes)['time_zone'].unserialized_value, @person.versions.last.changeset[:time_zone].last
|
||||
unserialized_value = Person::TimeZoneSerializer.load(@person.time_zone)
|
||||
assert_equal unserialized_value, @person.versions.last.changeset[:time_zone].last
|
||||
end
|
||||
should "record.changes (before save) returns the original, unserialized values" do
|
||||
assert_equal [ActiveSupport::TimeZone, ActiveSupport::TimeZone], @changes_before_save[:time_zone].map(&:class)
|
||||
|
|
Loading…
Reference in a new issue