Merge pull request #1041 from bacarini/master

Adding pagination links
This commit is contained in:
João Moura
2015-08-22 13:38:32 -03:00
13 changed files with 614 additions and 181 deletions

View File

@@ -0,0 +1,180 @@
require 'test_helper'
module ActionController
module Serialization
class JsonApi
class LinkedTest < ActionController::TestCase
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, 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 LinkedTestController
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
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_nested_include
get :render_resource_with_nested_include
response = JSON.parse(@response.body)
assert response.key? 'included'
assert_equal 1, response['included'].size
assert_equal 'Anonymous', response['included'].first['attributes']['name']
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 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? 'included'
assert has_type?(response['included'], 'roles')
end
def has_type?(collection, value)
collection.detect { |i| i['type'] == value}
end
end
end
end
end

View File

@@ -0,0 +1,116 @@
require 'test_helper'
require 'will_paginate/array'
require 'kaminari'
require 'kaminari/hooks'
::Kaminari::Hooks.init
module ActionController
module Serialization
class JsonApi
class PaginationTest < ActionController::TestCase
KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'
WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'
class PaginationTestController < ActionController::Base
def setup
@array = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
]
end
def using_kaminari
setup
Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
end
def using_will_paginate
setup
@array.paginate(page: params[:page][:number], per_page: params[:page][:size])
end
def render_pagination_using_kaminari
render json: using_kaminari, adapter: :json_api
end
def render_pagination_using_will_paginate
render json: using_will_paginate, adapter: :json_api
end
def render_array_without_pagination_links
setup
render json: @array, adapter: :json_api
end
end
tests PaginationTestController
def test_render_pagination_links_with_will_paginate
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
"first"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"prev"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_last_and_next_pagination_links
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"}
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_pagination_links_with_kaminari
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"next"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
"last"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_prev_and_first_pagination_links
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"}
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_last_and_next_pagination_links_with_additional_params
expected_links = { "self"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
"next"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
"last"=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"}
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: "additional"
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_prev_and_first_pagination_links_with_additional_params
expected_links = { "self"=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
"first"=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
"prev"=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"}
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: "additional"
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_array_without_pagination_links
get :render_array_without_pagination_links, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
refute response.key? 'links'
end
end
end
end
end

View File

@@ -1,179 +0,0 @@
require 'test_helper'
module ActionController
module Serialization
class JsonApiLinkedTest < ActionController::TestCase
class JsonApiLinkedTestController < 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, 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 JsonApiLinkedTestController
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
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_nested_include
get :render_resource_with_nested_include
response = JSON.parse(@response.body)
assert response.key? 'included'
assert_equal 1, response['included'].size
assert_equal 'Anonymous', response['included'].first['attributes']['name']
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 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? '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

@@ -0,0 +1,115 @@
require 'test_helper'
require 'will_paginate/array'
require 'kaminari'
require 'kaminari/hooks'
::Kaminari::Hooks.init
module ActiveModel
class Serializer
class Adapter
class JsonApi
class PaginationLinksTest < Minitest::Test
URI = 'http://example.com'
def setup
ActionController::Base.cache_store.clear
@array = [
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
]
end
def mock_request(query_parameters={}, original_url=URI)
context = Minitest::Mock.new
context.expect(:original_url, original_url )
context.expect(:query_parameters, query_parameters)
@options = {}
@options[:context] = context
end
def load_adapter(paginated_collection, options = {})
options = options.merge(adapter: :json_api)
ActiveModel::SerializableResource.new(paginated_collection, options)
end
def using_kaminari
Kaminari.paginate_array(@array).page(2).per(1)
end
def using_will_paginate
@array.paginate(page: 2, per_page: 1)
end
def data
{ data:[
{ id:"1", type:"profiles", attributes:{name:"Name 1", description:"Description 1" } },
{ id:"2", type:"profiles", attributes:{name:"Name 2", description:"Description 2" } },
{ id:"3", type:"profiles", attributes:{name:"Name 3", description:"Description 3" } }
]
}
end
def links
{
links:{
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
}
}
end
def expected_response_without_pagination_links
data
end
def expected_response_with_pagination_links
{}.tap do |hash|
hash[:data] = [data.values.flatten.second]
hash.merge! links
end
end
def expected_response_with_pagination_links_and_additional_params
new_links = links[:links].each_with_object({}) {|(key, value), hash| hash[key] = "#{value}&test=test" }
{}.tap do |hash|
hash[:data] = [data.values.flatten.second]
hash.merge! links: new_links
end
end
def test_pagination_links_using_kaminari
adapter = load_adapter(using_kaminari)
mock_request
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
end
def test_pagination_links_using_will_paginate
adapter = load_adapter(using_will_paginate)
mock_request
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
end
def test_pagination_links_with_additional_params
adapter = load_adapter(using_will_paginate)
mock_request({ test: 'test' })
assert_equal expected_response_with_pagination_links_and_additional_params,
adapter.serializable_hash(@options)
end
def test_not_showing_pagination_links
adapter = load_adapter(@array)
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
end
end
end
end
end
end