Skip to content

Commit f149e50

Browse files
committed
Merge pull request #1093 from beauby/improve-tests
Factor `with_adapter` + force cache clear before each test.
2 parents c03427d + 890003b commit f149e50

File tree

4 files changed

+74
-64
lines changed

4 files changed

+74
-64
lines changed

test/action_controller/serialization_test.rb

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,48 @@ class ImplicitSerializerTest < ActionController::TestCase
66
include ActiveSupport::Testing::Stream
77
class ImplicitSerializationTestController < ActionController::Base
88
def render_using_implicit_serializer
9-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
1010
render json: @profile
1111
end
1212

1313
def render_using_default_adapter_root
14-
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
15-
# JSON-API adapter sets root by default
16-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
17-
render json: @profile
18-
end
14+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
15+
render json: @profile
1916
end
2017

2118
def render_array_using_custom_root
22-
with_adapter ActiveModel::Serializer::Adapter::Json do
23-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
24-
render json: [@profile], root: 'custom_root'
25-
end
19+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
20+
render json: [@profile], root: 'custom_root'
2621
end
2722

2823
def render_array_that_is_empty_using_custom_root
29-
with_adapter ActiveModel::Serializer::Adapter::Json do
30-
render json: [], root: 'custom_root'
31-
end
24+
render json: [], root: 'custom_root'
3225
end
3326

3427
def render_object_using_custom_root
35-
with_adapter ActiveModel::Serializer::Adapter::Json do
36-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
37-
render json: @profile, root: 'custom_root'
38-
end
28+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
29+
render json: @profile, root: 'custom_root'
3930
end
4031

4132
def render_array_using_implicit_serializer
4233
array = [
43-
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
44-
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
34+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
35+
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
4536
]
4637
render json: array
4738
end
4839

4940
def render_array_using_implicit_serializer_and_meta
50-
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
51-
@profiles = [
52-
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
53-
]
54-
55-
render json: @profiles, meta: { total: 10 }
56-
end
41+
@profiles = [
42+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
43+
]
44+
render json: @profiles, meta: { total: 10 }
5745
end
5846

5947
def render_object_with_cache_enabled
60-
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
48+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
6149
@author = Author.new(id: 1, name: 'Joao Moura.')
62-
@post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author })
50+
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
6351

6452
generate_cached_serializer(@post)
6553

@@ -83,9 +71,9 @@ def update_and_render_object_with_cache_enabled
8371
end
8472

8573
def render_object_expired_with_cache_enabled
86-
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
74+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
8775
author = Author.new(id: 1, name: 'Joao Moura.')
88-
post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
76+
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
8977

9078
generate_cached_serializer(post)
9179

@@ -95,16 +83,16 @@ def render_object_expired_with_cache_enabled
9583
end
9684

9785
def render_changed_object_with_cache_enabled
98-
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
86+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
9987
author = Author.new(id: 1, name: 'Joao Moura.')
100-
post = Post.new({ id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author })
88+
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
10189

10290
render json: post
10391
end
10492

10593
def render_fragment_changed_object_with_only_cache_enabled
10694
author = Author.new(id: 1, name: 'Joao Moura.')
107-
role = Role.new({ id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author })
95+
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
10896

10997
generate_cached_serializer(role)
11098
role.name = 'lol'
@@ -115,7 +103,7 @@ def render_fragment_changed_object_with_only_cache_enabled
115103

116104
def render_fragment_changed_object_with_except_cache_enabled
117105
author = Author.new(id: 1, name: 'Joao Moura.')
118-
bio = Bio.new({ id: 42, content: 'ZOMG A ROLE', rating: 5, author: author })
106+
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
119107

120108
generate_cached_serializer(bio)
121109
bio.content = 'lol'
@@ -125,13 +113,13 @@ def render_fragment_changed_object_with_except_cache_enabled
125113
end
126114

127115
def render_fragment_changed_object_with_relationship
128-
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
129-
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
130-
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })
116+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
117+
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
118+
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
131119

132120
generate_cached_serializer(like)
133121
like.likable = comment2
134-
like.time = Time.now.to_s
122+
like.time = Time.zone.now.to_s
135123

136124
render json: like
137125
end
@@ -168,8 +156,9 @@ def test_render_using_implicit_serializer
168156
end
169157

