active_model_serializers/test/action_controller/json_api_linked_test.rb
Nicolás Hock Isaza 1d7d9fd6aa Fix nested include attributes
When the requests asked for a nested attribute in the `include` and it's
missing or empty, don't break because the type of the object can't be
determined.

If the request is for a collection and none of the elements has the
attribute, it will not be added to the `linked` key, similar to what
happens with simple includes.
2015-01-13 14:49:58 -05:00

159 lines
5.2 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
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
@post2.author = @author
@post2.comments = []
end
def render_resource_without_include
setup_post
render json: @post, adapter: :json_api
end
def render_resource_with_include
setup_post
render json: @post, include: 'author', adapter: :json_api
end
def render_resource_with_nested_include
setup_post
render json: @post, include: 'comments.author', adapter: :json_api
end
def render_resource_with_nested_has_many_include
setup_post
render json: @post, include: 'author,author.roles', adapter: :json_api
end
def render_resource_with_missing_nested_has_many_include
setup_post
@post.author = @author2 # author2 has no roles.
render json: @post, include: 'author,author.roles', adapter: :json_api
end
def render_collection_with_missing_nested_has_many_include
setup_post
@post.author = @author2
render json: [@post, @post2], include: 'author,author.roles', adapter: :json_api
end
def render_collection_without_include
setup_post
render json: [@post], adapter: :json_api
end
def render_collection_with_include
setup_post
render json: [@post], include: 'author,comments', adapter: :json_api
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
def test_render_resource_with_nested_attributes_even_when_missing_associations
get :render_resource_with_missing_nested_has_many_include
response = JSON.parse(@response.body)
assert response.key? 'linked'
refute response['linked'].key? 'roles'
end
def test_render_collection_with_missing_nested_has_many_include
get :render_collection_with_missing_nested_has_many_include
response = JSON.parse(@response.body)
assert response.key? 'linked'
assert response['linked'].key? 'roles'
end
end
end
end