2012-12-21 12:15:33 -05:00
|
|
|
require 'cases/helper'
|
2012-08-26 01:54:46 -04:00
|
|
|
require 'models/topic'
|
2013-04-24 13:04:07 -04:00
|
|
|
require 'models/reply'
|
2012-12-21 12:15:33 -05:00
|
|
|
require 'models/person'
|
2013-02-04 03:48:21 -05:00
|
|
|
require 'models/traffic_light'
|
2014-07-15 11:43:18 -04:00
|
|
|
require 'models/post'
|
2012-08-26 01:54:46 -04:00
|
|
|
require 'bcrypt'
|
|
|
|
|
|
|
|
class SerializedAttributeTest < ActiveRecord::TestCase
|
2014-07-15 11:43:18 -04:00
|
|
|
fixtures :topics, :posts
|
2012-08-26 01:54:46 -04:00
|
|
|
|
|
|
|
MyObject = Struct.new :attribute1, :attribute2
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2012-08-26 01:58:12 -04:00
|
|
|
Topic.serialize("content")
|
|
|
|
end
|
|
|
|
|
2014-06-06 11:43:09 -04:00
|
|
|
def test_serialize_does_not_eagerly_load_columns
|
2014-08-30 11:04:08 -04:00
|
|
|
Topic.reset_column_information
|
2014-06-06 11:43:09 -04:00
|
|
|
assert_no_queries do
|
|
|
|
Topic.serialize(:content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-26 01:54:46 -04:00
|
|
|
def test_list_of_serialized_attributes
|
2014-06-13 21:37:10 -04:00
|
|
|
assert_deprecated do
|
|
|
|
assert_equal %w(content), Topic.serialized_attributes.keys
|
|
|
|
end
|
2012-08-26 01:54:46 -04:00
|
|
|
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_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
|
|
|
|
|
2014-05-30 15:52:58 -04:00
|
|
|
def test_serialized_attributes_from_database_on_subclass
|
2012-08-26 02:03:12 -04:00
|
|
|
Topic.serialize :content, Hash
|
2012-08-26 01:54:46 -04:00
|
|
|
|
2014-05-30 15:52:58 -04:00
|
|
|
t = Reply.new(content: { foo: :bar })
|
|
|
|
assert_equal({ foo: :bar }, t.content)
|
2012-08-26 01:54:46 -04:00
|
|
|
t.save!
|
2014-05-30 15:52:58 -04:00
|
|
|
t = Reply.last
|
|
|
|
assert_equal({ foo: :bar }, t.content)
|
2012-08-26 01:54:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialized_attribute_calling_dup_method
|
2012-08-26 02:03:12 -04:00
|
|
|
Topic.serialize :content, JSON
|
2012-08-26 01:54:46 -04:00
|
|
|
|
2014-05-30 12:29:22 -04:00
|
|
|
orig = Topic.new(content: { foo: :bar })
|
|
|
|
clone = orig.dup
|
|
|
|
assert_equal(orig.content, clone.content)
|
2012-08-26 01:54:46 -04:00
|
|
|
end
|
|
|
|
|
2014-07-15 11:43:18 -04:00
|
|
|
def test_serialized_json_attribute_returns_unserialized_value
|
|
|
|
Topic.serialize :content, JSON
|
|
|
|
my_post = posts(:welcome)
|
|
|
|
|
|
|
|
t = Topic.new(content: my_post)
|
|
|
|
t.save!
|
|
|
|
t.reload
|
|
|
|
|
|
|
|
assert_instance_of(Hash, t.content)
|
|
|
|
assert_equal(my_post.id, t.content["id"])
|
|
|
|
assert_equal(my_post.title, t.content["title"])
|
|
|
|
end
|
|
|
|
|
2014-07-15 12:08:31 -04:00
|
|
|
def test_json_read_legacy_null
|
2014-07-14 13:38:14 -04:00
|
|
|
Topic.serialize :content, JSON
|
|
|
|
|
2014-07-15 12:08:31 -04:00
|
|
|
# Force a row to have a JSON "null" instead of a database NULL (this is how
|
|
|
|
# null values are saved on 4.1 and before)
|
|
|
|
id = Topic.connection.insert "INSERT INTO topics (content) VALUES('null')"
|
2014-07-14 13:38:14 -04:00
|
|
|
t = Topic.find(id)
|
|
|
|
|
|
|
|
assert_nil t.content
|
2014-07-15 12:08:31 -04:00
|
|
|
end
|
2014-07-14 13:38:14 -04:00
|
|
|
|
2014-07-15 12:08:31 -04:00
|
|
|
def test_json_read_db_null
|
|
|
|
Topic.serialize :content, JSON
|
2014-07-14 13:38:14 -04:00
|
|
|
|
2014-07-15 12:08:31 -04:00
|
|
|
# Force a row to have a database NULL instead of a JSON "null"
|
|
|
|
id = Topic.connection.insert "INSERT INTO topics (content) VALUES(NULL)"
|
|
|
|
t = Topic.find(id)
|
|
|
|
|
|
|
|
assert_nil t.content
|
2014-07-14 13:38:14 -04:00
|
|
|
end
|
|
|
|
|
2012-08-26 01:54:46 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2012-08-26 02:09:31 -04:00
|
|
|
def test_serialized_attribute_should_raise_exception_on_save_with_wrong_type
|
|
|
|
Topic.serialize(:content, Hash)
|
2014-05-30 12:29:22 -04:00
|
|
|
assert_raise(ActiveRecord::SerializationTypeMismatch) do
|
|
|
|
topic = Topic.new(content: 'string')
|
|
|
|
topic.save
|
|
|
|
end
|
2012-08-26 02:09:31 -04:00
|
|
|
end
|
|
|
|
|
2012-08-26 01:54:46 -04:00
|
|
|
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)
|
2012-08-26 02:03:53 -04:00
|
|
|
assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
|
2012-08-26 01:54:46 -04:00
|
|
|
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)
|
|
|
|
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"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialized_no_default_class_for_object
|
|
|
|
topic = Topic.new
|
|
|
|
assert_nil topic.content
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialized_boolean_value_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)
|
|
|
|
assert topic.save
|
|
|
|
topic = topic.reload
|
|
|
|
assert_equal topic.content, false
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialize_with_coder
|
2014-05-30 15:52:58 -04:00
|
|
|
some_class = Struct.new(:foo) do
|
|
|
|
def self.dump(value)
|
|
|
|
value.foo
|
2012-08-26 01:54:46 -04:00
|
|
|
end
|
|
|
|
|
2014-05-30 15:52:58 -04:00
|
|
|
def self.load(value)
|
|
|
|
new(value)
|
2012-08-26 01:54:46 -04:00
|
|
|
end
|
2014-05-30 15:52:58 -04:00
|
|
|
end
|
2012-08-26 01:54:46 -04:00
|
|
|
|
2014-05-30 15:52:58 -04:00
|
|
|
Topic.serialize(:content, some_class)
|
|
|
|
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')
|
2012-08-26 01:54:46 -04:00
|
|
|
end
|
2012-12-06 14:30:00 -05:00
|
|
|
|
|
|
|
def test_serialize_attribute_via_select_method_when_time_zone_available
|
2013-10-24 15:26:23 -04:00
|
|
|
with_timezone_config aware_attributes: true do
|
|
|
|
Topic.serialize(:content, MyObject)
|
2012-12-06 14:30:00 -05:00
|
|
|
|
2013-10-24 15:26:23 -04:00
|
|
|
myobj = MyObject.new('value1', 'value2')
|
|
|
|
topic = Topic.create(content: myobj)
|
2012-12-06 14:30:00 -05:00
|
|
|
|
2013-10-24 15:26:23 -04:00
|
|
|
assert_equal(myobj, Topic.select(:content).find(topic.id).content)
|
|
|
|
assert_raise(ActiveModel::MissingAttributeError) { Topic.select(:id).find(topic.id).content }
|
|
|
|
end
|
2012-12-06 14:30:00 -05:00
|
|
|
end
|
2012-12-21 12:15:33 -05:00
|
|
|
|
|
|
|
def test_serialize_attribute_can_be_serialized_in_an_integer_column
|
|
|
|
insures = ['life']
|
|
|
|
person = SerializedPerson.new(first_name: 'David', insures: insures)
|
|
|
|
assert person.save
|
|
|
|
person = person.reload
|
|
|
|
assert_equal(insures, person.insures)
|
|
|
|
end
|
2013-02-04 03:48:21 -05:00
|
|
|
|
|
|
|
def test_regression_serialized_default_on_text_column_with_null_false
|
|
|
|
light = TrafficLight.new
|
|
|
|
assert_equal [], light.state
|
|
|
|
assert_equal [], light.long_state
|
|
|
|
end
|
2013-04-24 13:04:07 -04:00
|
|
|
|
2014-04-05 13:19:18 -04:00
|
|
|
def test_serialized_column_should_unserialize_after_update_column
|
|
|
|
t = Topic.create(content: "first")
|
|
|
|
assert_equal("first", t.content)
|
|
|
|
|
2014-06-13 21:37:10 -04:00
|
|
|
t.update_column(:content, Topic.type_for_attribute('content').type_cast_for_database("second"))
|
2014-04-05 13:19:18 -04:00
|
|
|
assert_equal("second", t.content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialized_column_should_unserialize_after_update_attribute
|
|
|
|
t = Topic.create(content: "first")
|
|
|
|
assert_equal("first", t.content)
|
|
|
|
|
|
|
|
t.update_attribute(:content, "second")
|
|
|
|
assert_equal("second", t.content)
|
|
|
|
end
|
2012-08-26 01:54:46 -04:00
|
|
|
end
|