mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
According to http://jsonapi.org/format/#document-structure-resource-objects, > Resource objects have the same internal structure, regardless of > whether they represent primary or linked resources. And then http://jsonapi.org/format/#document-structure-resource-object-attributes, > There are four reserved keys in resource objects: > > "id" > "type" > "href" > "links" This commits includes `links` inside of linked resources.
150 lines
4.5 KiB
Ruby
150 lines
4.5 KiB
Ruby
require 'test_helper'
|
|
|
|
module ActionController
|
|
module Serialization
|
|
class JsonApiLinkedTest < ActionController::TestCase
|
|
class MyController < ActionController::Base
|
|
def setup_post
|
|
@role1 = Role.new(id: 1, name: 'admin')
|
|
@role2 = Role.new(id: 2, name: 'colab')
|
|
@author = Author.new(id: 1, name: 'Steve K.')
|
|
@author.posts = []
|
|
@author.bio = nil
|
|
@author.roles = [@role1, @role2]
|
|
@role1.author = @author
|
|
@role2.author = @author
|
|
@author2 = Author.new(id: 2, name: 'Anonymous')
|
|
@author2.posts = []
|
|
@author2.bio = nil
|
|
@author2.roles = []
|
|
@post = Post.new(id: 1, 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]
|
|
@post.author = @author
|
|
@first_comment.post = @post
|
|
@first_comment.author = @author2
|
|
@second_comment.post = @post
|
|
@second_comment.author = nil
|
|
end
|
|
|
|
def with_json_api_adapter
|
|
old_adapter = ActiveModel::Serializer.config.adapter
|
|
ActiveModel::Serializer.config.adapter = :json_api
|
|
yield
|
|
ensure
|
|
ActiveModel::Serializer.config.adapter = old_adapter
|
|
end
|
|
|
|
def render_resource_without_include
|
|
with_json_api_adapter do
|
|
setup_post
|
|
render json: @post
|
|
end
|
|
end
|
|
|
|
def render_resource_with_include
|
|
with_json_api_adapter do
|
|
setup_post
|
|
render json: @post, include: 'author'
|
|
end
|
|
end
|
|
|
|
def render_resource_with_nested_include
|
|
with_json_api_adapter do
|
|
setup_post
|
|
render json: @post, include: 'comments.author'
|
|
end
|
|
end
|
|
|
|
def render_resource_with_nested_has_many_include
|
|
with_json_api_adapter do
|
|
setup_post
|
|
render json: @post, include: 'author,author.roles'
|
|
end
|
|
end
|
|
|
|
def render_collection_without_include
|
|
with_json_api_adapter do
|
|
setup_post
|
|
render json: [@post]
|
|
end
|
|
end
|
|
|
|
def render_collection_with_include
|
|
with_json_api_adapter do
|
|
setup_post
|
|
render json: [@post], include: 'author,comments'
|
|
end
|
|
end
|
|
end
|
|
|
|
tests MyController
|
|
|
|
def test_render_resource_without_include
|
|
get :render_resource_without_include
|
|
response = JSON.parse(@response.body)
|
|
refute response.key? 'linked'
|
|
end
|
|
|
|
def test_render_resource_with_include
|
|
get :render_resource_with_include
|
|
response = JSON.parse(@response.body)
|
|
assert response.key? 'linked'
|
|
assert_equal 1, response['linked']['authors'].size
|
|
assert_equal 'Steve K.', response['linked']['authors'].first['name']
|
|
end
|
|
|
|
def test_render_resource_with_nested_has_many_include
|
|
get :render_resource_with_nested_has_many_include
|
|
response = JSON.parse(@response.body)
|
|
expected_linked = {
|
|
"authors" => [{
|
|
"id" => "1",
|
|
"name" => "Steve K.",
|
|
"links" => {
|
|
"posts" => [],
|
|
"roles" => ["1", "2"],
|
|
"bio" => nil
|
|
}
|
|
}],
|
|
"roles"=>[{
|
|
"id" => "1",
|
|
"name" => "admin",
|
|
"links" => {
|
|
"author" => "1"
|
|
}
|
|
}, {
|
|
"id" => "2",
|
|
"name" => "colab",
|
|
"links" => {
|
|
"author" => "1"
|
|
}
|
|
}]
|
|
}
|
|
assert_equal expected_linked, response['linked']
|
|
end
|
|
|
|
def test_render_resource_with_nested_include
|
|
get :render_resource_with_nested_include
|
|
response = JSON.parse(@response.body)
|
|
assert response.key? 'linked'
|
|
assert_equal 1, response['linked']['authors'].size
|
|
assert_equal 'Anonymous', response['linked']['authors'].first['name']
|
|
end
|
|
|
|
def test_render_collection_without_include
|
|
get :render_collection_without_include
|
|
response = JSON.parse(@response.body)
|
|
refute response.key? 'linked'
|
|
end
|
|
|
|
def test_render_collection_with_include
|
|
get :render_collection_with_include
|
|
response = JSON.parse(@response.body)
|
|
assert response.key? 'linked'
|
|
end
|
|
end
|
|
end
|
|
end
|