Implement included and id and type as per spec

This commit is contained in:
Mateo Murphy
2015-03-20 16:04:33 -04:00
parent d82c599c68
commit 33f3a88ba0
12 changed files with 203 additions and 130 deletions

View File

@@ -29,7 +29,17 @@ module ActionController
def test_render_using_adapter_override
get :render_using_adapter_override
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', response.body
expected = {
data: {
name: "Name 1",
description: "Description 1",
id: assigns(:profile).id.to_s,
type: "profiles"
}
}
assert_equal expected.to_json, response.body
end
def test_render_skipping_adapter

View File

@@ -83,80 +83,87 @@ module ActionController
def test_render_resource_without_include
get :render_resource_without_include
response = JSON.parse(@response.body)
refute response.key? 'linked'
refute response.key? 'included'
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']
assert response.key? 'included'
assert_equal 1, response['included'].size
assert_equal 'Steve K.', response['included'].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" => [{
expected_linked = [
{
"id" => "1",
"type" => "authors",
"name" => "Steve K.",
"links" => {
"posts" => { "linkage" => [] },
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
"bio" => { "linkage" => nil }
}
}],
"roles"=>[{
}, {
"id" => "1",
"type" => "roles",
"name" => "admin",
"links" => {
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
}
}, {
"id" => "2",
"type" => "roles",
"name" => "colab",
"links" => {
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
}
}]
}
assert_equal expected_linked, response['linked']
}
]
assert_equal expected_linked, response['included']
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']
assert response.key? 'included'
assert_equal 1, response['included'].size
assert_equal 'Anonymous', response['included'].first['name']
end
def test_render_collection_without_include
get :render_collection_without_include
response = JSON.parse(@response.body)
refute response.key? 'linked'
refute response.key? 'included'
end
def test_render_collection_with_include
get :render_collection_with_include
response = JSON.parse(@response.body)
assert response.key? 'linked'
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? 'linked'
refute response['linked'].key? 'roles'
assert response.key? 'included'
refute has_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? 'linked'
assert response['linked'].key? 'roles'
assert response.key? 'included'
assert has_type?(response['included'], 'roles')
end
def has_type?(collection, value)
collection.detect { |i| i['type'] == value}
end
end
end
end

View File

@@ -2,7 +2,7 @@ require 'test_helper'
require 'pathname'
class DefaultScopeNameTest < ActionController::TestCase
TestUser = Struct.new(:name, :admin)
TestUser = Struct.new(:id, :name, :admin)
class UserSerializer < ActiveModel::Serializer
attributes :admin?
@@ -17,11 +17,11 @@ class DefaultScopeNameTest < ActionController::TestCase
before_filter { request.format = :json }
def current_user
TestUser.new('Pete', false)
TestUser.new(1, 'Pete', false)
end
def render_new_user
render json: TestUser.new('pete', false), serializer: UserSerializer, adapter: :json_api
render json: TestUser.new(1, 'pete', false), serializer: UserSerializer, adapter: :json_api
end
end
@@ -29,12 +29,12 @@ class DefaultScopeNameTest < ActionController::TestCase
def test_default_scope_name
get :render_new_user
assert_equal '{"data":{"admin?":false}}', @response.body
assert_equal '{"data":{"admin?":false,"id":"1","type":"test_users"}}', @response.body
end
end
class SerializationScopeNameTest < ActionController::TestCase
TestUser = Struct.new(:name, :admin)
TestUser = Struct.new(:id, :name, :admin)
class AdminUserSerializer < ActiveModel::Serializer
attributes :admin?
@@ -50,11 +50,11 @@ class SerializationScopeNameTest < ActionController::TestCase
before_filter { request.format = :json }
def current_admin
TestUser.new('Bob', true)
TestUser.new(1, 'Bob', true)
end
def render_new_user
render json: TestUser.new('pete', false), serializer: AdminUserSerializer, adapter: :json_api
render json: TestUser.new(1, 'pete', false), serializer: AdminUserSerializer, adapter: :json_api
end
end
@@ -62,6 +62,6 @@ class SerializationScopeNameTest < ActionController::TestCase
def test_override_scope_name_with_controller
get :render_new_user
assert_equal '{"data":{"admin?":true}}', @response.body
assert_equal '{"data":{"admin?":true,"id":"1","type":"test_users"}}', @response.body
end
end

View File

@@ -15,13 +15,11 @@ module ActionController
end
def render_using_default_adapter_root
old_adapter = ActiveModel::Serializer.config.adapter
# JSON-API adapter sets root by default
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
ensure
ActiveModel::Serializer.config.adapter = old_adapter
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
# JSON-API adapter sets root by default
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
end
end
def render_using_custom_root_in_adapter_with_a_default
@@ -39,15 +37,14 @@ module ActionController
end
def render_array_using_implicit_serializer_and_meta
old_adapter = ActiveModel::Serializer.config.adapter
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]
render json: array, meta: { total: 10 }
ensure
ActiveModel::Serializer.config.adapter = old_adapter
@profiles = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]
render json: @profiles, meta: { total: 10 }
end
end
def render_object_with_cache_enabled
@@ -88,6 +85,15 @@ module ActionController
adapter = ActiveModel::Serializer.adapter.new(serializer)
adapter.to_json
end
def with_adapter(adapter)
old_adapter = ActiveModel::Serializer.config.adapter
# JSON-API adapter sets root by default
ActiveModel::Serializer.config.adapter = adapter
yield
ensure
ActiveModel::Serializer.config.adapter = old_adapter
end
end
tests MyController
@@ -96,8 +102,13 @@ module ActionController
def test_render_using_implicit_serializer
get :render_using_implicit_serializer
expected = {
name: "Name 1",
description: "Description 1"
}
assert_equal 'application/json', @response.content_type
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
assert_equal expected.to_json, @response.body
end
def test_render_using_custom_root
@@ -110,15 +121,33 @@ module ActionController
def test_render_using_default_root
get :render_using_default_adapter_root
expected = {
data: {
name: "Name 1",
description: "Description 1",
id: assigns(:profile).id.to_s,
type: "profiles"
}
}
assert_equal 'application/json', @response.content_type
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body
assert_equal expected.to_json, @response.body
end
def test_render_using_custom_root_in_adapter_with_a_default
get :render_using_custom_root_in_adapter_with_a_default
expected = {
data: {
name: "Name 1",
description: "Description 1",
id: assigns(:profile).id.to_s,
type: "profiles"
}
}
assert_equal 'application/json', @response.content_type
assert_equal '{"data":{"name":"Name 1","description":"Description 1"}}', @response.body
assert_equal expected.to_json, @response.body
end
def test_render_array_using_implicit_serializer
@@ -142,8 +171,22 @@ module ActionController
def test_render_array_using_implicit_serializer_and_meta
get :render_array_using_implicit_serializer_and_meta
expected = {
data: [
{
name: "Name 1",
description: "Description 1",
id: assigns(:profiles).first.id.to_s,
type: "profiles"
}
],
meta: {
total: 10
}
}
assert_equal 'application/json', @response.content_type
assert_equal '{"data":[{"name":"Name 1","description":"Description 1"}],"meta":{"total":10}}', @response.body
assert_equal expected.to_json, @response.body
end
def test_render_with_cache_enable