Skip to content

Commit 566442a

Browse files
committed
add action test to pagination links
1 parent 36d2140 commit 566442a

File tree

4 files changed

+274
-179
lines changed

4 files changed

+274
-179
lines changed

active_model_serializers.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency "bundler", "~> 1.6"
2525
spec.add_development_dependency "timecop", ">= 0.7"
2626
spec.add_development_dependency "rake"
27+
spec.add_development_dependency "kaminari"
28+
spec.add_development_dependency "will_paginate"
2729
end
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
require 'test_helper'
2+
require 'will_paginate/array'
3+
require 'kaminari'
4+
require 'kaminari/hooks'
5+
::Kaminari::Hooks.init
6+
7+
module ActionController
8+
module Serialization
9+
class JsonApi
10+
class PaginationTest < ActionController::TestCase
11+
class PaginationTestController < ActionController::Base
12+
def setup
13+
@array = [
14+
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
15+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
16+
Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
17+
]
18+
end
19+
20+
def using_kaminari
21+
setup
22+
Kaminari.paginate_array(@array).page(params[:page]).per(params[:per_page])
23+
end
24+
25+
def using_will_paginate
26+
setup
27+
@array.paginate(page: params[:page], per_page: params[:per_page])
28+
end
29+
30+
def render_pagination_using_kaminari
31+
render json: using_kaminari, adapter: :json_api, pagination: true
32+
end
33+
34+
def render_pagination_using_will_paginate
35+
render json: using_will_paginate, adapter: :json_api, pagination: true
36+
end
37+
38+
def render_array_without_pagination_links
39+
render json: using_will_paginate, adapter: :json_api, pagination: false
40+
end
41+
42+
def render_array_omitting_pagination_options
43+
render json: using_kaminari, adapter: :json_api
44+
end
45+
end
46+
47+
tests PaginationTestController
48+
49+
def test_render_pagination_links_with_will_paginate
50+
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"}
51+
52+
get :render_pagination_using_will_paginate, page: 2, per_page: 1
53+
response = JSON.parse(@response.body)
54+
assert_equal expected_links, response['links']
55+
end
56+
57+
def test_render_only_last_and_next_pagination_links
58+
expected_links = {"next"=>"?page=2&per_page=2", "last"=>"?page=2&per_page=2"}
59+
get :render_pagination_using_will_paginate, page: 1, per_page: 2
60+
response = JSON.parse(@response.body)
61+
assert_equal expected_links, response['links']
62+
end
63+
64+
def test_render_pagination_links_with_kaminari
65+
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"}
66+
get :render_pagination_using_kaminari, page: 2, per_page: 1
67+
response = JSON.parse(@response.body)
68+
assert_equal expected_links, response['links']
69+
end
70+
71+
def test_render_only_prev_and_first_pagination_links
72+
expected_links = {"first"=>"?page=1&per_page=1", "prev"=>"?page=2&per_page=1"}
73+
get :render_pagination_using_kaminari, page: 3, per_page: 1
74+
response = JSON.parse(@response.body)
75+
assert_equal expected_links, response['links']
76+
end
77+
78+
def test_array_without_pagination_links
79+
get :render_array_without_pagination_links
80+
response = JSON.parse(@response.body)
81+
refute response.key? 'links'
82+
end
83+
84+
def test_array_omitting_pagination_options
85+
get :render_array_omitting_pagination_options
86+
response = JSON.parse(@response.body)
87+
refute response.key? 'links'
88+
end
89+
end
90+
end
91+
end
92+
end

0 commit comments

Comments
 (0)