@@ -6,60 +6,48 @@ class ImplicitSerializerTest < ActionController::TestCase
6
6
include ActiveSupport ::Testing ::Stream
7
7
class ImplicitSerializationTestController < ActionController ::Base
8
8
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' )
10
10
render json : @profile
11
11
end
12
12
13
13
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
19
16
end
20
17
21
18
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'
26
21
end
27
22
28
23
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'
32
25
end
33
26
34
27
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'
39
30
end
40
31
41
32
def render_array_using_implicit_serializer
42
33
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' )
45
36
]
46
37
render json : array
47
38
end
48
39
49
40
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 }
57
45
end
58
46
59
47
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' )
61
49
@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 )
63
51
64
52
generate_cached_serializer ( @post )
65
53
@@ -83,9 +71,9 @@ def update_and_render_object_with_cache_enabled
83
71
end
84
72
85
73
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' )
87
75
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 )
89
77
90
78
generate_cached_serializer ( post )
91
79
@@ -95,16 +83,16 @@ def render_object_expired_with_cache_enabled
95
83
end
96
84
97
85
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' )
99
87
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 )
101
89
102
90
render json : post
103
91
end
104
92
105
93
def render_fragment_changed_object_with_only_cache_enabled
106
94
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 )
108
96
109
97
generate_cached_serializer ( role )
110
98
role . name = 'lol'
@@ -115,7 +103,7 @@ def render_fragment_changed_object_with_only_cache_enabled
115
103
116
104
def render_fragment_changed_object_with_except_cache_enabled
117
105
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 )
119
107
120
108
generate_cached_serializer ( bio )
121
109
bio . content = 'lol'
@@ -125,13 +113,13 @@ def render_fragment_changed_object_with_except_cache_enabled
125
113
end
126
114
127
115
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 )
131
119
132
120
generate_cached_serializer ( like )
133
121
like . likable = comment2
134
- like . time = Time . now . to_s
122
+ like . time = Time . zone . now . to_s
135
123
136
124
render json : like
137
125
end
@@ -168,8 +156,9 @@ def test_render_using_implicit_serializer
168
156
end
169
157
170
158
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
173
162
expected = {
174
163
data : {
175
164
id : assigns ( :profile ) . id . to_s ,
@@ -186,23 +175,28 @@ def test_render_using_default_root
186
175
end
187
176
188
177
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
191
181
expected = { custom_roots : [ { name : 'Name 1' , description : 'Description 1' } ] }
192
182
assert_equal 'application/json' , @response . content_type
193
183
assert_equal expected . to_json , @response . body
194
184
end
195
185
196
186
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
198
190
199
191
expected = { custom_roots : [ ] }
200
192
assert_equal 'application/json' , @response . content_type
201
193
assert_equal expected . to_json , @response . body
202
194
end
203
195
204
196
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
206
200
207
201
expected = { custom_root : { name : 'Name 1' , description : 'Description 1' } }
208
202
assert_equal 'application/json' , @response . content_type
@@ -232,20 +226,21 @@ def test_render_array_using_implicit_serializer
232
226
expected = [
233
227
{
234
228
name : 'Name 1' ,
235
- description : 'Description 1' ,
229
+ description : 'Description 1'
236
230
} ,
237
231
{
238
232
name : 'Name 2' ,
239
- description : 'Description 2' ,
233
+ description : 'Description 2'
240
234
}
241
235
]
242
236
243
237
assert_equal expected . to_json , @response . body
244
238
end
245
239
246
240
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
249
244
expected = {
250
245
data : [
251
246
{
@@ -287,7 +282,7 @@ def test_render_with_cache_enable
287
282
}
288
283
289
284
ActionController ::Base . cache_store . clear
290
- Timecop . freeze ( Time . now ) do
285
+ Timecop . freeze ( Time . zone . now ) do
291
286
get :render_object_with_cache_enabled
292
287
293
288
assert_equal 'application/json' , @response . content_type
@@ -352,13 +347,13 @@ def test_render_with_fragment_except_cache_enable
352
347
def test_render_fragment_changed_object_with_relationship
353
348
ActionController ::Base . cache_store . clear
354
349
355
- Timecop . freeze ( Time . now ) do
350
+ Timecop . freeze ( Time . zone . now ) do
356
351
get :render_fragment_changed_object_with_relationship
357
352
response = JSON . parse ( @response . body )
358
353
359
354
expected_return = {
360
355
'id' => 1 ,
361
- 'time' => Time . now . to_s ,
356
+ 'time' => Time . zone . now . to_s ,
362
357
'likeable' => {
363
358
'id' => 1 ,
364
359
'body' => 'ZOMG A COMMENT'
0 commit comments