mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
@@ -45,6 +45,18 @@ module ActiveModel
|
||||
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]})
|
||||
expected = [{
|
||||
title: 'New Post',
|
||||
links: {
|
||||
comments: ["1"],
|
||||
author: "1"
|
||||
}
|
||||
}]
|
||||
assert_equal expected, @adapter.serializable_hash[:linked][:posts]
|
||||
end
|
||||
|
||||
def test_include_nil_author
|
||||
serializer = PostSerializer.new(@anonymous_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
@@ -26,6 +26,15 @@ module ActiveModel
|
||||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], author: "1" } }
|
||||
], @adapter.serializable_hash[:posts])
|
||||
end
|
||||
|
||||
def test_limiting_fields
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, fields: ['title'])
|
||||
assert_equal([
|
||||
{ title: "Hello!!", links: { comments: [], author: "1" } },
|
||||
{ title: "New Post", links: { comments: [], author: "1" } }
|
||||
], @adapter.serializable_hash[:posts])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,7 +32,7 @@ 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')
|
||||
expected = [{
|
||||
@@ -53,6 +53,24 @@ module ActiveModel
|
||||
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: [: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
|
||||
serializer = PostSerializer.new(@post_without_comments)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
|
||||
@@ -12,7 +12,11 @@ module ActiveModel
|
||||
assert_equal([:name, :description],
|
||||
@profile_serializer.class._attributes)
|
||||
end
|
||||
|
||||
def test_attributes_with_fields_option
|
||||
assert_equal({name: 'Name 1'},
|
||||
@profile_serializer.attributes( { fields: [:name] } ) )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
26
test/serializers/fieldset_test.rb
Normal file
26
test/serializers/fieldset_test.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class FieldsetTest < Minitest::Test
|
||||
|
||||
def test_fieldset_with_hash
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']})
|
||||
|
||||
assert_equal(
|
||||
{:post=>[:id, :title], :coment=>[:body]},
|
||||
fieldset.fields
|
||||
)
|
||||
end
|
||||
|
||||
def test_fieldset_with_array_of_fields_and_root_name
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
||||
|
||||
assert_equal(
|
||||
{:post => [:title]},
|
||||
fieldset.fields
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user