diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-26 02:54:46 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-26 03:56:18 -0300 |
commit | 21be1808a0887a12cf8124ad01a54bc075361f03 (patch) | |
tree | ee1f91bd8162b5f2e0113abd57be9b811b157258 | |
parent | 258461ee333ab6bcec983362f46e271fca9f3614 (diff) | |
download | rails-21be1808a0887a12cf8124ad01a54bc075361f03.tar.gz rails-21be1808a0887a12cf8124ad01a54bc075361f03.tar.bz2 rails-21be1808a0887a12cf8124ad01a54bc075361f03.zip |
Move the serialized attribute tests to their own file
-rw-r--r-- | activerecord/test/cases/attribute_methods_test.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 196 | ||||
-rw-r--r-- | activerecord/test/cases/serialized_attribute_test.rb | 214 |
3 files changed, 214 insertions, 207 deletions
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index c043e32a20..1ea6f8295a 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -729,17 +729,6 @@ class AttributeMethodsTest < ActiveRecord::TestCase Object.send(:undef_method, :title) # remove test method from object end - def test_list_of_serialized_attributes - assert_equal %w(content), Topic.serialized_attributes.keys - assert_equal %w(preferences), Contact.serialized_attributes.keys - end - - def test_serialized_attributes_are_class_level_settings - topic = Topic.new - assert_raise(NoMethodError) { topic.serialized_attributes = [] } - assert_deprecated { topic.serialized_attributes } - end - def test_instance_method_should_be_defined_on_the_base_class subklass = Class.new(Topic) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 8915e1aea7..6c65183768 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -26,7 +26,6 @@ require 'models/bird' require 'models/teapot' require 'rexml/document' require 'active_support/core_ext/exception' -require 'bcrypt' class FirstAbstractClass < ActiveRecord::Base self.abstract_class = true @@ -1297,201 +1296,6 @@ class BasicsTest < ActiveRecord::TestCase assert_equal 0, replies.size end - MyObject = Struct.new :attribute1, :attribute2 - - def test_serialized_attribute - Topic.serialize("content", MyObject) - - myobj = MyObject.new('value1', 'value2') - topic = Topic.create("content" => myobj) - assert_equal(myobj, topic.content) - - topic.reload - assert_equal(myobj, topic.content) - end - - def test_serialized_attribute_init_with - topic = Topic.allocate - topic.init_with('attributes' => { 'content' => '--- foo' }) - assert_equal 'foo', topic.content - end - - def test_serialized_attribute_in_base_class - Topic.serialize("content", Hash) - - hash = { 'content1' => 'value1', 'content2' => 'value2' } - important_topic = ImportantTopic.create("content" => hash) - assert_equal(hash, important_topic.content) - - important_topic.reload - assert_equal(hash, important_topic.content) - end - - # This test was added to fix GH #4004. Obviously the value returned - # is not really the value 'before type cast' so we should maybe think - # about changing that in the future. - def test_serialized_attribute_before_type_cast_returns_unserialized_value - klass = Class.new(ActiveRecord::Base) - klass.table_name = "topics" - klass.serialize :content, Hash - - t = klass.new(:content => { :foo => :bar }) - assert_equal({ :foo => :bar }, t.content_before_type_cast) - t.save! - t.reload - assert_equal({ :foo => :bar }, t.content_before_type_cast) - end - - def test_serialized_attribute_calling_dup_method - klass = Class.new(ActiveRecord::Base) - klass.table_name = "topics" - klass.serialize :content, JSON - - t = klass.new(:content => { :foo => :bar }).dup - assert_equal({ :foo => :bar }, t.content_before_type_cast) - end - - def test_serialized_attribute_declared_in_subclass - hash = { 'important1' => 'value1', 'important2' => 'value2' } - important_topic = ImportantTopic.create("important" => hash) - assert_equal(hash, important_topic.important) - - important_topic.reload - assert_equal(hash, important_topic.important) - assert_equal(hash, important_topic.read_attribute(:important)) - end - - def test_serialized_time_attribute - myobj = Time.local(2008,1,1,1,0) - topic = Topic.create("content" => myobj).reload - assert_equal(myobj, topic.content) - end - - def test_serialized_string_attribute - myobj = "Yes" - topic = Topic.create("content" => myobj).reload - assert_equal(myobj, topic.content) - end - - def test_nil_serialized_attribute_without_class_constraint - topic = Topic.new - assert_nil topic.content - end - - def test_nil_not_serialized_without_class_constraint - 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 - 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) - assert topic.save - Topic.serialize(:content, Hash) - assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).reload.content } - ensure - Topic.serialize(:content) - end - - def test_serialized_attribute_with_class_constraint - settings = { "color" => "blue" } - Topic.serialize(:content, Hash) - topic = Topic.new(:content => settings) - assert topic.save - assert_equal(settings, Topic.find(topic.id).content) - ensure - Topic.serialize(:content) - end - - def test_serialized_default_class - Topic.serialize(:content, Hash) - topic = Topic.new - assert_equal Hash, topic.content.class - assert_equal Hash, topic.read_attribute(:content).class - topic.content["beer"] = "MadridRb" - assert topic.save - topic.reload - assert_equal Hash, topic.content.class - assert_equal "MadridRb", topic.content["beer"] - ensure - Topic.serialize(:content) - end - - def test_serialized_no_default_class_for_object - topic = Topic.new - assert_nil topic.content - end - - def test_serialized_boolean_value_true - Topic.serialize(:content) - topic = Topic.new(:content => true) - assert topic.save - topic = topic.reload - assert_equal topic.content, true - end - - def test_serialized_boolean_value_false - Topic.serialize(:content) - topic = Topic.new(:content => false) - assert topic.save - topic = topic.reload - assert_equal topic.content, false - end - - def test_serialize_with_coder - coder = Class.new { - # Identity - def load(thing) - thing - end - - # base 64 - def dump(thing) - [thing].pack('m') - end - }.new - - Topic.serialize(:content, coder) - s = 'hello world' - topic = Topic.new(:content => s) - assert topic.save - topic = topic.reload - assert_equal [s].pack('m'), topic.content - ensure - Topic.serialize(:content) - end - - def test_serialize_with_bcrypt_coder - crypt_coder = Class.new { - def load(thing) - return unless thing - BCrypt::Password.new thing - end - - def dump(thing) - BCrypt::Password.create(thing).to_s - end - }.new - - Topic.serialize(:content, crypt_coder) - password = 'password' - topic = Topic.new(:content => password) - assert topic.save - topic = topic.reload - assert_kind_of BCrypt::Password, topic.content - assert_equal(true, topic.content == password, 'password should equal') - ensure - Topic.serialize(:content) - end - def test_quote author_name = "\\ \001 ' \n \\n \"" topic = Topic.create('author_name' => author_name) diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb new file mode 100644 index 0000000000..fab297db14 --- /dev/null +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -0,0 +1,214 @@ +require "cases/helper" +require 'models/topic' +require 'bcrypt' + +class SerializedAttributeTest < ActiveRecord::TestCase + fixtures :topics + + MyObject = Struct.new :attribute1, :attribute2 + + def test_list_of_serialized_attributes + assert_equal %w(content), Topic.serialized_attributes.keys + end + + def test_serialized_attributes_are_class_level_settings + topic = Topic.new + assert_raise(NoMethodError) { topic.serialized_attributes = [] } + assert_deprecated { topic.serialized_attributes } + end + + def test_serialized_attribute + Topic.serialize("content", MyObject) + + myobj = MyObject.new('value1', 'value2') + topic = Topic.create("content" => myobj) + assert_equal(myobj, topic.content) + + topic.reload + assert_equal(myobj, topic.content) + end + + def test_serialized_attribute_init_with + Topic.serialize("content") + topic = Topic.allocate + topic.init_with('attributes' => { 'content' => '--- foo' }) + assert_equal 'foo', topic.content + end + + def test_serialized_attribute_in_base_class + Topic.serialize("content", Hash) + + hash = { 'content1' => 'value1', 'content2' => 'value2' } + important_topic = ImportantTopic.create("content" => hash) + assert_equal(hash, important_topic.content) + + important_topic.reload + assert_equal(hash, important_topic.content) + end + + # This test was added to fix GH #4004. Obviously the value returned + # is not really the value 'before type cast' so we should maybe think + # about changing that in the future. + def test_serialized_attribute_before_type_cast_returns_unserialized_value + klass = Class.new(ActiveRecord::Base) + klass.table_name = "topics" + klass.serialize :content, Hash + + t = klass.new(:content => { :foo => :bar }) + assert_equal({ :foo => :bar }, t.content_before_type_cast) + t.save! + t.reload + assert_equal({ :foo => :bar }, t.content_before_type_cast) + end + + def test_serialized_attribute_calling_dup_method + klass = Class.new(ActiveRecord::Base) + klass.table_name = "topics" + klass.serialize :content, JSON + + t = klass.new(:content => { :foo => :bar }).dup + assert_equal({ :foo => :bar }, t.content_before_type_cast) + end + + def test_serialized_attribute_declared_in_subclass + hash = { 'important1' => 'value1', 'important2' => 'value2' } + important_topic = ImportantTopic.create("important" => hash) + assert_equal(hash, important_topic.important) + + important_topic.reload + assert_equal(hash, important_topic.important) + assert_equal(hash, important_topic.read_attribute(:important)) + end + + def test_serialized_time_attribute + myobj = Time.local(2008,1,1,1,0) + topic = Topic.create("content" => myobj).reload + assert_equal(myobj, topic.content) + end + + def test_serialized_string_attribute + myobj = "Yes" + topic = Topic.create("content" => myobj).reload + assert_equal(myobj, topic.content) + end + + def test_nil_serialized_attribute_without_class_constraint + topic = Topic.new + assert_nil topic.content + end + + def test_nil_not_serialized_without_class_constraint + 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 + 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) + assert topic.save + Topic.serialize(:content, Hash) + assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).reload.content } + ensure + Topic.serialize(:content) + end + + def test_serialized_attribute_with_class_constraint + settings = { "color" => "blue" } + Topic.serialize(:content, Hash) + topic = Topic.new(:content => settings) + assert topic.save + assert_equal(settings, Topic.find(topic.id).content) + ensure + Topic.serialize(:content) + end + + def test_serialized_default_class + Topic.serialize(:content, Hash) + topic = Topic.new + assert_equal Hash, topic.content.class + assert_equal Hash, topic.read_attribute(:content).class + topic.content["beer"] = "MadridRb" + assert topic.save + topic.reload + assert_equal Hash, topic.content.class + assert_equal "MadridRb", topic.content["beer"] + ensure + Topic.serialize(:content) + end + + def test_serialized_no_default_class_for_object + topic = Topic.new + assert_nil topic.content + end + + def test_serialized_boolean_value_true + Topic.serialize(:content) + topic = Topic.new(:content => true) + assert topic.save + topic = topic.reload + assert_equal topic.content, true + end + + def test_serialized_boolean_value_false + Topic.serialize(:content) + topic = Topic.new(:content => false) + assert topic.save + topic = topic.reload + assert_equal topic.content, false + end + + def test_serialize_with_coder + coder = Class.new { + # Identity + def load(thing) + thing + end + + # base 64 + def dump(thing) + [thing].pack('m') + end + }.new + + Topic.serialize(:content, coder) + s = 'hello world' + topic = Topic.new(:content => s) + assert topic.save + topic = topic.reload + assert_equal [s].pack('m'), topic.content + ensure + Topic.serialize(:content) + end + + def test_serialize_with_bcrypt_coder + crypt_coder = Class.new { + def load(thing) + return unless thing + BCrypt::Password.new thing + end + + def dump(thing) + BCrypt::Password.create(thing).to_s + end + }.new + + Topic.serialize(:content, crypt_coder) + password = 'password' + topic = Topic.new(:content => password) + assert topic.save + topic = topic.reload + assert_kind_of BCrypt::Password, topic.content + assert_equal(true, topic.content == password, 'password should equal') + ensure + Topic.serialize(:content) + end + +end |