2010-08-03 14:26:24 +00:00
|
|
|
require "cases/helper"
|
2011-02-22 06:55:49 +00:00
|
|
|
require 'active_support/core_ext/object/instance_variables'
|
2010-08-03 14:26:24 +00:00
|
|
|
|
|
|
|
class SerializationTest < ActiveModel::TestCase
|
|
|
|
class User
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2011-02-22 06:55:49 +00:00
|
|
|
attr_accessor :name, :email, :gender, :address, :friends
|
|
|
|
|
|
|
|
def initialize(name, email, gender)
|
|
|
|
@name, @email, @gender = name, email, gender
|
|
|
|
@friends = []
|
|
|
|
end
|
2010-08-03 14:26:24 +00:00
|
|
|
|
|
|
|
def attributes
|
2011-02-22 06:55:49 +00:00
|
|
|
instance_values.except("address", "friends")
|
2010-08-03 14:26:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def foo
|
|
|
|
'i_am_foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-22 06:55:49 +00:00
|
|
|
class Address
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
|
|
|
attr_accessor :street, :city, :state, :zip
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
instance_values
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-03 14:26:24 +00:00
|
|
|
setup do
|
2011-02-22 06:55:49 +00: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 14:26:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work
|
|
|
|
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected , @user.serializable_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_only_option
|
|
|
|
expected = {"name"=>"David"}
|
|
|
|
assert_equal expected , @user.serializable_hash(:only => [:name])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_except_option
|
|
|
|
expected = {"gender"=>"male", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected , @user.serializable_hash(:except => [:name])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_methods_option
|
|
|
|
expected = {"name"=>"David", "gender"=>"male", :foo=>"i_am_foo", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected , @user.serializable_hash(:methods => [:foo])
|
|
|
|
end
|
|
|
|
|
2011-02-22 00:17:30 +00:00
|
|
|
def test_method_serializable_hash_should_work_with_only_and_methods
|
|
|
|
expected = {:foo=>"i_am_foo"}
|
|
|
|
assert_equal expected , @user.serializable_hash(:only => [], :methods => [:foo])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_method_serializable_hash_should_work_with_except_and_methods
|
|
|
|
expected = {"gender"=>"male", :foo=>"i_am_foo"}
|
|
|
|
assert_equal expected , @user.serializable_hash(:except => [:name, :email], :methods => [:foo])
|
|
|
|
end
|
|
|
|
|
2011-02-22 00:29:10 +00:00
|
|
|
def test_should_not_call_methods_that_dont_respond
|
|
|
|
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com"}
|
|
|
|
assert_equal expected , @user.serializable_hash(:methods => [:bar])
|
|
|
|
end
|
|
|
|
|
2011-02-22 06:55:49 +00:00
|
|
|
def test_include_option_with_singular_association
|
|
|
|
expected = {"name"=>"David", "gender"=>"male", "email"=>"david@example.com",
|
|
|
|
:address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111}}
|
|
|
|
assert_equal expected , @user.serializable_hash(:include => :address)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_option_with_plural_association
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_include_option_with_empty_association
|
|
|
|
@user.friends = []
|
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David", :friends=>[]}
|
|
|
|
assert_equal expected , @user.serializable_hash(:include => :friends)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiple_includes
|
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
|
|
|
:address=>{"street"=>"123 Lane", "city"=>"Springfield", "state"=>"CA", "zip"=>11111},
|
|
|
|
:friends=>[{"name"=>'Joe', "email"=>'joe@example.com', "gender"=>'male'},
|
|
|
|
{"name"=>'Sue', "email"=>'sue@example.com', "gender"=>'female'}]}
|
|
|
|
assert_equal expected , @user.serializable_hash(:include => [:address, :friends])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_with_options
|
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
|
|
|
:address=>{"street"=>"123 Lane"}}
|
|
|
|
assert_equal expected , @user.serializable_hash(:include => {:address => {:only => "street"}})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested_include
|
|
|
|
@user.friends.first.friends = [@user]
|
|
|
|
expected = {"email"=>"david@example.com", "gender"=>"male", "name"=>"David",
|
|
|
|
: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 => []}]}
|
|
|
|
assert_equal expected , @user.serializable_hash(:include => {:friends => {:include => :friends}})
|
|
|
|
end
|
2010-08-03 14:26:24 +00:00
|
|
|
end
|