2010-08-03 10:26:24 -04:00
|
|
|
require "cases/helper"
|
2011-02-22 01:55:49 -05:00
|
|
|
require 'active_support/core_ext/object/instance_variables'
|
2010-08-03 10:26:24 -04:00
|
|
|
|
|
|
|
class SerializationTest < ActiveModel::TestCase
|
|
|
|
class User
|
2011-11-30 12:38:28 -05:00
|
|
|
include ActiveModel::Serialization
|
2010-08-03 10:26:24 -04:00
|
|
|
|
2011-02-22 01:55:49 -05:00
|
|
|
attr_accessor :name, :email, :gender, :address, :friends
|
|
|
|
|
|
|
|
def initialize(name, email, gender)
|
|
|
|
@name, @email, @gender = name, email, gender
|
|
|
|
@friends = []
|
|
|
|
end
|
2010-08-03 10:26:24 -04:00
|
|
|
|
|
|
|
def attributes
|
2011-02-22 01:55:49 -05:00
|
|
|
instance_values.except("address", "friends")
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def foo
|
|
|
|
'i_am_foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-22 01:55:49 -05:00
|
|
|
class Address
|
2011-11-30 12:38:28 -05:00
|
|
|
include ActiveModel::Serialization
|
2011-02-22 01:55:49 -05:00
|
|
|
|
|
|
|
attr_accessor :street, :city, :state, :zip
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
instance_values
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-03 10:26:24 -04:00
|
|
|
setup do
|
2011-02-22 01:55:49 -05:00
|
|
|
@user = User.new('David', 'david@example.com', 'male')
|
|
|
|
@user.address = Address.new
|
|
|
|
@user.address.street = "123 Lane"
|
|
|
|
@user.address.city = "Springfield"
|
|
|
|
@user.address.state = "CA"
|
|
|
|
@user.address.zip = 11111
|
|
|
|
@user.friends = [User.new('Joe', 'joe@example.com', 'male'),
|
|
|
|
User.new('Sue', 'sue@example.com', 'female')]
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected, @user.serializable_hash
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_only_option
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"name"=>"David"}
|
|
|
|
assert_equal expected, @user.serializable_hash(:only => [:name])
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_except_option
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"gender"=>"male", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected, @user.serializable_hash(:except => [:name])
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_methods_option
|
2012-02-13 08:22:59 -05:00
|
|
|
expected = {"name"=>"David", "gender"=>"male", "foo"=>"i_am_foo", "email"=>"david@example.com"}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:methods => [:foo])
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|
|
|
|
|
2011-02-21 19:17:30 -05:00
|
|
|
def test_method_serializable_hash_should_work_with_only_and_methods
|
2012-02-13 08:22:59 -05:00
|
|
|
expected = {"foo"=>"i_am_foo"}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:only => [], :methods => [:foo])
|
2011-02-21 19:17:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_except_and_methods
|
2012-02-13 08:22:59 -05:00
|
|
|
expected = {"gender"=>"male", "foo"=>"i_am_foo"}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:except => [:name, :email], :methods => [:foo])
|
2011-02-21 19:17:30 -05:00
|
|
|
end
|
|
|
|
|
2011-02-21 19:29:10 -05:00
|
|
|
def test_should_not_call_methods_that_dont_respond
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected, @user.serializable_hash(:methods => [:bar])
|
2011-02-21 19:29:10 -05:00
|
|
|
end
|
|
|
|
|
2011-09-30 08:20:41 -04:00
|
|
|
def test_should_use_read_attribute_for_serialization
|
|
|
|
def @user.read_attribute_for_serialization(n)
|
2011-09-26 07:40:03 -04:00
|
|
|
"Jon"
|
|
|
|
end
|
|
|
|
|
2011-09-30 08:20:41 -04:00
|
|
|
expected = { "name" => "Jon" }
|
2011-09-26 07:40:03 -04:00
|
|
|
assert_equal expected, @user.serializable_hash(:only => :name)
|
|
|
|
end
|
|
|
|
|
2011-02-22 01:55:49 -05:00
|
|
|
def test_include_option_with_singular_association
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com",
|
2012-03-09 19:51:12 -05:00
|
|
|
"address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:include => :address)
|
2011-02-22 01:55:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_option_with_plural_association
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
2012-03-09 19:51:12 -05:00
|
|
|
"friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
|
2012-02-13 08:19:44 -05:00
|
|
|
{"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
|
|
|
|
assert_equal expected, @user.serializable_hash(:include => :friends)
|
2011-02-22 01:55:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_option_with_empty_association
|
|
|
|
@user.friends = []
|
2012-03-09 19:51:12 -05:00
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", "friends"=>[]}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:include => :friends)
|
2011-02-22 01:55:49 -05:00
|
|
|
end
|
|
|
|
|
2012-05-11 12:17:29 -04:00
|
|
|
class FriendList
|
|
|
|
def initialize(friends)
|
|
|
|
@friends = friends
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_ary
|
|
|
|
@friends
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_option_with_ary
|
|
|
|
@user.friends = FriendList.new(@user.friends)
|
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
|
|
|
"friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
|
|
|
|
{"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
|
|
|
|
assert_equal expected, @user.serializable_hash(:include => :friends)
|
|
|
|
end
|
|
|
|
|
2011-02-22 01:55:49 -05:00
|
|
|
def test_multiple_includes
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
2012-03-09 19:51:12 -05:00
|
|
|
"address"=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111},
|
|
|
|
"friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
|
2012-02-13 08:19:44 -05:00
|
|
|
{"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
|
|
|
|
assert_equal expected, @user.serializable_hash(:include => [:address, :friends])
|
2011-02-22 01:55:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_with_options
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
2012-03-09 19:51:12 -05:00
|
|
|
"address"=>{"street"=>"123 Lane"}}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:include => {:address => {:only => "street"}})
|
2011-02-22 01:55:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_include
|
|
|
|
@user.friends.first.friends = [@user]
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
2012-03-09 19:51:12 -05:00
|
|
|
"friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male',
|
|
|
|
"friends"=> [{"email"=>"david@example.com", "gender"=>"male", "name"=>"David"}]},
|
|
|
|
{"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female', "friends"=> []}]}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:include => {:friends => {:include => :friends}})
|
2011-02-22 01:55:49 -05:00
|
|
|
end
|
2011-08-23 06:33:58 -04:00
|
|
|
|
|
|
|
def test_only_include
|
2012-03-09 19:51:12 -05:00
|
|
|
expected = {"name"=>"David", "friends" => [{"name" => "Joe"}, {"name" => "Sue"}]}
|
2012-02-13 08:19:44 -05:00
|
|
|
assert_equal expected, @user.serializable_hash(:only => :name, :include => {:friends => {:only => :name}})
|
2011-08-23 06:33:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_except_include
|
|
|
|
expected = {"name"=>"David", "email"=>"david@example.com",
|
2012-03-09 19:51:12 -05:00
|
|
|
"friends"=> [{"name" => 'Joe', "email" => 'joe@example.com'},
|
2012-02-13 08:19:44 -05:00
|
|
|
{"name" => "Sue", "email" => 'sue@example.com'}]}
|
|
|
|
assert_equal expected, @user.serializable_hash(:except => :gender, :include => {:friends => {:except => :gender}})
|
2011-08-23 06:33:58 -04:00
|
|
|
end
|
|
|
|
|
2011-10-11 12:09:43 -04:00
|
|
|
def test_multiple_includes_with_options
|
2012-02-13 08:19:44 -05:00
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
2012-03-09 19:51:12 -05:00
|
|
|
"address"=>{"street"=>"123 Lane"},
|
|
|
|
"friends"=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
|
2012-02-13 08:19:44 -05:00
|
|
|
{"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
|
|
|
|
assert_equal expected, @user.serializable_hash(:include => [{:address => {:only => "street"}}, :friends])
|
2011-10-11 12:09:43 -04:00
|
|
|
end
|
2010-08-03 10:26:24 -04:00
|
|
|
end
|