1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
# frozen_string_literal: true
require 'test_helper'
module ActionController
module Serialization
class JsonApi
class LinkedTest < ActionDispatch::IntegrationTest
class LinkedTestController < ActionController::Base
def setup_post
ActionController::Base.cache_store.clear
@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 = []
@blog = Blog.new(id: 1, name: 'My Blog!!')
@post.blog = @blog
@post2.blog = @blog
end
def render_resource_without_include
setup_post
render json: @post
end
def render_resource_with_include
setup_post
render json: @post, adapter: :json_api, include: [:author]
end
def render_resource_with_include_of_custom_key_by_original
setup_post
render json: @post, adapter: :json_api, include: [:reviews], serializer: PostWithCustomKeysSerializer
end
def render_resource_with_nested_include
setup_post
render json: @post, adapter: :json_api, include: [comments: [:author]]
end
def render_resource_with_nested_has_many_include_wildcard
setup_post
render json: @post, adapter: :json_api, include: 'author.*'
end
def render_resource_with_missing_nested_has_many_include
setup_post
@post.author = @author2 # author2 has no roles.
render json: @post, adapter: :json_api, include: [author: [:roles]]
end
def render_collection_with_missing_nested_has_many_include
setup_post
@post.author = @author2
render json: [@post, @post2], adapter: :json_api, include: [author: [:roles]]
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], adapter: :json_api, include: 'author,comments'
end
end
setup do
@routes = Rails.application.routes.draw do
ActiveSupport::Deprecation.silence do
match ':action', to: LinkedTestController, via: [:get, :post]
end
end
end
def test_render_resource_without_include
get '/render_resource_without_include'
response = JSON.parse(@response.body)
refute response.key? 'included'
end
def test_render_resource_with_include
get '/render_resource_with_include'
response = JSON.parse(@response.body)
assert response.key? 'included'
assert_equal 1, response['included'].size
assert_equal 'Steve K.', response['included'].first['attributes']['name']
end
def test_render_resource_with_nested_has_many_include
get '/render_resource_with_nested_has_many_include_wildcard'
response = JSON.parse(@response.body)
expected_linked = [
{
'id' => '1',
'type' => 'authors',
'attributes' => {
'name' => 'Steve K.'
},
'relationships' => {
'posts' => { 'data' => [] },
'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
'bio' => { 'data' => nil }
}
}, {
'id' => '1',
'type' => 'roles',
'attributes' => {
'name' => 'admin',
'description' => nil,
'slug' => 'admin-1'
},
'relationships' => {
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
}
}, {
'id' => '2',
'type' => 'roles',
'attributes' => {
'name' => 'colab',
'description' => nil,
'slug' => 'colab-2'
},
'relationships' => {
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
}
}
]
assert_equal expected_linked, response['included']
end
def test_render_resource_with_include_of_custom_key_by_original
get '/render_resource_with_include_of_custom_key_by_original'
response = JSON.parse(@response.body)
assert response.key? 'included'
relationships = response['data']['relationships']
assert_includes relationships, 'reviews'
assert_includes relationships, 'writer'
assert_includes relationships, 'site'
end
def test_render_resource_with_nested_include
get '/render_resource_with_nested_include'
response = JSON.parse(@response.body)
assert response.key? 'included'
assert_equal 3, response['included'].size
end
def test_render_collection_without_include
get '/render_collection_without_include'
response = JSON.parse(@response.body)
refute response.key? 'included'
end
def test_render_collection_with_include
get '/render_collection_with_include'
response = JSON.parse(@response.body)
assert response.key? 'included'
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? 'included'
refute include_type?(response['included'], '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? 'included'
assert include_type?(response['included'], 'roles')
end
def include_type?(collection, value)
collection.detect { |i| i['type'] == value }
end
end
end
end
end
|