mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
merge upstream update fieldset
This commit is contained in:
@@ -7,6 +7,8 @@ module ActiveModel
|
||||
class BelongsToTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = []
|
||||
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
||||
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@@ -31,12 +33,28 @@ module ActiveModel
|
||||
|
||||
def test_includes_linked_post
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post')
|
||||
assert_equal([{id: "42", title: 'New Post', body: 'Body'}], @adapter.serializable_hash[:linked][:posts])
|
||||
expected = [{
|
||||
id: "42",
|
||||
title: 'New Post',
|
||||
body: 'Body',
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
end
|
||||
|
||||
def test_limiting_linked_post_fields
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'post', fields: {post: [:title]})
|
||||
assert_equal([{title: 'New Post'}], @adapter.serializable_hash[:linked][:posts])
|
||||
expected = [{
|
||||
title: 'New Post',
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
end
|
||||
|
||||
def test_include_nil_author
|
||||
@@ -49,7 +67,53 @@ module ActiveModel
|
||||
def test_include_type_for_association_when_is_different_than_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
assert_equal({type: "author", id: "1"}, adapter.serializable_hash[:blogs][:links][:writer])
|
||||
links = adapter.serializable_hash[:blogs][:links]
|
||||
expected = {
|
||||
writer: {
|
||||
type: "author",
|
||||
id: "1"
|
||||
},
|
||||
articles: {
|
||||
type: "posts",
|
||||
ids: ["42", "43"]
|
||||
}
|
||||
}
|
||||
assert_equal expected, links
|
||||
end
|
||||
|
||||
def test_include_linked_resources_with_type_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, include: "writer,articles")
|
||||
linked = adapter.serializable_hash[:linked]
|
||||
expected = {
|
||||
authors: [{
|
||||
id: "1",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: [],
|
||||
roles: [],
|
||||
bio: nil
|
||||
}
|
||||
}],
|
||||
posts: [{
|
||||
title: "New Post",
|
||||
body: "Body",
|
||||
id: "42",
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}, {
|
||||
title: "Hello!!",
|
||||
body: "Hello, world!!",
|
||||
id: "43",
|
||||
links: {
|
||||
comments: [],
|
||||
author: nil
|
||||
}
|
||||
}]
|
||||
}
|
||||
assert_equal expected, linked
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@ module ActiveModel
|
||||
class CollectionTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
||||
@first_post.comments = []
|
||||
|
||||
@@ -7,6 +7,8 @@ module ActiveModel
|
||||
class HasManyEmbedIdsTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(name: 'Steve K.')
|
||||
@author.bio = nil
|
||||
@author.roles = nil
|
||||
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
||||
@author.posts = [@first_post, @second_post]
|
||||
|
||||
@@ -8,6 +8,7 @@ module ActiveModel
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@author.posts = []
|
||||
@author.bio = nil
|
||||
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
||||
@post_without_comments = Post.new(id: 2, title: 'Second Post', body: 'Second')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@@ -31,21 +32,43 @@ module ActiveModel
|
||||
def test_includes_comment_ids
|
||||
assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments])
|
||||
end
|
||||
|
||||
|
||||
def test_includes_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments')
|
||||
assert_equal([
|
||||
{id: "1", body: 'ZOMG A COMMENT'},
|
||||
{id: "2", body: 'ZOMG ANOTHER COMMENT'}
|
||||
], @adapter.serializable_hash[:linked][:comments])
|
||||
expected = [{
|
||||
id: "1",
|
||||
body: 'ZOMG A COMMENT',
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
body: 'ZOMG ANOTHER COMMENT',
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
||||
end
|
||||
|
||||
def test_limit_fields_of_linked_comments
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:body]})
|
||||
assert_equal([
|
||||
{body: 'ZOMG A COMMENT'},
|
||||
{body: 'ZOMG ANOTHER COMMENT'}
|
||||
], @adapter.serializable_hash[:linked][:comments])
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'comments', fields: {comment: [:id]})
|
||||
expected = [{
|
||||
id: "1",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:comments]
|
||||
end
|
||||
|
||||
def test_no_include_linked_if_comments_is_empty
|
||||
|
||||
@@ -7,6 +7,7 @@ module ActiveModel
|
||||
class LinkedTest < Minitest::Test
|
||||
def setup
|
||||
@author = Author.new(id: 1, name: 'Steve K.')
|
||||
@bio = Bio.new(id: 1, content: 'AMS Contributor')
|
||||
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
||||
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
||||
@first_post.comments = []
|
||||
@@ -14,9 +15,12 @@ module ActiveModel
|
||||
@first_post.author = @author
|
||||
@second_post.author = @author
|
||||
@author.posts = [@first_post, @second_post]
|
||||
@author.bio = @bio
|
||||
@author.roles = []
|
||||
@bio.author = @author
|
||||
|
||||
@serializer = ArraySerializer.new([@first_post, @second_post])
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,comments')
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'author,author.bio,comments')
|
||||
end
|
||||
|
||||
def test_include_multiple_posts_and_linked
|
||||
@@ -31,7 +35,42 @@ module ActiveModel
|
||||
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: ['1', '2'], author: "1" } },
|
||||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], :author => "1" } }
|
||||
], @adapter.serializable_hash[:posts])
|
||||
assert_equal({ :comments => [{ :id => "1", :body => "ZOMG A COMMENT" }, { :id => "2", :body => "ZOMG ANOTHER COMMENT" }], :authors => [{ :id => "1", :name => "Steve K." }] }, @adapter.serializable_hash[:linked])
|
||||
|
||||
|
||||
expected = {
|
||||
comments: [{
|
||||
id: "1",
|
||||
body: "ZOMG A COMMENT",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}, {
|
||||
id: "2",
|
||||
body: "ZOMG ANOTHER COMMENT",
|
||||
links: {
|
||||
post: "1",
|
||||
author: nil
|
||||
}
|
||||
}],
|
||||
authors: [{
|
||||
id: "1",
|
||||
name: "Steve K.",
|
||||
links: {
|
||||
posts: ["1", "2"],
|
||||
roles: [],
|
||||
bio: "1"
|
||||
}
|
||||
}],
|
||||
bios: [{
|
||||
id: "1",
|
||||
content: "AMS Contributor",
|
||||
links: {
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
}
|
||||
assert_equal expected, @adapter.serializable_hash[:linked]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user