|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActionController |
| 4 | + module Serialization |
| 5 | + class JsonApi |
| 6 | + class LinkedTest < ActionController::TestCase |
| 7 | + class LinkedTestController < ActionController::Base |
| 8 | + def setup_post |
| 9 | + ActionController::Base.cache_store.clear |
| 10 | + @role1 = Role.new(id: 1, name: 'admin') |
| 11 | + @role2 = Role.new(id: 2, name: 'colab') |
| 12 | + @author = Author.new(id: 1, name: 'Steve K.') |
| 13 | + @author.posts = [] |
| 14 | + @author.bio = nil |
| 15 | + @author.roles = [@role1, @role2] |
| 16 | + @role1.author = @author |
| 17 | + @role2.author = @author |
| 18 | + @author2 = Author.new(id: 2, name: 'Anonymous') |
| 19 | + @author2.posts = [] |
| 20 | + @author2.bio = nil |
| 21 | + @author2.roles = [] |
| 22 | + @post = Post.new(id: 1, title: 'New Post', body: 'Body') |
| 23 | + @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') |
| 24 | + @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT') |
| 25 | + @post.comments = [@first_comment, @second_comment] |
| 26 | + @post.author = @author |
| 27 | + @first_comment.post = @post |
| 28 | + @first_comment.author = @author2 |
| 29 | + @second_comment.post = @post |
| 30 | + @second_comment.author = nil |
| 31 | + @post2 = Post.new(id: 2, title: "Another Post", body: "Body") |
| 32 | + @post2.author = @author |
| 33 | + @post2.comments = [] |
| 34 | + @blog = Blog.new(id: 1, name: "My Blog!!") |
| 35 | + @post.blog = @blog |
| 36 | + @post2.blog = @blog |
| 37 | + end |
| 38 | + |
| 39 | + def render_resource_without_include |
| 40 | + setup_post |
| 41 | + render json: @post, adapter: :json_api |
| 42 | + end |
| 43 | + |
| 44 | + def render_resource_with_include |
| 45 | + setup_post |
| 46 | + render json: @post, include: 'author', adapter: :json_api |
| 47 | + end |
| 48 | + |
| 49 | + def render_resource_with_nested_include |
| 50 | + setup_post |
| 51 | + render json: @post, include: 'comments.author', adapter: :json_api |
| 52 | + end |
| 53 | + |
| 54 | + def render_resource_with_nested_has_many_include |
| 55 | + setup_post |
| 56 | + render json: @post, include: ['author', 'author.roles'], adapter: :json_api |
| 57 | + end |
| 58 | + |
| 59 | + def render_resource_with_missing_nested_has_many_include |
| 60 | + setup_post |
| 61 | + @post.author = @author2 # author2 has no roles. |
| 62 | + render json: @post, include: 'author,author.roles', adapter: :json_api |
| 63 | + end |
| 64 | + |
| 65 | + def render_collection_with_missing_nested_has_many_include |
| 66 | + setup_post |
| 67 | + @post.author = @author2 |
| 68 | + render json: [@post, @post2], include: 'author,author.roles', adapter: :json_api |
| 69 | + end |
| 70 | + |
| 71 | + def render_collection_without_include |
| 72 | + setup_post |
| 73 | + render json: [@post], adapter: :json_api |
| 74 | + end |
| 75 | + |
| 76 | + def render_collection_with_include |
| 77 | + setup_post |
| 78 | + render json: [@post], include: ['author', 'comments'], adapter: :json_api |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + tests LinkedTestController |
| 83 | + |
| 84 | + def test_render_resource_without_include |
| 85 | + get :render_resource_without_include |
| 86 | + response = JSON.parse(@response.body) |
| 87 | + refute response.key? 'included' |
| 88 | + end |
| 89 | + |
| 90 | + def test_render_resource_with_include |
| 91 | + get :render_resource_with_include |
| 92 | + response = JSON.parse(@response.body) |
| 93 | + assert response.key? 'included' |
| 94 | + assert_equal 1, response['included'].size |
| 95 | + assert_equal 'Steve K.', response['included'].first['attributes']['name'] |
| 96 | + end |
| 97 | + |
| 98 | + def test_render_resource_with_nested_has_many_include |
| 99 | + get :render_resource_with_nested_has_many_include |
| 100 | + response = JSON.parse(@response.body) |
| 101 | + expected_linked = [ |
| 102 | + { |
| 103 | + "id" => "1", |
| 104 | + "type" => "authors", |
| 105 | + "attributes" => { |
| 106 | + "name" => "Steve K." |
| 107 | + }, |
| 108 | + "relationships" => { |
| 109 | + "posts" => { "data" => [] }, |
| 110 | + "roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] }, |
| 111 | + "bio" => { "data" => nil } |
| 112 | + } |
| 113 | + }, { |
| 114 | + "id" => "1", |
| 115 | + "type" => "roles", |
| 116 | + "attributes" => { |
| 117 | + "name" => "admin", |
| 118 | + "description" => nil, |
| 119 | + "slug" => "admin-1" |
| 120 | + }, |
| 121 | + "relationships" => { |
| 122 | + "author" => { "data" => { "type" =>"authors", "id" => "1" } } |
| 123 | + } |
| 124 | + }, { |
| 125 | + "id" => "2", |
| 126 | + "type" => "roles", |
| 127 | + "attributes" => { |
| 128 | + "name" => "colab", |
| 129 | + "description" => nil, |
| 130 | + "slug" => "colab-2" |
| 131 | + }, |
| 132 | + "relationships" => { |
| 133 | + "author" => { "data" => { "type" =>"authors", "id" => "1" } } |
| 134 | + } |
| 135 | + } |
| 136 | + ] |
| 137 | + assert_equal expected_linked, response['included'] |
| 138 | + end |
| 139 | + |
| 140 | + def test_render_resource_with_nested_include |
| 141 | + get :render_resource_with_nested_include |
| 142 | + response = JSON.parse(@response.body) |
| 143 | + assert response.key? 'included' |
| 144 | + assert_equal 1, response['included'].size |
| 145 | + assert_equal 'Anonymous', response['included'].first['attributes']['name'] |
| 146 | + end |
| 147 | + |
| 148 | + def test_render_collection_without_include |
| 149 | + get :render_collection_without_include |
| 150 | + response = JSON.parse(@response.body) |
| 151 | + refute response.key? 'included' |
| 152 | + end |
| 153 | + |
| 154 | + def test_render_collection_with_include |
| 155 | + get :render_collection_with_include |
| 156 | + response = JSON.parse(@response.body) |
| 157 | + assert response.key? 'included' |
| 158 | + end |
| 159 | + |
| 160 | + def test_render_resource_with_nested_attributes_even_when_missing_associations |
| 161 | + get :render_resource_with_missing_nested_has_many_include |
| 162 | + response = JSON.parse(@response.body) |
| 163 | + assert response.key? 'included' |
| 164 | + refute has_type?(response['included'], 'roles') |
| 165 | + end |
| 166 | + |
| 167 | + def test_render_collection_with_missing_nested_has_many_include |
| 168 | + get :render_collection_with_missing_nested_has_many_include |
| 169 | + response = JSON.parse(@response.body) |
| 170 | + assert response.key? 'included' |
| 171 | + assert has_type?(response['included'], 'roles') |
| 172 | + end |
| 173 | + |
| 174 | + def has_type?(collection, value) |
| 175 | + collection.detect { |i| i['type'] == value} |
| 176 | + end |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | +end |
0 commit comments