@@ -102,12 +102,13 @@ def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_o
102
102
103
103
render_object_with_cache ( uncached_author )
104
104
key = "#{ uncached_author_serializer . class . _cache_key } /#{ uncached_author_serializer . object . id } -#{ uncached_author_serializer . object . updated_at . strftime ( "%Y%m%d%H%M%S%9N" ) } "
105
+ key = "#{ key } /#{ adapter . cached_name } "
105
106
assert_equal ( uncached_author_serializer . attributes . to_json , cache_store . fetch ( key ) . to_json )
106
107
end
107
108
108
109
def test_default_cache_key_fallback
109
110
render_object_with_cache ( @comment )
110
- key = @comment . cache_key
111
+ key = " #{ @comment . cache_key } / #{ adapter . cached_name } "
111
112
assert_equal ( @comment_serializer . attributes . to_json , cache_store . fetch ( key ) . to_json )
112
113
end
113
114
@@ -138,9 +139,9 @@ def test_associations_separately_cache
138
139
Timecop . freeze ( Time . current ) do
139
140
render_object_with_cache ( @post )
140
141
141
- key = @post . cache_key
142
+ key = " #{ @post . cache_key } / #{ adapter . cached_name } "
142
143
assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
143
- key = @comment . cache_key
144
+ key = " #{ @comment . cache_key } / #{ adapter . cached_name } "
144
145
assert_equal ( @comment_serializer . attributes , cache_store . fetch ( key ) )
145
146
end
146
147
end
@@ -151,9 +152,9 @@ def test_associations_cache_when_updated
151
152
render_object_with_cache ( @post )
152
153
153
154
# Check if it cached the objects separately
154
- key = @post . cache_key
155
+ key = " #{ @post . cache_key } / #{ adapter . cached_name } "
155
156
assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
156
- key = @comment . cache_key
157
+ key = " #{ @comment . cache_key } / #{ adapter . cached_name } "
157
158
assert_equal ( @comment_serializer . attributes , cache_store . fetch ( key ) )
158
159
159
160
# Simulating update on comments relationship with Post
@@ -165,9 +166,9 @@ def test_associations_cache_when_updated
165
166
render_object_with_cache ( @post )
166
167
167
168
# Check if the the new comment was cached
168
- key = new_comment . cache_key
169
+ key = " #{ new_comment . cache_key } / #{ adapter . cached_name } "
169
170
assert_equal ( new_comment_serializer . attributes , cache_store . fetch ( key ) )
170
- key = @post . cache_key
171
+ key = " #{ @post . cache_key } / #{ adapter . cached_name } "
171
172
assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
172
173
end
173
174
end
@@ -183,7 +184,8 @@ def test_fragment_fetch_with_virtual_associations
183
184
hash = render_object_with_cache ( @location )
184
185
185
186
assert_equal ( hash , expected_result )
186
- assert_equal ( { place : 'Nowhere' } , cache_store . fetch ( @location . cache_key ) )
187
+ key = "#{ @location . cache_key } /#{ adapter . cached_name } "
188
+ assert_equal ( { place : 'Nowhere' } , cache_store . fetch ( key ) )
187
189
end
188
190
189
191
def test_fragment_cache_with_inheritance
@@ -194,9 +196,87 @@ def test_fragment_cache_with_inheritance
194
196
refute_includes ( base . keys , :special_attribute )
195
197
end
196
198
199
+ def test_uses_adapter_in_cache_key
200
+ render_object_with_cache ( @post )
201
+ key = "#{ @post . cache_key } /#{ adapter . class . to_s . demodulize . underscore } "
202
+ assert_equal ( @post_serializer . attributes , cache_store . fetch ( key ) )
203
+ end
204
+
205
+ # Based on original failing test by @kevintyll
206
+ # rubocop:disable Metrics/AbcSize
207
+ def test_a_serializer_rendered_by_two_adapter_returns_differently_cached_attributes
208
+ Object . const_set ( :Alert , Class . new ( ActiveModelSerializers ::Model ) do
209
+ attr_accessor :id , :status , :resource , :started_at , :ended_at , :updated_at , :created_at
210
+ end )
211
+ Object . const_set ( :UncachedAlertSerializer , Class . new ( ActiveModel ::Serializer ) do
212
+ attributes :id , :status , :resource , :started_at , :ended_at , :updated_at , :created_at
213
+ end )
214
+ Object . const_set ( :AlertSerializer , Class . new ( UncachedAlertSerializer ) do
215
+ cache
216
+ end )
217
+
218
+ alert = Alert . new (
219
+ id : 1 ,
220
+ status : 'fail' ,
221
+ resource : 'resource-1' ,
222
+ started_at : Time . new ( 2016 , 3 , 31 , 21 , 36 , 35 , 0 ) ,
223
+ ended_at : nil ,
224
+ updated_at : Time . new ( 2016 , 3 , 31 , 21 , 27 , 35 , 0 ) ,
225
+ created_at : Time . new ( 2016 , 3 , 31 , 21 , 37 , 35 , 0 )
226
+ )
227
+
228
+ expected_cached_attributes = {
229
+ id : 1 ,
230
+ status : 'fail' ,
231
+ resource : 'resource-1' ,
232
+ started_at : alert . started_at ,
233
+ ended_at : nil ,
234
+ updated_at : alert . updated_at ,
235
+ created_at : alert . created_at
236
+ }
237
+ expected_cached_jsonapi_attributes = {
238
+ id : '1' ,
239
+ type : 'alerts' ,
240
+ attributes : {
241
+ status : 'fail' ,
242
+ resource : 'resource-1' ,
243
+ started_at : alert . started_at ,
244
+ ended_at : nil ,
245
+ updated_at : alert . updated_at ,
246
+ created_at : alert . created_at
247
+ }
248
+ }
249
+
250
+ # Assert attributes are serialized correctly
251
+ serializable_alert = serializable ( alert , serializer : AlertSerializer , adapter : :attributes )
252
+ attributes_serialization = serializable_alert . as_json
253
+ assert_equal expected_cached_attributes , alert . attributes
254
+ assert_equal alert . attributes , attributes_serialization
255
+ attributes_cache_key = CachedSerializer . new ( serializable_alert . adapter . serializer ) . cache_key ( serializable_alert . adapter )
256
+ assert_equal attributes_serialization , cache_store . fetch ( attributes_cache_key )
257
+
258
+ serializable_alert = serializable ( alert , serializer : AlertSerializer , adapter : :json_api )
259
+ jsonapi_cache_key = CachedSerializer . new ( serializable_alert . adapter . serializer ) . cache_key ( serializable_alert . adapter )
260
+ # Assert cache keys differ
261
+ refute_equal attributes_cache_key , jsonapi_cache_key
262
+ # Assert (cached) serializations differ
263
+ jsonapi_serialization = serializable_alert . as_json
264
+ assert_equal alert . status , jsonapi_serialization . fetch ( :data ) . fetch ( :attributes ) . fetch ( :status )
265
+ serializable_alert = serializable ( alert , serializer : UncachedAlertSerializer , adapter : :json_api )
266
+ assert_equal serializable_alert . as_json , jsonapi_serialization
267
+
268
+ cached_serialization = cache_store . fetch ( jsonapi_cache_key )
269
+ assert_equal expected_cached_jsonapi_attributes , cached_serialization
270
+ ensure
271
+ Object . send ( :remove_const , :Alert )
272
+ Object . send ( :remove_const , :AlertSerializer )
273
+ Object . send ( :remove_const , :UncachedAlertSerializer )
274
+ end
275
+ # rubocop:enable Metrics/AbcSize
276
+
197
277
def test_uses_file_digest_in_cache_key
198
278
render_object_with_cache ( @blog )
199
- key = "#{ @blog . cache_key } /#{ ::Model ::FILE_DIGEST } "
279
+ key = "#{ @blog . cache_key } /#{ adapter . cached_name } / #{ ::Model ::FILE_DIGEST } "
200
280
assert_equal ( @blog_serializer . attributes , cache_store . fetch ( key ) )
201
281
end
202
282
@@ -205,13 +285,13 @@ def test_cache_digest_definition
205
285
end
206
286
207
287
def test_object_cache_keys
208
- serializer = ActiveModel :: Serializer :: CollectionSerializer . new ( [ @comment , @comment ] )
288
+ serializable = ActiveModelSerializers :: SerializableResource . new ( [ @comment , @comment ] )
209
289
include_tree = ActiveModel ::Serializer ::IncludeTree . from_include_args ( '*' )
210
290
211
- actual = CachedSerializer . object_cache_keys ( serializer , include_tree )
291
+ actual = CachedSerializer . object_cache_keys ( serializable . adapter . serializer , serializable . adapter , include_tree )
212
292
213
293
assert_equal actual . size , 3
214
- assert actual . any? { |key | key == ' comment/1' }
294
+ assert actual . any? { |key | key == " comment/1/ #{ serializable . adapter . cached_name } " }
215
295
assert actual . any? { |key | key =~ %r{post/post-\d +} }
216
296
assert actual . any? { |key | key =~ %r{author/author-\d +} }
217
297
end
@@ -226,13 +306,13 @@ def test_cached_attributes
226
306
attributes . send ( :cache_attributes )
227
307
cached_attributes = attributes . instance_variable_get ( :@cached_attributes )
228
308
229
- assert_equal cached_attributes [ @comment . cache_key ] , Comment . new ( id : 1 , body : 'ZOMG A COMMENT' ) . attributes
230
- assert_equal cached_attributes [ @comment . post . cache_key ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes
309
+ assert_equal cached_attributes [ " #{ @comment . cache_key } / #{ attributes . cached_name } " ] , Comment . new ( id : 1 , body : 'ZOMG A COMMENT' ) . attributes
310
+ assert_equal cached_attributes [ " #{ @comment . post . cache_key } / #{ attributes . cached_name } " ] , Post . new ( id : 'post' , title : 'New Post' , body : 'Body' ) . attributes
231
311
232
312
writer = @comment . post . blog . writer
233
313
writer_cache_key = writer . cache_key
234
314
235
- assert_equal cached_attributes [ writer_cache_key ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes
315
+ assert_equal cached_attributes [ " #{ writer_cache_key } / #{ attributes . cached_name } " ] , Author . new ( id : 'author' , name : 'Joao M. D. Moura' ) . attributes
236
316
end
237
317
end
238
318
@@ -287,16 +367,21 @@ def test_warn_on_serializer_not_defined_in_file
287
367
288
368
private
289
369
370
+ def cache_store
371
+ ActiveModelSerializers . config . cache_store
372
+ end
373
+
290
374
def render_object_with_cache ( obj , options = { } )
291
- serializable ( obj , options ) . serializable_hash
375
+ @serializable_resource = serializable ( obj , options )
376
+ @serializable_resource . serializable_hash
292
377
end
293
378
294
- def cache_store
295
- ActiveModelSerializers . config . cache_store
379
+ def adapter
380
+ @serializable_resource . adapter
296
381
end
297
382
298
383
def cached_serialization ( serializer )
299
- cache_key = CachedSerializer . new ( serializer ) . cache_key
384
+ cache_key = CachedSerializer . new ( serializer ) . cache_key ( adapter )
300
385
cache_store . fetch ( cache_key )
301
386
end
302
387
end
0 commit comments