mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
implement sparse fieldsets http://jsonapi.org/format/#fetching-sparse-fieldsets
This commit is contained in:
56
test/adapter/json_api/fieldset_test.rb
Normal file
56
test/adapter/json_api/fieldset_test.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Adapter
|
||||
class JsonApi
|
||||
class FieldsetTest < Minitest::Test
|
||||
def setup
|
||||
@post = Post.new(title: 'New Post', body: 'Body')
|
||||
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
||||
@post.comments = [@first_comment, @second_comment]
|
||||
@first_comment.post = @post
|
||||
@second_comment.post = @post
|
||||
|
||||
@serializer = PostSerializer.new(@post)
|
||||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
||||
end
|
||||
|
||||
def teardown
|
||||
@serializer = nil
|
||||
@adapter = nil
|
||||
end
|
||||
|
||||
def test_fieldset_with_fields_array
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, ['title'])
|
||||
|
||||
assert_equal(
|
||||
{:title=>"New Post", :links=>{:comments=>["1", "2"]}},
|
||||
@adapter.serializable_hash({fieldset: fieldset})[:posts]
|
||||
)
|
||||
end
|
||||
|
||||
def test_fieldset_with_hash
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, {post: [:body]})
|
||||
|
||||
assert_equal(
|
||||
{:body=>"Body", :links=>{:comments=>["1", "2"]}},
|
||||
@adapter.serializable_hash({fieldset: fieldset})[:posts]
|
||||
)
|
||||
end
|
||||
|
||||
def test_fieldset_with_multiple_hashes
|
||||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, {post: [:title], comment: [:body]})
|
||||
|
||||
assert_equal(
|
||||
[{:body=>"ZOMG A COMMENT" }, {:body=>"ZOMG ANOTHER COMMENT"}],
|
||||
@adapter.serializable_hash({fieldset: fieldset})[:linked][:comments]
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user