Skip to content

Commit 4ac07de

Browse files
committed
Unsuffixed association keys tests
1 parent 6f4f30c commit 4ac07de

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

test/unit/active_model/serializer/has_many_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ def test_associations_embedding_objects_using_embed_namespace
242242
}
243243
}, @post_serializer.as_json)
244244
end
245+
246+
CONFIG.default_key_type = :name
247+
class NameKeyPostSerializer < ActiveModel::Serializer
248+
attributes :title, :body
249+
250+
has_many :comments
251+
end
252+
CONFIG.default_key_type = nil
253+
254+
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
255+
@association = NameKeyPostSerializer._associations[:comments]
256+
@association.embed = :ids
257+
258+
assert_equal({
259+
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
260+
}, NameKeyPostSerializer.new(@post).serializable_hash)
261+
end
262+
263+
def test_associations_name_key_embedding_ids_serialization_using_as_json
264+
@association = NameKeyPostSerializer._associations[:comments]
265+
@association.embed = :ids
266+
267+
assert_equal({
268+
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
269+
}, NameKeyPostSerializer.new(@post).as_json)
270+
end
245271
end
246272
end
247273
end

test/unit/active_model/serializer/has_one_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ def test_associations_embedding_ids_using_embed_namespace_and_embed_in_root_key
216216
}
217217
}, @user_serializer.as_json)
218218
end
219+
220+
CONFIG.default_key_type = :name
221+
class NameKeyUserSerializer < ActiveModel::Serializer
222+
attributes :name, :email
223+
224+
has_one :profile
225+
end
226+
CONFIG.default_key_type = nil
227+
228+
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
229+
@association = NameKeyUserSerializer._associations[:profile]
230+
@association.embed = :ids
231+
232+
assert_equal({
233+
name: 'Name 1', email: '[email protected]', 'profile' => @user.profile.object_id
234+
}, NameKeyUserSerializer.new(@user).serializable_hash)
235+
end
236+
237+
def test_associations_name_key_embedding_ids_serialization_using_as_json
238+
@association = NameKeyUserSerializer._associations[:profile]
239+
@association.embed = :ids
240+
241+
assert_equal({
242+
'name_key_user' => { name: 'Name 1', email: '[email protected]', 'profile' => @user.profile.object_id }
243+
}, NameKeyUserSerializer.new(@user).as_json)
244+
end
219245
end
220246
end
221247
end

0 commit comments

Comments
 (0)