170158
def test_render_using_default_root
171-
get :render_using_default_adapter_root
172-
159+
with_adapter :json_api do
160+
get :render_using_default_adapter_root
161+
end
173162
expected = {
174163
data: {
175164
id: assigns(:profile).id.to_s,
@@ -186,23 +175,28 @@ def test_render_using_default_root
186175
end
187176

188177
def test_render_array_using_custom_root
189-
get :render_array_using_custom_root
190-
178+
with_adapter :json do
179+
get :render_array_using_custom_root
180+
end
191181
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
192182
assert_equal 'application/json', @response.content_type
193183
assert_equal expected.to_json, @response.body
194184
end
195185

196186
def test_render_array_that_is_empty_using_custom_root
197-
get :render_array_that_is_empty_using_custom_root
187+
with_adapter :json do
188+
get :render_array_that_is_empty_using_custom_root
189+
end
198190

199191
expected = { custom_roots: [] }
200192
assert_equal 'application/json', @response.content_type
201193
assert_equal expected.to_json, @response.body
202194
end
203195

204196
def test_render_object_using_custom_root
205-
get :render_object_using_custom_root
197+
with_adapter :json do
198+
get :render_object_using_custom_root
199+
end
206200

207201
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
208202
assert_equal 'application/json', @response.content_type
@@ -232,20 +226,21 @@ def test_render_array_using_implicit_serializer
232226
expected = [
233227
{
234228
name: 'Name 1',
235-
description: 'Description 1',
229+
description: 'Description 1'
236230
},
237231
{
238232
name: 'Name 2',
239-
description: 'Description 2',
233+
description: 'Description 2'
240234
}
241235
]
242236

243237
assert_equal expected.to_json, @response.body
244238
end
245239

246240
def test_render_array_using_implicit_serializer_and_meta
247-
get :render_array_using_implicit_serializer_and_meta
248-
241+
with_adapter :json_api do
242+
get :render_array_using_implicit_serializer_and_meta
243+
end
249244
expected = {
250245
data: [
251246
{
@@ -287,7 +282,7 @@ def test_render_with_cache_enable
287282
}
288283

289284
ActionController::Base.cache_store.clear
290-
Timecop.freeze(Time.now) do
285+
Timecop.freeze(Time.zone.now) do
291286
get :render_object_with_cache_enabled
292287

293288
assert_equal 'application/json', @response.content_type
@@ -352,13 +347,13 @@ def test_render_with_fragment_except_cache_enable
352347
def test_render_fragment_changed_object_with_relationship
353348
ActionController::Base.cache_store.clear
354349

355-
Timecop.freeze(Time.now) do
350+
Timecop.freeze(Time.zone.now) do
356351
get :render_fragment_changed_object_with_relationship
357352
response = JSON.parse(@response.body)
358353

359354
expected_return = {
360355
'id' => 1,
361-
'time' => Time.now.to_s,
356+
'time' => Time.zone.now.to_s,
362357
'likeable' => {
363358
'id' => 1,
364359
'body' => 'ZOMG A COMMENT'

test/adapter/json_api/resource_type_config_test.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,28 @@ def setup
2828
end
2929

3030
def with_jsonapi_resource_type type
31-
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type]
32-
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
31+
old_type = ActiveModel::Serializer.config.jsonapi_resource_type
32+
ActiveModel::Serializer.config.jsonapi_resource_type = type
3333
yield
3434
ensure
35-
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
35+
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
3636
end
3737

3838
def test_config_plural
39-
with_jsonapi_resource_type :plural do
40-
serializer = CommentSerializer.new(@comment)
41-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
42-
ActionController::Base.cache_store.clear
43-
assert_equal('comments', adapter.serializable_hash[:data][:type])
39+
with_adapter :json_api do
40+
with_jsonapi_resource_type :plural do
41+
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
42+
assert_equal('comments', hash[:data][:type])
43+
end
4444
end
4545
end
4646

4747
def test_config_singular
48-
with_jsonapi_resource_type :singular do
49-
serializer = CommentSerializer.new(@comment)
50-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
51-
ActionController::Base.cache_store.clear
52-
assert_equal('comment', adapter.serializable_hash[:data][:type])
48+
with_adapter :json_api do
49+
with_jsonapi_resource_type :singular do
50+
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
51+
assert_equal('comment', hash[:data][:type])
52+
end
5353
end
5454
end
5555
end

test/support/serialization_testing.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Minitest::Test
2+
def before_setup
3+
ActionController::Base.cache_store.clear
4+
end
5+
6+
def with_adapter(adapter)
7+
old_adapter = ActiveModel::Serializer.config.adapter
8+
ActiveModel::Serializer.config.adapter = adapter
9+
yield
10+
ensure
11+
ActiveModel::Serializer.config.adapter = old_adapter
12+
end
13+
end

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
require 'support/test_case'
4848

49+
require 'support/serialization_testing'
50+
4951
require 'fixtures/active_record'
5052

5153
require 'fixtures/poro'

0 commit comments

Comments
 (0)