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 = {
|
2011-09-10 16:10:01 -04:00
|
|
|
:name => 'aaron stack',
|
|
|
|
:age => 25,
|
|
|
|
:avatar => 'binarydata',
|
|
|
|
:created_at => Time.utc(2006, 8, 1),
|
|
|
|
:awesome => false,
|
|
|
|
:preferences => { :gem => '<strong>ruby</strong>' },
|
2012-01-25 17:42:08 -05:00
|
|
|
:alternative_id => nil,
|
|
|
|
:id => nil
|
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
|
2012-01-29 11:54:17 -05:00
|
|
|
FORMATS.each do |format|
|
2007-09-20 19:22:30 -04:00
|
|
|
@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
|
2012-01-29 11:54:17 -05:00
|
|
|
FORMATS.each do |format|
|
2007-09-20 19:22:30 -04:00
|
|
|
@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
|
2012-01-29 11:54:17 -05:00
|
|
|
FORMATS.each do |format|
|
2007-09-20 19:22:30 -04:00
|
|
|
@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
|