2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/contact'
|
2010-08-02 19:43:32 -04:00
|
|
|
require 'models/topic'
|
2007-09-20 19:22:30 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class SerializationTest < ActiveRecord::TestCase
|
2007-09-20 19:22:30 -04:00
|
|
|
FORMATS = [ :xml, :json ]
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-09-20 19:22:30 -04:00
|
|
|
def setup
|
|
|
|
@contact_attributes = {
|
|
|
|
:name => 'aaron stack',
|
|
|
|
:age => 25,
|
|
|
|
:avatar => 'binarydata',
|
|
|
|
:created_at => Time.utc(2006, 8, 1),
|
|
|
|
:awesome => false,
|
2007-10-28 20:18:43 -04:00
|
|
|
:preferences => { :gem => '<strong>ruby</strong>' }
|
2007-09-20 19:22:30 -04:00
|
|
|
}
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2011-02-01 18:23:55 -05:00
|
|
|
def test_serialized_init_with
|
|
|
|
topic = Topic.allocate
|
|
|
|
topic.init_with('attributes' => { 'content' => '--- foo' })
|
|
|
|
assert_equal 'foo', topic.content
|
|
|
|
end
|
|
|
|
|
2007-09-20 19:22:30 -04:00
|
|
|
def test_serialize_should_be_reversible
|
|
|
|
for format in FORMATS
|
|
|
|
@serialized = Contact.new.send("to_#{format}")
|
|
|
|
contact = Contact.new.send("from_#{format}", @serialized)
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-09-20 19:22:30 -04:00
|
|
|
assert_equal @contact_attributes.keys.collect(&:to_s).sort, contact.attributes.keys.collect(&:to_s).sort, "For #{format}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialize_should_allow_attribute_only_filtering
|
|
|
|
for format in FORMATS
|
|
|
|
@serialized = Contact.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ])
|
|
|
|
contact = Contact.new.send("from_#{format}", @serialized)
|
|
|
|
assert_equal @contact_attributes[:name], contact.name, "For #{format}"
|
|
|
|
assert_nil contact.avatar, "For #{format}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serialize_should_allow_attribute_except_filtering
|
|
|
|
for format in FORMATS
|
|
|
|
@serialized = Contact.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ])
|
|
|
|
contact = Contact.new.send("from_#{format}", @serialized)
|
|
|
|
assert_nil contact.name, "For #{format}"
|
|
|
|
assert_nil contact.age, "For #{format}"
|
|
|
|
assert_equal @contact_attributes[:awesome], contact.awesome, "For #{format}"
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:31:37 -05:00
|
|
|
end
|