From b9064279d6cb3eeeaeaccc4e7b044beb61c073c4 Mon Sep 17 00:00:00 2001 From: Master Lambaster Date: Tue, 13 Jul 2010 11:10:45 +0300 Subject: [PATCH] Test raw data types serialization --- activerecord/test/cases/base_test.rb | 45 ++++++++++++++++++++++++++++++++++ 1 files changed, 45 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index a4cf512..602d65b 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1539,6 +1539,7 @@ class BasicsTest < ActiveRecord::TestCase end MyObject = Struct.new :attribute1, :attribute2 + MyNewObject = Struct.new :attribute1, :attribute2 def test_serialized_attribute myobj = MyObject.new('value1', 'value2') @@ -1565,6 +1566,50 @@ class BasicsTest < ActiveRecord::TestCase assert_nil topic.content end + def test_serialized_multivalue_class_constraints + Topic.serialize(:content, [MyObject, MyNewObject]) + o1 = MyObject.new('v1', 'v2') + o2 = MyNewObject.new('v3', 'v4') + topic = Topic.create(:content => o1) + assert_equal(o1, topic.content) + topic.content = o2 + assert topic.save + assert_equal(o2, topic.content) + ensure + Topic.serialize(:content) + end + + def test_serialized_boolean_attribute + topic = Topic.create(:content => false) + assert_equal(false, Topic.find(topic.id).content) + end + + def test_serialized_fixnum_attribute + topic = Topic.create(:content => 42) + assert_equal(42, Topic.find(topic.id).content) + end + + def test_serialized_fixnum_attribute_with_class_constraint + Topic.serialize(:content, Fixnum) + topic = Topic.create(:content => 42) + assert_equal(42, Topic(topic.id).content) + ensure + Topic.serialize(:content) + end + + def test_serialized_float_attribute + topic = Topic.create(:content => 0.42) + assert_equal(0.42, Topic.find(topic.id).content) + end + + def test_serialized_fixnum_attribute_with_class_constraint + Topic.serialize(:content, Float) + topic = Topic.create(:content => 0.42) + assert_equal(0.42, Topic.find(topic.id).content) + ensure + Topic.serialize(:content) + end + def test_should_raise_exception_on_serialized_attribute_with_type_mismatch myobj = MyObject.new('value1', 'value2') topic = Topic.new(:content => myobj) -- 1.7.1.1