add action test to pagination links

This commit is contained in:
Bruno Bacarini 2015-08-08 11:26:20 -03:00
parent 1fe8b06986
commit e040d6fcce
4 changed files with 274 additions and 179 deletions

View File

@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "timecop", ">= 0.7"
spec.add_development_dependency "rake"
spec.add_development_dependency "kaminari"
spec.add_development_dependency "will_paginate"
end

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,92 @@
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
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]).per(params[:per_page])
end
def using_will_paginate
setup
@array.paginate(page: params[:page], per_page: params[:per_page])
end
def render_pagination_using_kaminari
render json: using_kaminari, adapter: :json_api, pagination: true
end
def render_pagination_using_will_paginate
render json: using_will_paginate, adapter: :json_api, pagination: true
end
def render_array_without_pagination_links
render json: using_will_paginate, adapter: :json_api, pagination: false
end
def render_array_omitting_pagination_options
render json: using_kaminari, adapter: :json_api
end
end
tests PaginationTestController
def test_render_pagination_links_with_will_paginate
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=1&per_page=1", "next"=>"?page=3&per_page=1", "last"=>"?page=3&per_page=1"}
get :render_pagination_using_will_paginate, page: 2, per_page: 1
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_last_and_next_pagination_links
expected_links = {"next"=>"?page=2&per_page=2", "last"=>"?page=2&per_page=2"}
get :render_pagination_using_will_paginate, page: 1, per_page: 2
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_pagination_links_with_kaminari
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=1&per_page=1", "next"=>"?page=3&per_page=1", "last"=>"?page=3&per_page=1"}
get :render_pagination_using_kaminari, page: 2, per_page: 1
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_render_only_prev_and_first_pagination_links
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=2&per_page=1"}
get :render_pagination_using_kaminari, page: 3, per_page: 1
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end
def test_array_without_pagination_links
get :render_array_without_pagination_links
response = JSON.parse(@response.body)
refute response.key? 'links'
end
def test_array_omitting_pagination_options
get :render_array_omitting_pagination_options
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