From 4ac07de799a18b0b751e83e64d4e5586008fe961 Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Wed, 8 Oct 2014 16:20:49 +0200 Subject: [PATCH] Unsuffixed association keys tests --- .../active_model/serializer/has_many_test.rb | 26 +++++++++++++++++++ .../active_model/serializer/has_one_test.rb | 26 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/test/unit/active_model/serializer/has_many_test.rb b/test/unit/active_model/serializer/has_many_test.rb index 2e5d212f..d1bb83ad 100644 --- a/test/unit/active_model/serializer/has_many_test.rb +++ b/test/unit/active_model/serializer/has_many_test.rb @@ -242,6 +242,32 @@ module ActiveModel } }, @post_serializer.as_json) 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 diff --git a/test/unit/active_model/serializer/has_one_test.rb b/test/unit/active_model/serializer/has_one_test.rb index 52fac88d..521c16d5 100644 --- a/test/unit/active_model/serializer/has_one_test.rb +++ b/test/unit/active_model/serializer/has_one_test.rb @@ -216,6 +216,32 @@ module ActiveModel } }, @user_serializer.as_json) 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