Unsuffixed association keys tests

This commit is contained in:
Gauthier Delacroix 2014-10-08 16:20:49 +02:00
parent 6f4f30cda6
commit 4ac07de799
2 changed files with 52 additions and 0 deletions

View File

@ -242,6 +242,32 @@ module ActiveModel
} }
}, @post_serializer.as_json) }, @post_serializer.as_json)
end end
CONFIG.default_key_type = :name
class NameKeyPostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments
end
CONFIG.default_key_type = nil
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
@association = NameKeyPostSerializer._associations[:comments]
@association.embed = :ids
assert_equal({
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
}, NameKeyPostSerializer.new(@post).serializable_hash)
end
def test_associations_name_key_embedding_ids_serialization_using_as_json
@association = NameKeyPostSerializer._associations[:comments]
@association.embed = :ids
assert_equal({
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
}, NameKeyPostSerializer.new(@post).as_json)
end
end end
end end
end end

View File

@ -216,6 +216,32 @@ module ActiveModel
} }
}, @user_serializer.as_json) }, @user_serializer.as_json)
end end
CONFIG.default_key_type = :name
class NameKeyUserSerializer < ActiveModel::Serializer
attributes :name, :email
has_one :profile
end
CONFIG.default_key_type = nil
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
@association = NameKeyUserSerializer._associations[:profile]
@association.embed = :ids
assert_equal({
name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id
}, NameKeyUserSerializer.new(@user).serializable_hash)
end
def test_associations_name_key_embedding_ids_serialization_using_as_json
@association = NameKeyUserSerializer._associations[:profile]
@association.embed = :ids
assert_equal({
'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }
}, NameKeyUserSerializer.new(@user).as_json)
end
end end
end end
end end