diff options
Diffstat (limited to 'activerecord/test/cases/serialized_attribute_test.rb')
-rw-r--r-- | activerecord/test/cases/serialized_attribute_test.rb | 90 |
1 files changed, 65 insertions, 25 deletions
diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 6056156698..bebd856faf 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -1,10 +1,10 @@ -require 'cases/helper' -require 'models/topic' -require 'models/reply' -require 'models/person' -require 'models/traffic_light' -require 'models/post' -require 'bcrypt' +require "cases/helper" +require "models/topic" +require "models/reply" +require "models/person" +require "models/traffic_light" +require "models/post" +require "bcrypt" class SerializedAttributeTest < ActiveRecord::TestCase fixtures :topics, :posts @@ -25,7 +25,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase def test_serialized_attribute Topic.serialize("content", MyObject) - myobj = MyObject.new('value1', 'value2') + myobj = MyObject.new("value1", "value2") topic = Topic.create("content" => myobj) assert_equal(myobj, topic.content) @@ -36,7 +36,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase def test_serialized_attribute_in_base_class Topic.serialize("content", Hash) - hash = { 'content1' => 'value1', 'content2' => 'value2' } + hash = { "content1" => "value1", "content2" => "value2" } important_topic = ImportantTopic.create("content" => hash) assert_equal(hash, important_topic.content) @@ -97,7 +97,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_serialized_attribute_declared_in_subclass - hash = { 'important1' => 'value1', 'important2' => 'value2' } + hash = { "important1" => "value1", "important2" => "value2" } important_topic = ImportantTopic.create("important" => hash) assert_equal(hash, important_topic.important) @@ -124,26 +124,26 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_nil_not_serialized_without_class_constraint - assert Topic.new(:content => nil).save - assert_equal 1, Topic.where(:content => nil).count + assert Topic.new(content: nil).save + assert_equal 1, Topic.where(content: nil).count end def test_nil_not_serialized_with_class_constraint Topic.serialize :content, Hash - assert Topic.new(:content => nil).save - assert_equal 1, Topic.where(:content => nil).count + assert Topic.new(content: nil).save + assert_equal 1, Topic.where(content: nil).count end def test_serialized_attribute_should_raise_exception_on_assignment_with_wrong_type Topic.serialize(:content, Hash) assert_raise(ActiveRecord::SerializationTypeMismatch) do - Topic.new(content: 'string') + Topic.new(content: "string") end end def test_should_raise_exception_on_serialized_attribute_with_type_mismatch - myobj = MyObject.new('value1', 'value2') - topic = Topic.new(:content => myobj) + myobj = MyObject.new("value1", "value2") + topic = Topic.new(content: myobj) assert topic.save Topic.serialize(:content, Hash) assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content } @@ -152,11 +152,18 @@ class SerializedAttributeTest < ActiveRecord::TestCase def test_serialized_attribute_with_class_constraint settings = { "color" => "blue" } Topic.serialize(:content, Hash) - topic = Topic.new(:content => settings) + topic = Topic.new(content: settings) assert topic.save assert_equal(settings, Topic.find(topic.id).content) end + def test_where_by_serialized_attribute_with_hash + settings = { "color" => "green" } + Topic.serialize(:content, Hash) + topic = Topic.create!(content: settings) + assert_equal topic, Topic.where(content: settings).take + end + def test_serialized_default_class Topic.serialize(:content, Hash) topic = Topic.new @@ -175,14 +182,14 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_serialized_boolean_value_true - topic = Topic.new(:content => true) + topic = Topic.new(content: true) assert topic.save topic = topic.reload assert_equal topic.content, true end def test_serialized_boolean_value_false - topic = Topic.new(:content => false) + topic = Topic.new(content: false) assert topic.save topic = topic.reload assert_equal topic.content, false @@ -200,18 +207,18 @@ class SerializedAttributeTest < ActiveRecord::TestCase end Topic.serialize(:content, some_class) - topic = Topic.new(:content => some_class.new('my value')) + topic = Topic.new(content: some_class.new("my value")) topic.save! topic.reload assert_kind_of some_class, topic.content - assert_equal topic.content, some_class.new('my value') + assert_equal topic.content, some_class.new("my value") end def test_serialize_attribute_via_select_method_when_time_zone_available with_timezone_config aware_attributes: true do Topic.serialize(:content, MyObject) - myobj = MyObject.new('value1', 'value2') + myobj = MyObject.new("value1", "value2") topic = Topic.create(content: myobj) assert_equal(myobj, Topic.select(:content).find(topic.id).content) @@ -220,8 +227,8 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_serialize_attribute_can_be_serialized_in_an_integer_column - insures = ['life'] - person = SerializedPerson.new(first_name: 'David', insures: insures) + insures = ["life"] + person = SerializedPerson.new(first_name: "David", insures: insures) assert person.save person = person.reload assert_equal(insures, person.insures) @@ -295,4 +302,37 @@ class SerializedAttributeTest < ActiveRecord::TestCase topic.update_attribute :content, nil assert_equal [topic], Topic.where(content: nil) end + + def test_mutation_detection_does_not_double_serialize + coder = Object.new + def coder.dump(value) + return if value.nil? + value + " encoded" + end + def coder.load(value) + return if value.nil? + value.gsub(" encoded", "") + end + type = Class.new(ActiveModel::Type::Value) do + include ActiveModel::Type::Helpers::Mutable + + def serialize(value) + return if value.nil? + value + " serialized" + end + + def deserialize(value) + return if value.nil? + value.gsub(" serialized", "") + end + end.new + model = Class.new(Topic) do + attribute :foo, type + serialize :foo, coder + end + + topic = model.create!(foo: "bar") + topic.foo + refute topic.changed? + end end |